JavaScript function that composes other functions to create a master function 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) =&gt; {
    return val =&gt; {
        let i = 0,
            len = functions.length,
            fnResult = functions[i](val);

        while (++i &lt; len) {
            fnResult = functions[i](fnResult);
        }

        return fnResult;
    };
};


Application:

const add7 = num =&gt; num + 7;
const squared = num =&gt; num * num;
const subtract13 = num =&gt; 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) =&gt; {
    return val =&gt; {
        return functions.reduce((prevVal, currFunc) =&gt; {
            return currFunc(prevVal);
        }, functions[0](val));
    };
};


It's not pretty, but it works--and it's functional. ;)
One little suggestion on your reduce version is to make the initial value not the first function's result. This way, if ever the arguments is empty functions[0](val) won't call a missing function and throw up. Also makes reduce call the callback for each item, which makes it less of a cognitive burden.

Also, arrow functions that have expressions as its body implicitly returns the expression's value. You can have something as tiny as the following:

const Compose = (...args) =&gt; v =&gt; args.reduce((p, c) =&gt; c(p), v)