Skip to content

Instantly share code, notes, and snippets.

@ricealexander
Created October 10, 2020 19:32
Show Gist options
  • Save ricealexander/526a1539df6e9c33677837948b2c8a27 to your computer and use it in GitHub Desktop.
Save ricealexander/526a1539df6e9c33677837948b2c8a27 to your computer and use it in GitHub Desktop.
Deconstructing a recursive map from Reddit user tarley_apologizer, with an alternative
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