Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Last active October 13, 2016 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoDF/9b2af61a8565396079b9fa22226b65c1 to your computer and use it in GitHub Desktop.
Save HugoDF/9b2af61a8565396079b9fa22226b65c1 to your computer and use it in GitHub Desktop.
Recursive implementation of map in ES5.
function map(arr, fn) {
var head = arr[0];
var tail = arr.slice(1);
if(head === undefined && tail.length === 0) return [];
if(tail.length === 0) {
return [ fn(head) ];
}
return [].concat(fn(head), map(tail, fn));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment