This is a job for iteratees and iteratees like libraries.
Using the Proxy library.
import Control.Proxy
runProxy $ fromListS [1..10] >-> <processing> >-> printD >-> <more> processing>
Where <processing>
is the addition calculations you need to make.
Similar questions: lazy version of mapM, Is Haskell's mapM not lazy?
For example:
> labeledPrint label x = putStrLn $ label ++ show x
> runProxy $ fromListS [1..4] >-> printD >-> mapD (*2)
>-> useD (labeledPrint "Second printer: ")
1
Second printer: 2
2
Second printer: 4
3
Second printer: 6
4
Second printer: 8
If you reverse the order of application and use <-<
instead of >->
then it looks like normal function application.
runProxy $ useD (labeledPrint "Second printer: ") <-< mapD (*2)
<-< printD
<-< fromListS [1..4]