I am trying to learn the pipes package by writing my own sum function, and I am getting dumb. I would not want to use the utility functions from Pipes.Prelude (since it has sum and fold and other functions that make it trivial) and use only the information as described in Pipes.Tutorial . The tutorial does not talk about Proxy constructors, but if I look at sum and fold in the source, it uses these constructors, and I wonder if it is possible to write my sum function without knowing these low-level details.
I am having problems with how this function can continue to take values ββas long as there are available values, and then somehow return this amount to the user. I assume the type will be:
sum' :: Monad m => Consumer Int m Int
It seems to me that this may work, because this function can consume values ββuntil there are no more, and then returns the final amount. I would use it as follows:
mysum <- runEffect $ inputs >-> sum'
However, the function from Pipes.Prelude has the following signature instead:
sum :: (Monad m, Num a) => Producer am () -> ma
So, I think this is my first hurdle. Why does the sum function accept Producer as an argument and not use >-> to connect?
FYI I received the following after a response from danidiaz:
sum' = go 0 where go np = next p >>= \x -> case x of Left _ -> return n Right (_, p') -> go (n + 1) p'
pipe haskell
Ana
source share