Skip to content

Instantly share code, notes, and snippets.

@mrosata
Created April 23, 2017 00:45
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 mrosata/eb9887774f5f1cf7435d6a4f86658b3c to your computer and use it in GitHub Desktop.
Save mrosata/eb9887774f5f1cf7435d6a4f86658b3c to your computer and use it in GitHub Desktop.
A better order for the arguments to the map function
// 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