I haven't used Pipes before, but after going through the tutorial I found it really simple:
import Pipes
import qualified Pipes.Prelude as P
nums :: Producer Int IO ()
nums = each [1..20]
process :: Producer Int IO ()
process = nums >-> (P.drop 5) >-> (P.take 5)
result :: IO Int
result = P.fold (+) 0 id process
main = result >>= print
UPDATE:
As there is no "effectful" processing in the example we can even use Identity
monad as the base monad for pipe:
import Pipes
import qualified Pipes.Prelude as P
import Control.Monad.Identity
nums :: Producer Int Identity ()
nums = each [1..20]
process :: Producer Int Identity ()
process = nums >-> (P.drop 5) >-> (P.take 5)
result :: Identity Int
result = P.fold (+) 0 id process
main = print $ runIdentity result
UPDATE 1:
Below is the solution I came up with (for the gist link comment), but I feel like it can be made more elegant
fun :: Pipe Int (Int, Int) Identity ()
fun = do
replicateM_ 5 await
a <- replicateM 5 await
replicateM_ 5 await
b <- replicateM 5 await
yield (sum a, sum b)
main = f $ runIdentity $ P.head $ nums >-> fun where
f (Just (a,b)) = print (a,b)
f Nothing = print "Not enough data"