-- | 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.catMaybeswheregoM :: Conduit (k, v) m (Maybe (k, v))goM = dos <- C.mapAccum go Nothingcase s ofNothing -> yield NothingJust (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))
Is there any way I can get this to have the type `Pipe (k, v) (k, v) m r`?foldValues :: (Monad m, Eq k) => (v -> v -> v) -> Producer (k, v) m r -> Producer (k, v) m rfoldValues append xs =P.concat <-< folds step Nothing id (view (groupsBy keyEq) xs)wherekeyEq (k, _) (k', _) = k == k'step (Nothing) (k, v) = Just (k, v)step (Just (_, v0)) (k, v) = Just (k, v0 `append` v)
Thanks for the tips Tran and John, this is what I came up with:Is there any way I can get this to have the type `Pipe (k, v) (k, v) m r`?foldValues :: (Monad m, Eq k) => (v -> v -> v) -> Producer (k, v) m r -> Producer (k, v) m rfoldValues append xs =P.concat <-< folds step Nothing id (view (groupsBy keyEq) xs)wherekeyEq (k, _) (k', _) = k == k'step (Nothing) (k, v) = Just (k, v)step (Just (_, v0)) (k, v) = Just (k, v0 `append` v)
Ideally I would like to be able to apply this operation to a consumer as well.
--
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-pipe...@googlegroups.com.
To post to this group, send email to haskel...@googlegroups.com.