Week 7
Quiz 16a

Assignment: Function composition

JavaScript is functional and functional languages should provide function composition. Let's assume we have functions like

const inc    = x => x + 1;
const double = x => x * 2;

Provide a general function then that can combine any such functions such that

inc.then(double).then(double)

can be used like

inc.then(double).then(double) (1) === 8

Note: you cannot assume what the functions are actually doing. It must work for all functions!

Solution
Function.prototype.then = function (f) {
    return x => f(this(x));
}

Your solution will be tested against:

add.then(mult).then(add) (1) === (1 + add_) * factor_ + add_