Created
October 9, 2016 16:14
-
-
Save ronen-e/e9fce2e85fb2614fad14b289312a8e54 to your computer and use it in GitHub Desktop.
compose functions
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
/** | |
* add the result of a function as an argument for the next function, e.g f(g(c)) | |
* @param {...Function} functions to be composed | |
* @return {Function} the composed function | |
*/ | |
function compose() { | |
var funcs = arguments; | |
return function composed(value) { | |
var i = funcs.length; | |
while(i--) { | |
value = funcs[i](value); | |
} | |
return value; | |
} | |
} | |
/* | |
usage example: | |
function g(c) { return c + 'A'; } | |
function f(x) { return x + 'B'; } | |
compose(f, g)('Test: '); // => f(g(c)) => 'Test: AB' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment