Can't resist a challenge.
Array.prototype.inject = function(init, f) {
var r = init
for(var i = 0; i < this.length; i++) {
r = f(r, this[i])
}
return r
}
> I'm wondering, can it be implemented in terms of map?
I don't think so; they return different things. (Think of it this way:
map always returns a list exactly as long as the list supplied to it, so
there's no way to pare it down to a single returned value.)
--
...computer contrarian of the first order... / http://aperiodic.net/phil/
PGP: 026A27F2 print: D200 5BDB FC4B B24A 9248 9F7A 4322 2D22 026A 27F2
--- --
<awkward> anyone around?
<Flav> no, we're all irregular polygons
-- seen on #debian
---- --- --
See the gist for an example of using it: http://tinyurl.com/enumerable-jsArray.prototype.inject = function(init, f) {var r, argc = arguments.lengthargc == 1 ? f = init : r = initthis.eachWithIndex(function(e,i){r = (i == 0 && argc == 1) ? e : f(r, e)})return r}