Skip to content

Instantly share code, notes, and snippets.

@koozdra
Created November 6, 2016 15:39
Show Gist options
  • Save koozdra/ac7bf41416fe175507fbe74f0924074d to your computer and use it in GitHub Desktop.
Save koozdra/ac7bf41416fe175507fbe74f0924074d to your computer and use it in GitHub Desktop.
Javascript compose using reduce
function compose(...fns) {
return x => _.reduceRight(fns, (acc, f) => f(acc), x);
}
const square = x => x * x;
const double = x => x + x;
const squareDouble = compose(square, double);
const test = compose(); // empty compose acts like identity
console.log(squareDouble(3), test(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment