Hi Tran

So with the foldValues implementation below, I need to have the the 
Producer in order to apply the transformation.

But what if I only have a Consumer?

Can I transform a Consumer in the same, but opposite, way? That is, take a 
Consumer which expects folded (k,v) pairs and create a consumer which 
expects groups of (k,v) pairs which haven't been folded yet.

On Friday, June 12, 2015 at 10:59:07 AM UTC+10, Tran Ma wrote:
>
> Hi Jacob,
>
> On Friday, 12 June 2015 06:19:21 UTC+10, Jacob Stanley wrote:
>>
>> 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`?
>>
>  
> I don't think it is possible to both retain the same semantics (emitting 
> only final folded values) and have the `Pipe` signature, because `Pipe` is 
> a pull-based stream. Even if you layer some sort of state in the monad 
> underneath the `Pipe`, it needs to do some buffering before emitting the 
> final value.
>
>
>> Ideally I would like to be able to apply this operation to a consumer as 
>> well.
>>
>
> I'm not sure what you mean, could you give an example?
>  
>
>> 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