To quit prematurely
It is at least possible, but I think this is not a good idea. Functional composition should behave as one function. This is an atomic operation. Therefore, premature exit is not provided out of the box.
Take a step back. The composition of functions n is a classic reduction:
compn performs a similar calculation, for example:
const I = x => x; const inc = x => x + 1; const computation = x => inc(inc(inc(inc(I(x))))); console.log( computation(0)
If we want to leave the composition prematurely, we must adapt both the iterative algorithm ( foldr ) and the composition ( compn ):
The composition continues until the current return value is less than two. The code is hard to understand. I am not even trying to explain it (this is not worth the effort). In any case, compWhile performs the following calculation:
const comp = f => g => x => f(g(x)); const I = x => x; const inc = x => x + 1; const lt = y => x => x < y; const lt2 = lt(2); const computation = x => lt2(x) ? comp(x => lt2(x) ? comp(x => lt2(x) ? comp(x => lt2(x) ? comp(I) (inc) (x) : x) (inc) (x) : x) (inc) (x) : x) (inc) (x) : x; console.log( computation(0)
Branching composition
Just divide the composition into smaller compositions that represent the desired branches. If you try to create conditional functions, you will inevitably get unreadable code.
If you have a simple condition, and you do not want to split your composition, at least make the branching as explicit as possible, for example, with a conditional operator :? :
const foldr = f => acc => xs => xs.reduceRight((acc, x, i) => f(x) (acc, i), acc); const comp = f => g => x => f(g(x)); const I = x => x; const inc = x => x + 1; const compn = foldr(comp) (I); console.log( compn([x => x >= 2 ? x : inc(x), inc, inc]) (0)
Conclusion
The composition of functions is an atomic operation. Do not try to exit prematurely or branch out inside the composition.
source share