Fun.js – Just a sketch
blog.fogus.me
blog.fogus.me
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! =OEdit: 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. =)
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. var gtThreshold = gt(100);
And then: filter(gtThreshold, array);
This is nicely fluent I would say.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?
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:
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!
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. :-)
Focusing on modern browsers and shimming the old ones is the way to go, IMHO
Here's my recommendation: http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell...