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.

1 Answer 1


It's pretty common to break up a chain into well-named intermediate results even if you don't need that result for anything other than continuing the chain. Putting those intermediate calculations into separate functions is also fairly common. It helps a lot with readability and maintainability.

All pure functional programs are essentially one giant expression, so you can often keep chains going for way too long if you are so inclined. It's not like imperative programming where a lot of things can't be done without going to a new statement. If you can think of a meaningful name for the intermediate result, break it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.