Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created May 17, 2016 01:18
Show Gist options
  • Save andredublin/b37804fe46780bb6ee3d8734a3ed77f7 to your computer and use it in GitHub Desktop.
Save andredublin/b37804fe46780bb6ee3d8734a3ed77f7 to your computer and use it in GitHub Desktop.
composition and map
// composition = f(g(x))
var compose = function(f, g) {
return function(x) {
return f(g(x));
}
}
var squareAdd10 = compose(square, add10);
[1, 2, 3, 4, 5].map(squareAdd10); // 11, 14, 19, 26, 35
// or
var squared = [1, 2, 3, 4, 5].map(square); // 1, 4, 9, 16, 25
var added = [1, 2, 3, 4, 5].map(add10); // 11, 14, 19, 26, 35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment