Created
November 6, 2016 15:39
-
-
Save koozdra/ac7bf41416fe175507fbe74f0924074d to your computer and use it in GitHub Desktop.
Javascript compose using reduce
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(...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