Skip to content

Instantly share code, notes, and snippets.

@idettman
Last active February 18, 2017 08:00
Show Gist options
  • Select an option

  • Save idettman/fc05af6c506d57bb79d9 to your computer and use it in GitHub Desktop.

Select an option

Save idettman/fc05af6c506d57bb79d9 to your computer and use it in GitHub Desktop.
JavaScript composition variations
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