At some point I had to do something similar (summarise values by keys, 
assuming duplicate keys are in a continuous range) and used 
pipes-group https://hackage.haskell.org/package/pipes-group

On Thursday, 11 June 2015 10:53:19 UTC+10, Jacob Stanley wrote:
>
> I have a situation which I found easy to solve with `conduit` but could 
> not figure out with `pipes`.
>
> -- | Given a stream of key/value pairs, fold all the values associated 
> with a
> -- key in to a single value. Assumes that the stream has already been
> -- sorted/grouped by key.
> foldValues :: forall m k v. (Monad m, Eq k) => (v -> v -> v) -> Conduit 
> (k, v) m (k, v)
> foldValues step = goM =$= C.catMaybes
>   where
>     goM :: Conduit (k, v) m (Maybe (k, v))
>     goM = do
>         s <- C.mapAccum go Nothing
>         case s of
>           Nothing     -> yield Nothing
>           Just (k, x) -> yield (Just (k, x))
>
>     go :: (k, v) -> Maybe (k, v) -> (Maybe (k, v), Maybe (k, v))
>     go (k, v) (Nothing)                  = (Just (k, v),        Nothing)
>     go (k, v) (Just (k0, x)) | k == k0   = (Just (k, step x v), Nothing)
>                              | otherwise = (Just (k, v),        Just (k0, 
> x))
>
>
> It seems to be related to the fact that with `pipes` I cannot observe that 
> the stream has stopped emitting values. I had a quick look at `pipes-parse` 
> and it seems like maybe that would help?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to haskell-pipes+unsubscr...@googlegroups.com.
To post to this group, send email to haskell-pipes@googlegroups.com.

Reply via email to