News for hackers. Stuff that matters.
Re:Functional Programming using JavaScript by lelf on Monday November 24, 2014 @11:32AM (#8653119)
For those you want to learn the functional programming I'd suggest to immedeately commence looking at Ocaml/Haskell instead to see the real big picture. Because it's of course adorable that you can write map and compose in JS (it's 2014, it can do it everywhere), but FP = many more serious things.
Re:Functional Programming using JavaScript by coldtea on Monday November 24, 2014 @03:42PM (#8654623)
>Because it's of course adorable that you can write map and compose in JS (it's 2014, it can do it everywhere), but FP = many more serious things.

Or you know, stick to JS, because it's of course adorable that you can do functional programming in a functional language (we could do that since the 60s), but having access to the most prevalent language in the web and being employable is even better.

Re:Functional Programming using JavaScript by tel on Monday November 24, 2014 @01:29PM (#8653791)
Strong agreement. "Adorable" is exactly how I describe it. Even if it's already producing practical advantage in Javascript, it's mostly a cute toy given how it stacks up against "normal" usage.

Learn Haskell/OCaml and you'll see how to really take this kind of reasoning many, many orders of magnitude upward. Learn Coq/Agda to see yet another meteoric jump.

Re:Functional Programming using JavaScript by CrossEye on Monday November 24, 2014 @02:28PM (#8654210)
While I'm not upset that this has been posted here and has been garnering some attention, this talk wasn't designed for this kind of consumption. I have given this talk to two kinds of audiences:

* In a large corporate office with heterogeneous skill-sets among developers, but where almost all had some experience with Javascript.

* Javascript Users Groups

In each case, the idea was to make the case that FP could offer them practical advantages. The goal was definitely not to teach them FP or to suggest the best FP tool.

There are definitely better languages in which to do FP. But if for whatever reasons you're working in Javascript, I would suggest that a functional style could be useful.

Re:Functional Programming using JavaScript by tel on Monday November 24, 2014 @09:11PM (#8655949)
For that audience I agree with dipping toes. I just want to place hedges and guards against making "FP" (whatever that means) out to being better syntax for some kinds of streaming operations when it has much, much more to offer.
Re:Functional Programming using JavaScript by samspot on Monday November 24, 2014 @03:26PM (#8654556)
I hate to be this guy, but learning FP techniques that aren't available in my day to day language isn't terribly exciting. I want to see a short term benefit, so JS or Java focused articles and books are golden for me.
Re:Functional Programming using JavaScript by xwowsersx on Monday November 24, 2014 @03:30PM (#8654570)
The issue is that if you're going to be doing FP, you'll want to use a language in which it's not just possible, but natural and happens without an uphill battle. Similarly, if you're just trying to learn the concepts, you're going to have a lot of noise distracting you if you use a language like JS, etc in which FP concepts are quite unnatural to express.
Re:Functional Programming using JavaScript by coldtea on Monday November 24, 2014 @03:43PM (#8654632)
>The issue is that if you're going to be doing FP, you'll want to use a language in which it's not just possible, but natural and happens without an uphill battle.

Most FP paradigms are natural and quite easy in JS.

And learning Haskell can also be described as an "uphill battle".

Re:Functional Programming using JavaScript by cheepin on Monday November 24, 2014 @05:35PM (#8655121)
I think he was referring to Java
Re:Functional Programming using JavaScript by jbergens on Tuesday November 25, 2014 @06:26AM (#8657273)
Or, you want to use it in your day-to-day job where it might exist thousands upon thousands of lines of code in a not-so-FP language. I think just learning the concepts is great start. If you could rewrite your whole app, please do so. If not, see if you can write some parts with more FP style code. Like using LINQ in C# instead of rewriting everything to F#, or with Java 8 start using streams and other concepts.
Re:Functional Programming using JavaScript by kbeaty on Monday November 24, 2014 @02:00PM (#8653995)
While I am currently trying to teach myself Haskell for exactly this reason (understand the "real big picture" in FP), I don't know if there is much harm in starting the journey by exploring some FP concepts in languages one is already familiar with, despite how lacking the language is in that regard. This presentation does point out many features that JavaScript lacks from "true FP" languages.

I, for one, don't know if I would have ever tried to start learning Haskell if I didn't discover some of the benefits of FP first hand through a familiar language.

Re:Functional Programming using JavaScript by sagischwarz on Monday November 24, 2014 @01:29PM (#8653794)
I'm currently in one of the last chapters of Learn You a Haskell for Great Good (http://learnyouahaskell.com/), which is a really nice introduction into functional programming in general and the kind of thought process behind it. It also really helped me thinking recursively and out of the procedural/OOP box (and also learning Haskell, of course). Learning about FP has been the most eye-opening thing I have done since I'm programming.
Re:Functional Programming using JavaScript by zackmorris on Monday November 24, 2014 @02:29PM (#8654212)
I've been wondering lately, if we skip performance as a consideration (for example lazy evaluation), what is the difference between functional programming and imperative programming without globals, pointers, mutable variables or shared memory? In other words, would an imperative language without side effects (that could do static code analysis) be any different in practice than FP?

I'm very serious about this, because as far as I can tell, the next best aspect of FP might be first class functions, but those are starting to be used more in mainstream languages (at least the technique of applying transformations on generic data rather than writing special classes everywhere or using templates).

I'm beginning to feel that the real advantages of FP are being obfuscated by unusual syntax. There should be no reason why we can't go from some of the FP notations to the plain old algebra style (x=y) of C-style languages like Javascript. With better references and immutable variables, we should be able to split calculations up over several lines and have the compiler figure out how to optimize it into basically FP code.

P.S. I do know lisp, so I'm asking for a rather more enlightened answer than just to research it further.

Re:Functional Programming using JavaScript by Tyr42 on Monday November 24, 2014 @08:05PM (#8655755)
Consider one particular case, where we need to read in a gig of data (made of many records), filter out some records, compute some stream of transformed versions of those records, and then write out the rolling average.

This actually sounds pretty nice to write as a shell pipeline (and that's a really good way to do it), but really, it's likely that you'd end up writing some code like

    main() {
      q = queue(5);
      avg = 0;
      while(!eof) {
        r, eof = get_record();
        t = transform(r);
        if(q.size() >= 5){
           avg -= q.pop();
        }
        avg += t;
        q.push(t);
        print  avg/q.size();
      }
    }
or something, but it'd actually be annoying to extract out the logic for handling the rolling average and split it out from the looping code, at least if you want to maintain constant memory use, and if you had to do something similar in a few places, you'd end up duplicating code.

I claim that it's possible to factor that code nicely out in Haskell, and still get the compiler to generate the same machine code as the C loop. This doesn't particularly use first class functions, and more relies on the combo of sufficiently smart compiler and sufficiently strong guarantees about what code will do.

I also take slight offense at calling C-style languages "plain old algebraic style", since it'd be really strange in algebra to say both x=5 and x=6. I would not hesitate to call FP languages where you can substitute equals for equals and such the algebraic languages.

Re:Functional Programming using JavaScript by tel on Monday November 24, 2014 @07:08PM (#8655526)
Pure FP is very different from imperative languages because it has no notion of temporal order. You can (in truly pure FP) evaluate things in whatever order you choose and stop as soon as you're satisfied. The language cannot care at all [0].

That said, FP often involves the introduction of monads which restore sequencing (along with many other beneficial effects). A monadic FP computation is very similar to a plain imperative computation... just with all of the arbitrary details automatically selected to be coherent and optimal.

You can even pretty easily embed OO in FP languages (though it gets a little hairy sometimes) since as soon as you can simulate open recursion somehow you can get late-binding as you like. See Oleg's O'Haskell papers for this kind of nonsense.

[0] In this regard, even Haskell is impure since it allows for non-terminating computation which can be seen as an impurity since you cannot expect to just evaluate `let x = x in x` repeatedly and get anywhere meaningful. Other languages like Coq, Agda, Idris are terminating (thus not Turing Complete) and therefore truly pure.

Re:Functional Programming using JavaScript by zackmorris on Monday November 24, 2014 @08:33PM (#8655837)
Thank you, I hadn't considered that FP can evaluate in arbitrary order or simultaneously. That makes sense, because if there are no dependencies between code paths (so no side effects) then there's no reason they can't be executed at the same time. They didn't mention that back in the Scheme course I took in college, and I think something vitally important was lost on us (although in fairness they did emphasize that the code was evaluated, not run sequentially).

Seeing how monads reintroduce temporal dependence is also an astounding insight, and I can't believe that I've never seen them described that way. That means that the monad has more in common with, say, the mutex, than it does with I/O. I've always tried to approach them with metaphors like sockets and utterly failed to grok anything for more than a couple of days.

I'm thinking that if Haskell is an "impure" functional language, then I would like to see a "pure" imperative language. I envision it as similar to Go but multithreaded and basically every function in the language would be its own process, communicating with sockets (so basically a shell with C-style syntax). The overhead could be largely eliminated with a stackless context switcher. It wouldn't be able to achieve the optimization of FP but it might be more approachable to the mainstream and by extension maybe iterate faster and lead to higher productivity. I’d like to see it take the place of OpenCL/CUDA and SIMD because I find them tedious compared to more elegant languages like MATLAB/Octave.

Re:Functional Programming using JavaScript by tel on Monday November 24, 2014 @08:54PM (#8655903)
I'd suggest taking a look at Coq if you want something more pure than Haskell. I'll warn you that this comes with some degree of added sophistication! Coq is not just a programming language but instead a proof system which can be used to verify programs as correct (not just unit tested) and then extract the skeletal, underlying program as OCaml, Haskell, or Scheme code.

http://adam.chlipala.net/cpdt/

True purity actually doesn't look much like Go at all. Instead, it means that every expression literally can be evaluated and replaced with its value in a completely arbitrary order. This massively simplifies the meaning of a language and opens it up to bear much more structure (such as that of a mathematical proof).

Re:Functional Programming using JavaScript by Dewie on Monday November 24, 2014 @09:11PM (#8655948)
Idris is Turing Complete. Total functions are opt-in.
Re:Functional Programming using JavaScript by tel on Monday November 24, 2014 @09:13PM (#8655959)
Idris' opt-in totality is a weird beast since those opts are extralinguistic. I really consider it to be a downside when a language is turing complete these days—I'd rather Idris just use a partiality monad.

So, maybe I'm just using the power of wishful thinking with my characterization above.

Re:Functional Programming using JavaScript by dustingetz on Monday November 24, 2014 @02:43PM (#8654326)
It's different in practice because languages have built-in "opinions" (often codified by syntax, or the standard library, or other ways) that heavily influence their ecosystem.

Functional javascript, or python, is not awesome because the default datastructures are mutable, and the ecosystem of libraries are written in an OOP style using mutable data structures. There is some small value in writing the code you control in as FP a way as possible, but you aren't going to have a ton of success in that unless you are willing to rewrite your dependencies to also be more functional.

ReactJS may be a killer app that can influence javascript's "opinions" at the ecosystem scale, but that is going to take more time. I've been using ReactJS since it came out as tech lead on small teams, and it was not without challenges to get an OOP trained team to use React well, and to avoid the OO parts of React.

Edit: In theory, yes, if you could somehow statically enforce that your C code (and dependencies, including the OS) have no side effects, then some hypothetical compiler could combine your imperative C statements into functional expressions, though I don't know what advantage that would give you. Maybe I have misunderstood your post.

Re:Functional Programming using JavaScript by Dewie on Monday November 24, 2014 @09:09PM (#8655943)
> I'm beginning to feel that the real advantages of FP are being obfuscated by unusual syntax.

Or that unusual syntax is precisely motivated by trying to seamlessly and naturally express FP code.

Either that or they were just being intentionally obtuse... come on, man. Which sounds more likely of these two? I'd personally never want to program in a language with the semantics of Haskell, and the syntax of a C-like language.

> With better references and immutable variables, we should be able to split calculations up over several lines and have the compiler figure out how to optimize it into basically FP code.

Or just dive the code up into whatever temporary and intermediate steps that you need with let-in expressions and where-declarations? I mean, why not? If what you want the compiler to process is FP code, then just divide it up like you would divide up FP code. FP /= one liners.

Re:Functional Programming using JavaScript by codygman on Monday November 24, 2014 @05:14PM (#8655038)
While I love and use Haskell a lot I would recommend Purescript [0] over Haskell or Ocaml.

Even if the only reason (hint: it's not) were that Purescript uses familiar tooling such as NPM, node, CommonJS modules, bower, etc and would allow users to focus on learning functional programming faster.

For those that wasn't more than the examples, there is the wonderfully comprehensive, practical, and just plain fun Purescript book[1].

0: http://www.purescript.org/

1: https://leanpub.com/purescript/read

Re:Functional Programming using JavaScript by KurtMueller on Monday November 24, 2014 @11:47AM (#8653202)
Edx has a course in functional programming using Haskell. I haven't taken it yet but I'm going over Christmas.
Re:Functional Programming using JavaScript by atrilumen on Monday November 24, 2014 @12:49PM (#8653542)
Introduction to Functional Programming https://www.edx.org/node/2126#.VGrvnnVGh5Q