Skip to content

Instantly share code, notes, and snippets.

@amowu
Created October 30, 2016 08:45
Show Gist options
  • Select an option

  • Save amowu/49786ee83cb9e9ed0d22cefb3a1ab614 to your computer and use it in GitHub Desktop.

Select an option

Save amowu/49786ee83cb9e9ed0d22cefb3a1ab614 to your computer and use it in GitHub Desktop.
Compose
var compose = function (f, g) {
return function (x) {
return f(g(x));
};
};
var toUpperCase = x => x.toUpperCase();
var exclaim = x => x + '!';
var shout = compose(exclaim, toUpperCase);
shout('hello world');
// "HELLO WORLD!"
var head = x => x[0];
var reverse = reduce((acc, x) => [x].concat(acc), []);
var last = compose(head, reverse);
last(['hello', 'world']);
// 'world'
var loudLastUpper = compose(shout, last);
loudLastUpper(['hello', 'world']);
// 'WORLD!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment