Thanks for the tips Tran and John, this is what I came up with:

foldValues :: (Monad m, Eq k) => (v -> v -> v) -> Producer (k, v) m r -> 
Producer (k, v) m r
foldValues append xs =
    P.concat <-< folds step Nothing id (view (groupsBy keyEq) xs)
  where
    keyEq (k, _) (k', _) = k == k'

    step (Nothing)      (k, v) = Just (k, v)
    step (Just (_, v0)) (k, v) = Just (k, v0 `append` v)

Is there any way I can get this to have the type `Pipe (k, v) (k, v) m r`?

Ideally I would like to be able to apply this operation to a consumer as 
well.

On Thursday, June 11, 2015 at 5:05:45 PM UTC+10, Tran Ma wrote:
>
> 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