Search code examples
javascriptfunctional-programmingcomposition

Functional composition in JavaScript


I know this is quite possible since my Haskell friends seem to be able to do this kind of thing in their sleep, but I can't wrap my head around more complicated functional composition in JS.

Say, for example, you have these three functions:

const round = v => Math.round(v);

const clamp = v => v < 1.3 ? 1.3 : v;

const getScore = (iteration, factor) =>
    iteration < 2 ? 1 :
    iteration === 2 ? 6 :
    (getScore(iteration - 1, factor) * factor);

In this case, say iteration should be an integer, so we would want to apply round() to that argument. And imagine that factor must be at least 1.3, so we would want to apply clamp() to that argument.

If we break getScore into two functions, this seems easier to compose:

const getScore = iteration => factor =>
    iteration < 2 ? 1 :
    iteration === 2 ? 6 :
    (getScore(iteration - 1)(factor) * factor);

The code to do this probably looks something like this:

const getRoundedClampedScore = compose(round, clamp, getScore);

But what does the compose function look like? And how is getRoundedClampedScore invoked? Or is this horribly wrong?


Solution

  • The compose function should probably take the core function to be composed first, using rest parameters to put the other functions into an array, and then return a function that calls the ith function in the array with the ith argument:

    const round = v => Math.round(v);
    
    const clamp = v => v < 1.3 ? 1.3 : v;
    
    const getScore = iteration => factor =>
        iteration < 2 ? 1 :
        iteration === 2 ? 6 :
        (getScore(iteration - 1)(factor) * factor);
    
    const compose = (fn, ...transformArgsFns) => (...args) => {
      const newArgs = transformArgsFns.map((tranformArgFn, i) => tranformArgFn(args[i]));
      return fn(...newArgs);
    }
    
    const getRoundedClampedScore = compose(getScore, round, clamp);
    
    console.log(getRoundedClampedScore(1)(5))
    console.log(getRoundedClampedScore(3.3)(5))
    console.log(getRoundedClampedScore(3.3)(1))