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.
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. =)
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.
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. ;-)
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?
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).
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.
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.
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
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).
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.
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. =)