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?
composefunction and one shouldn't invent newcomposefunctions to suit a specific situation.(b -> c) -> (a -> b) -> a -> c) is just the most basic form of function composition. There are more complex forms of function composition too such as(c -> d) -> (a -> b -> c) -> a -> b -> d. In this particular question, the form of function composition is(a -> b -> c) -> (d -> a) -> (e -> b) -> d -> e -> c. Now, more complex forms of function composition can be decomposed into simpler forms. Asking what the compose function looks like (and how to simplify it) is a reasonable question.