The current version of the Pipes manual uses the following two functions in one example:
stdout :: () -> Consumer String IO r stdout () = forever $ do str <- request () lift $ putStrLn str stdin :: () -> Producer String IO () stdin () = loop where loop = do eof <- lift $ IO.hIsEOF IO.stdin unless eof $ do str <- lift getLine respond str loop
As indicated in the tutorial itself, P.stdin is a bit more complicated due to the need to check the end of input.
Are there any good ways to rewrite P.stdin so as not to require a manual tail recursive loop and use larger order flow controllers like P.stdout? In an imperative language, I would use a structured while or break loop to do the same:
while(not IO.isEOF(IO.stdin) ){ str <- getLine() respond(str) } forever(){ if(IO.isEOF(IO.stdin) ){ break } str <- getLine() respond(str) }
source share