Last active
October 13, 2016 18:01
-
-
Save HugoDF/9b2af61a8565396079b9fa22226b65c1 to your computer and use it in GitHub Desktop.
Recursive implementation of map in ES5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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