Skip to content

Instantly share code, notes, and snippets.

@jfarmer
Created October 12, 2009 17:57
Show Gist options
  • Save jfarmer/208582 to your computer and use it in GitHub Desktop.
Save jfarmer/208582 to your computer and use it in GitHub Desktop.
var inject = function(iterable, initial, func) {
var cur = initial
for(var i = 0;i < iterable.length;i++) {
cur = func(cur, iterable[i])
}
return cur
}
var map = function(arr, func) {
return inject(arr, [], function(memo, val) {
memo.push(func(val))
return memo
})
}
map([1,2,3], function(x) { return x + 1 }) // #=> [2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment