Created
October 10, 2020 19:32
-
-
Save ricealexander/526a1539df6e9c33677837948b2c8a27 to your computer and use it in GitHub Desktop.
Deconstructing a recursive map from Reddit user tarley_apologizer, with an alternative
This file contains hidden or 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 (array, mapFunction) { | |
if (array.length === 0) return [] | |
return [ | |
mapFunction(array[0]), | |
...map(array.slice(1), mapFunction) | |
] | |
} | |
map([1, 2, 3], x => x * 2) // [2, 4, 6] | |
function map (array, mapFunction) { | |
const mappedValues = [] | |
for (value of array) { | |
mappedValues.push(mapFunction(value)) | |
} | |
return mappedValues | |
} | |
map([1, 2, 3], x => x * 2) // [2, 4, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment