Created
April 23, 2017 00:45
-
-
Save mrosata/eb9887774f5f1cf7435d6a4f86658b3c to your computer and use it in GitHub Desktop.
A better order for the arguments to the map function
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
// One suggestion I have on the way I wrote map in this video: | |
// https://www.youtube.com/watch?v=qTeeVd8hOFY | |
// Instead of passing the transform function as the last param | |
// and the array as the first... it would be better to pass the | |
// transformFn first and the array as the last parameter to map. | |
function map (transformFn, array) { | |
const rv = [] | |
for (let i = 0; i < array.length; i++) { | |
const nextItem = array[i] | |
const mappedItem = transformFn(nextItem) | |
rv.push(mappedItem) | |
} | |
return rv | |
} | |
// The reason this would be better is because if the mapped function | |
// was curried (see my other video), then we could write a function | |
// such as... | |
const _map = curry(map) | |
const addTenToAll = _map((n) => n + 10) | |
addTenToAll([1, 10, 11, 20]) // [11, 20, 21, 30] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment