Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Compose = (...functions) => {
- return val => {
- let i = 0,
- len = functions.length,
- fnResult = functions[i](val);
- while (++i < len) {
- fnResult = functions[i](fnResult);
- }
- return fnResult;
- };
- };
- const add7 = num => num + 7;
- const squared = num => num * num;
- const subtract13 = num => num - 13;
- const add7ThenSquareThenSubtract13 = Compose(add7, squared, subtract13);
- add7ThenSquareThenSubtract13(5); // 131
- add7ThenSquareThenSubtract13(0); // 2
- add7ThenSquareThenSubtract13(100); // 11436
- // Let's say 'val' is 5.
- // 1. Call add7(val), result is 12.
- // 2. Call squared(12), result is 144
- // 3. Call subtract13(144), result is 131
- // subtract13( squared( add7(val) ) ) === 131
- const Compose = (...functions) => {
- return val => {
- return functions.reduce((prevVal, currFunc) => {
- return currFunc(prevVal);
- }, functions[0](val));
- };
- };
- const Compose = (...args) => v => args.reduce((p, c) => c(p), v)
Add Comment
Please, Sign In to add comment