Created
October 30, 2016 08:45
-
-
Save amowu/49786ee83cb9e9ed0d22cefb3a1ab614 to your computer and use it in GitHub Desktop.
Compose
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
| 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