I wrote a little Compose
function, which takes a variable number of arguments of type function
and then calls each of those in order, passing in the value of the previously called function.
My function works just like I want it to, however since the function provides a way to program functionally, I figured it's application should be functional as well--but it's not.
The Compose
function:
const Compose = (...functions) => {
return val => {
let i = 0,
len = functions.length,
fnResult = functions[i](val);
while (++i < len) {
fnResult = functions[i](fnResult);
}
return fnResult;
};
};
Application:
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
What it's doing under the hood:
// 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
Anyone have any idea how I can make the logic inside the Compose
function more functional? I am having a hard time since I am just now learning to think in a functional way. Is it even possible? I don't know of any ways to loop an array without using for
or while
.
UPDATE:
Never mind, I figured it out. Didn't think I could use reduce
for this but I suppose I can:
const Compose = (...functions) => {
return val => {
return functions.reduce((prevVal, currFunc) => {
return currFunc(prevVal);
}, functions[0](val));
};
};
It's not pretty, but it works--and it's functional. ;)