Last active
August 4, 2022 06:34
-
-
Save sethdavis512/a85b26fb708ce078f156242c9d4cd310 to your computer and use it in GitHub Desktop.
This file contains 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
// Building-blocks to use for composition | |
const double = x => x + x; | |
const triple = x => 3 * x; | |
const quadruple = x => 4 * x; | |
// Function composition enabling pipe functionality | |
const pipe = (...fns) => input => [...fns].reduce((acc, fn) => fn(acc), input); | |
// Composed functions for multiplication of specific values | |
const multiply6 = pipe(double, triple); | |
const multiply9 = pipe(triple, triple); | |
const multiply16 = pipe(quadruple, quadruple); | |
const multiply24 = pipe(double, triple, quadruple); | |
// Usage | |
multiply6(6); // 36 | |
multiply9(9); // 81 | |
multiply16(16); // 256 | |
multiply24(10); // 240 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment