I have a set of pure functions that can be composed almost trivially in a pipe as
initialValue -> [f] -> [g] -> [h] -> ... -> [m] -> [n] -> [o] -> outputValue
The problem I encountered is that one of the functions in the middle of the chain (e.g.
[n]) requires the output of one of the earliest functions (e.g
[g]) in addition to the output of the previous function
[m]. Since I am new to functional programming I was wondering how more experienced programmers approach this issue, which I guess is commonly encountered. I can think in some of the following strategies but if there is any better one, please let me know.
One option is to use a sub-pipe and an intermediate variable:
subpipe = [h] -> ... -> [m] intermediateResult = initialValue -> [f] -> [g] intermediateResult -> subpipe -> curry(m)(intermediateResult) -> [n] -> [o] -> outputValue
To get rid of the intermediate variable, I could extract the last line into a function
[s] and the final composition would read
initialValue -> [f] -> [g] -> [s]
But I'm not sure if this is considered less explicit or harder to read.
Another option is to keep passing the result along the chain, although I believe this is considered an anti-pattern.