@@ -283,8 +283,11 @@ module Streamly.Fold
283 -- select the fold. This is useful to demultiplex the input stream.
283 -- select the fold. This is useful to demultiplex the input stream.
284 , partitionByM
284 , partitionByM
285 , partitionBy
285 , partitionBy
287 , classify
289 , classify
289 -- * Unzipping
292 -- * Unzipping
@@ -1804,6 +1807,18 @@ partitionBy :: Monad m
1804 => (a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
1807 => (a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
1805partitionBy f = partitionByM (return . f)
1808partitionBy f = partitionByM (return . f)
1810+-- | Compose two folds such that the combined fold accepts a stream of 'Either'
1811+-- and routes the 'Left' values to the first fold and 'Right' values to the
1814+-- > partition = partitionBy id
1817+{-# INLINE partition #-}
1818+partition :: Monad m
1819+ => Fold m b x -> Fold m c y -> Fold m (Either b c) (x, y)
1820+partition = partitionBy id
1808-- | Send one item to each fold in a round-robin fashion. This is the consumer
1823-- | Send one item to each fold in a round-robin fashion. This is the consumer
1809-- side dual of producer side 'mergeN' operation.
1824-- side dual of producer side 'mergeN' operation.
@@ -1847,8 +1862,8 @@ partitionBy f = partitionByM (return . f)
1849-- @since 0.7.0
1864-- @since 0.7.0
1850−demux_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1865+demuxWith_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1851−demux_ f kv = Fold step initial extract
1866+demuxWith_ f kv = Fold step initial extract
@@ -1863,6 +1878,15 @@ demux_ f kv = Fold step initial extract
1863 initial' >>= \x -> step' x a >>= extract'
1878 initial' >>= \x -> step' x a >>= extract'
1864 extract = return
1879 extract = return
1881+-- | Given a stream of key value pairs and a map from keys to folds, fold the
1882+-- values for each key using the corresponding folds.
1884+-- See 'demuxWith_' for details.
1887+demux_ :: (Monad m, Ord k) => Map k (Fold m a ()) -> Fold m (k, a) ()
1888+demux_ fs = demuxWith_ fst (Map.map (lmap snd) fs)
1866-- XXX instead of a Map we could yield the results as a pure stream as they
1890-- XXX instead of a Map we could yield the results as a pure stream as they
1867-- complete. We could then concatMap the fold results to implement the
1891-- complete. We could then concatMap the fold results to implement the
1868-- windowing combinators.
1892-- windowing combinators.
@@ -1878,8 +1902,8 @@ demux_ f kv = Fold step initial extract
1880-- @since 0.7.0
1904-- @since 0.7.0
1881−classify :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1905+classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1882−classify f (Fold step initial extract) = Fold step' initial' extract'
1906+classifyWith f (Fold step initial extract) = Fold step' initial' extract'
@@ -1896,6 +1920,24 @@ classify f (Fold step initial extract) = Fold step' initial' extract'
1896 return $ Map.insert k r kv
1920 return $ Map.insert k r kv
1897 extract' = mapM extract
1921 extract' = mapM extract
1923+-- | Given an input stream of key value pairs and a fold for values, fold all
1924+-- the values belonging to each key. Useful for map/reduce, bucketizing the
1925+-- input in different bins or for generating histograms.
1928+-- > let input = S.fromList [(\"ONE",1),(\"ONE",1.1),(\"TWO",2), (\"TWO",2.2)]
1929+-- in FL.foldl' (FL.classify FL.toListRev) input
1930+-- fromList [(\"ONE",[1.1,1.0]),(\"TWO",[2.2,2.0])]
1935+-- > classify fld = classifyWith fst (lmap snd fld)
1938+classify :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
1939+classify fld = classifyWith fst (lmap snd fld)
1899------------------------------------------------------------------------------
1941------------------------------------------------------------------------------
1900-- Unzipping
1942-- Unzipping
1901------------------------------------------------------------------------------
1943------------------------------------------------------------------------------