Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active August 4, 2022 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sethdavis512/a85b26fb708ce078f156242c9d4cd310 to your computer and use it in GitHub Desktop.
Save sethdavis512/a85b26fb708ce078f156242c9d4cd310 to your computer and use it in GitHub Desktop.
// 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