I have not used Pipes before, but after going through the tutorial, I found it very 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:
Since there is no โefficientโ processing in this example, we can use the Identity monad as the base monad for the channel:
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 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"
Ankur source share