Last active
February 18, 2017 08:00
-
-
Save idettman/fc05af6c506d57bb79d9 to your computer and use it in GitHub Desktop.
JavaScript composition variations
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
| function compose (a, b) | |
| { | |
| return function applyArguments (c) | |
| { | |
| return a(b(c)); | |
| } | |
| } | |
| /** | |
| * @param {Function} a | |
| * @param {Function} b | |
| * @returns {*} | |
| */ | |
| function compose (a, b) | |
| { | |
| return (...args) => a(b(...args)); | |
| } | |
| function compose (...functions) | |
| { | |
| return (...args) => | |
| { | |
| for (var i = functions.length - 1; i > -1; i--) | |
| { | |
| args = [functions[i](...args)]; | |
| } | |
| return args[0]; | |
| } | |
| } | |
| // Test compose implementations | |
| function add1(a) | |
| { | |
| return a + a; | |
| } | |
| function mult1(a) | |
| { | |
| return a * a; | |
| } | |
| function divide1(a) | |
| { | |
| return a/(a/2); | |
| } | |
| var f = compose(mult1, add1); | |
| //var f = compose(divide1, mult1, add1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment