@@ -159,7 +159,9 @@ module Streamly.Internal.Data.Fold
160 -- , demuxWith
160 -- , demuxWith
162 -- , demuxWith_
163 -- , demuxWith_
164 -- * Classifying
166 -- * Classifying
@@ -1016,6 +1018,32 @@ demux :: (Monad m, Ord k)
1016 => Map k (Fold m a b) -> Fold m (k, a) (Map k b)
1018 => Map k (Fold m a b) -> Fold m (k, a) (Map k b)
1017demux = demuxWith id
1019demux = demuxWith id
1021+{-# INLINE demuxWithDefault_ #-}
1022+demuxWithDefault_ :: (Monad m, Ord k)
1023+ => (a -> (k, a')) -> Map k (Fold m a' b) -> Fold m (k, a') b -> Fold m a ()
1024+demuxWithDefault_ f kv (Fold dstep dinitial dextract) =
1025+ Fold step initial extract
1029+ initFold (Fold s i e) = i >>= \r -> return (Fold s (return r) e)
1031+ mp <- Prelude.mapM initFold kv
1033+ return (Tuple' mp dacc)
1034+ step (Tuple' mp dacc) a
1036+ = case Map.lookup k mp of
1038+ acc <- dstep dacc (k, a')
1039+ return (Tuple' mp acc)
1040+ Just (Fold step' acc _) -> do
1041+ _ <- acc >>= \x -> step' x a'
1042+ return (Tuple' mp dacc)
1043+ extract (Tuple' mp dacc) = do
1044+ void $ dextract dacc
1045+ Prelude.mapM_ (\(Fold _ acc e) -> acc >>= e) mp
1019-- | Split the input stream based on a key field and fold each split using a
1047-- | Split the input stream based on a key field and fold each split using a
1020-- specific fold without collecting the results. Useful for cases like protocol
1048-- specific fold without collecting the results. Useful for cases like protocol
1021-- handlers to handle different type of packets.
1049-- handlers to handle different type of packets.
@@ -1074,6 +1102,11 @@ demuxWith_ f kv = Fold step initial extract
1074demux_ :: (Monad m, Ord k) => Map k (Fold m a ()) -> Fold m (k, a) ()
1102demux_ :: (Monad m, Ord k) => Map k (Fold m a ()) -> Fold m (k, a) ()
1075demux_ = demuxWith_ id
1103demux_ = demuxWith_ id
1105+{-# INLINE demuxDefault_ #-}
1106+demuxDefault_ :: (Monad m, Ord k)
1107+ => Map k (Fold m a ()) -> Fold m (k, a) () -> Fold m (k, a) ()
1108+demuxDefault_ = demuxWithDefault_ id
1077-- TODO If the data is large we may need a map/hashmap in pinned memory instead
1110-- TODO If the data is large we may need a map/hashmap in pinned memory instead
1078-- of a regular Map. That may require a serializable constraint though. We can
1111-- of a regular Map. That may require a serializable constraint though. We can
1079-- have another API for that.
1112-- have another API for that.