News for hackers. Stuff that matters.
Re:Fun.js – Just a sketch by thepicard on Thursday June 06, 2013 @06:11PM (#5835495)
A lot of these examples are wrong...

    function double(n) { return 2*n; }
    map(double, [1,2,3])
    //=> [1, 4, 9]
should be => [2, 4, 6]

    function greaterThan(l, r) {
      return l > r;
    }
    greaterThan(5, 100);
    //=> true
should be => false

    maybeGT(5)(10000000);
    //=> false
Based on your earlier declaration of greaterThan this result is actually correct. The curried application ordering is also correct and flip is unneeded.

    schonfinkel(function(a1,a2) {return [a1,a2];})(1)(2);
    //=> [1, 2]
I would proofread the rest but I am about to be late for class! =O

Edit: since i'm going to complain I might as well say something nice as well. I did like this article example correctness aside, and I hadn't seen that implementation of pipeline before! Much awesome. =)

Re:Fun.js – Just a sketch by thepicard on Thursday June 06, 2013 @07:01PM (#5835797)
Actually, I think the original intent regarding greater than was:

    greaterThan(100, 5);
    //=> true
That is, l is greater than r, and to then properly curry the arguments do need to be flipped such that maybeGT(5) results in a function that returns whether the argument is greater than 5.
Re:Fun.js – Just a sketch by fogus on Thursday June 06, 2013 @07:19PM (#5835907)
Exactly right. We like to be able to do something like:

    var gtThreshold = gt(100);
And then:

    filter(gtThreshold, array);
This is nicely fluent I would say.
Re:Fun.js – Just a sketch by fogus on Thursday June 06, 2013 @07:21PM (#5835920)
Someone has already addressed the maybeGT case, but you were right about the double problem. I had originally made it a squaring function, but for some dumb reason decided to make it a doubler instead just before submitting. There is a lesson in there about last moment changes. ;-)
Re:Fun.js – Just a sketch by dhucerbin on Friday June 07, 2013 @07:35AM (#5838311)
Some questions from JS novice:

Why map isn't implemented:

    function map(fun, coll) {
      if (exists(coll)) return _map(fun)(coll);
      return _map(fun);
    }
Why use arguments when you can name your parameter in declaration?

And why _map is defined in global scope? Shouldn't it be better defined inside map function? What are up/downsides in performance for this move?

Re:Fun.js – Just a sketch by dweinus on Thursday June 06, 2013 @06:05PM (#5835463)
Sorry to nitpick, but I got hung up right at the beginning with his "truthy" function.

It returns true on values such as NaN, zero, or an empty string. I don't think I'm alone in considering those to be "falsy" values, for example:

http://www.sitepoint.com/javascript-truthy-falsy/

Re:Fun.js – Just a sketch by fogus on Thursday June 06, 2013 @06:16PM (#5835521)
Technically the are JS falsey, but I choose not to treat them that way. I rarely want to treat the result of a calculation as a boolean condition. However, I often want to know if a calculation isNaN or isEmpty. Just because something is falsey doesn't mean it should be treated that way universally.
Re:Fun.js – Just a sketch by dweinus on Thursday June 06, 2013 @06:27PM (#5835580)
Right on, I get that, but truthy(NaN) and truthy(5) both return true, so you still don't know if the calculation isNaN.

Btw, I should have said first: I love the article in general and it's great to see these techniques becoming more mainstream in javascript. Thank you!

Re:Fun.js – Just a sketch by fogus on Thursday June 06, 2013 @07:24PM (#5835944)
I would probably structure the problem like this:

    var ans = someCalculation(x,y);
    
    if (truthy(ans) && !isNaN(ans))
      //do something
    else
      //do something else
The point being I like to dispatch conditions on boolean results of certain properties of my results rather than the result directly. It helps me to keep my sanity.

Thanks for the kind words BTW. :-)

Re:Fun.js – Just a sketch by kivikakk on Thursday June 06, 2013 @06:28PM (#5835586)
It's probably as common to have them falsy as truthy; notably, they're truthy in Ruby.
Re:Fun.js – Just a sketch by tomTheBom on Thursday June 06, 2013 @10:13PM (#5836718)
Very interesting post but I'm having trouble with one thing. pipeline(5) returns 5. When I try to follow that path, I get to this: reduce(function(l,r) { return r(l); }, seed, funs) with seed=5 and funs=[] (an empty array). How does that reduction evaluate to 5? In other words, what is "r(l)" when l=5 and r=the first member of an empty array?
Re:Fun.js – Just a sketch by fogus on Thursday June 06, 2013 @11:47PM (#5837101)
In reduce the accumulator starts as the seed and is changed in the forEach for each elem. Since there are so elems (funs) the accumulator never changes and returns as the value of seed (5).
Re:Fun.js – Just a sketch by tomTheBom on Friday June 07, 2013 @10:14AM (#5838986)
Of course. Shoulda seen that. ;) forEach on an empty collection returns immediately. Thanks.
Re:Fun.js – Just a sketch by dugmartin on Thursday June 06, 2013 @06:21PM (#5835543)
Just a note - forEach doesn't work in IE8. I wonder at what point can we stop caring about that?
Re:Fun.js – Just a sketch by gotofritz on Friday June 07, 2013 @05:40AM (#5838028)
there are shims - the MDN one works well, or you can download this library to shim the whole of ES5 https://github.com/kriskowal/es5-shim (other libraries are availble)

Focusing on modern browsers and shimming the old ones is the way to go, IMHO

Re:Fun.js – Just a sketch by raam86 on Thursday June 06, 2013 @05:45PM (#5835363)
What would you say are the skills required to fully understand this?
Re:Fun.js – Just a sketch by barefoot on Thursday June 06, 2013 @05:55PM (#5835413)
Possibly not too high. Try using map, filter, and reduce on some example problems and you'll pick it up fairly quickly. Project Euler makes for decent fodder. When you've successfully managed to apply them then try rewriting the functions (delete the implementation you are using - such as the library above - and rewrite it from scratch).
Re:Fun.js – Just a sketch by innguest on Thursday June 06, 2013 @07:54PM (#5836127)
I wouldn't recommend Project Euler - it's math-based and that would be a roundabout way of learning anything else but math.

Here's my recommendation: http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell...

Re:Fun.js – Just a sketch by egonschiele on Thursday June 06, 2013 @05:51PM (#5835391)
It's a pretty fast paced intro to a lot of functional programming concepts. You can read a guide like learnyouahaskell.com, which will take you on a gentler ride.
Re:Fun.js – Just a sketch by raam86 on Thursday June 06, 2013 @06:42PM (#5835669)
Thanks I tried haskell a few ties but never got the real life application to that.
Re:Fun.js – Just a sketch by graup on Thursday June 06, 2013 @06:13PM (#5835503)
Good read, nice intro into functional programming. Thanks!