Skip to content

Instantly share code, notes, and snippets.

@ronen-e
Created October 9, 2016 16:14
Show Gist options
  • Save ronen-e/e9fce2e85fb2614fad14b289312a8e54 to your computer and use it in GitHub Desktop.
Save ronen-e/e9fce2e85fb2614fad14b289312a8e54 to your computer and use it in GitHub Desktop.
compose functions
/**
* 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