The various list manipulation prototypes are always great. Since you want only one per post, I'll just post foldl
, which I discovered via SML (it "folds" the list, left to right, it has a counter part in foldr
of course).
Array.prototype.foldl = function(fnc,start) {
var a = start;
for (var i = 0; i < this.length; i++) {
a = fnc(this[i],a);
}
return a;
}
Some trivial examples could be:
var l = ["hello" , "world"];
l.foldl(function(i, acc) { return acc+" "+i; }, "") // => returns "hello world"
Sadly, the failure of standard DOM methods to return true arrays makes alot of these such methods rather useless. And if you're using a Lib of some sort, they often define methods like these already (map, filter, exists, etc).