on master help sign in
Add classifyWith, demuxWith_, partition combinators
commit2298874d7faab402344bd7344b7b7cfec5265a4d authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 21 May 2019, 02:53 (7 years ago) parent1a7932b
1 file changed+46−4
src/Streamly/Fold.hs+46−4
src/Streamly/Fold.hs+46−4
@@ -283,8 +283,11 @@ module Streamly.Fold
283283 -- select the fold. This is useful to demultiplex the input stream.
284284 , partitionByM
285285 , partitionBy
286+ , partition
286287 , demux_
288+ , demuxWith_
287289 , classify
290+ , classifyWith
288291
289292 -- * Unzipping
290293 , unzip
@@ -1804,6 +1807,18 @@ partitionBy :: Monad m
18041807 => (a -> Either b c) -> Fold m b x -> Fold m c y -> Fold m a (x, y)
18051808partitionBy f = partitionByM (return . f)
18061809
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
1812+-- second fold.
1813+--
1814+-- > partition = partitionBy id
1815+--
1816+-- @since 0.7.0
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
1821+
18071822{-
18081823-- | Send one item to each fold in a round-robin fashion. This is the consumer
18091824-- side dual of producer side 'mergeN' operation.
@@ -1847,8 +1862,8 @@ partitionBy f = partitionByM (return . f)
18471862-- @
18481863--
18491864-- @since 0.7.0
1850−demux_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1851−demux_ f kv = Fold step initial extract
1865+demuxWith_ :: (Monad m, Ord k) => (a -> k) -> Map k (Fold m a ()) -> Fold m a ()
1866+demuxWith_ f kv = Fold step initial extract
18521867
18531868 where
18541869
@@ -1863,6 +1878,15 @@ demux_ f kv = Fold step initial extract
18631878 initial' >>= \x -> step' x a >>= extract'
18641879 extract = return
18651880
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.
1883+--
1884+-- See 'demuxWith_' for details.
1885+--
1886+-- @since 0.7.0
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)
1889+
18661890-- XXX instead of a Map we could yield the results as a pure stream as they
18671891-- complete. We could then concatMap the fold results to implement the
18681892-- windowing combinators.
@@ -1878,8 +1902,8 @@ demux_ f kv = Fold step initial extract
18781902-- @
18791903--
18801904-- @since 0.7.0
1881−classify :: (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'
1905+classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1906+classifyWith f (Fold step initial extract) = Fold step' initial' extract'
18831907
18841908 where
18851909
@@ -1896,6 +1920,24 @@ classify f (Fold step initial extract) = Fold step' initial' extract'
18961920 return $ Map.insert k r kv
18971921 extract' = mapM extract
18981922
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.
1926+--
1927+-- @
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])]
1931+-- @
1932+--
1933+-- Same as:
1934+--
1935+-- > classify fld = classifyWith fst (lmap snd fld)
1936+--
1937+-- @since 0.7.0
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)
1940+
18991941------------------------------------------------------------------------------
19001942-- Unzipping
19011943------------------------------------------------------------------------------