Skip to content

Instantly share code, notes, and snippets.

@jremmen
Last active August 29, 2015 14:02
Show Gist options
  • Save jremmen/5928ddb7412b676021a4 to your computer and use it in GitHub Desktop.
Save jremmen/5928ddb7412b676021a4 to your computer and use it in GitHub Desktop.
js: multimap and multireduce array methods
Array.prototype.multimap = function(f) {
return this.length == 0 ? [] : [(this[0] instanceof Array ? this[0].multimap(f) : f(this[0]))].concat(this.slice(1).multimap(f));
}
Array.prototype.multireduce = function(f, z) {
return this.length == 0 ? z : f((this[0] instanceof Array ? this[0].multireduce(f, z) : this[0]), this.slice(1).multireduce(f, z));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment