on master help sign in
Make classifyWith work with IsMap
1a76a06Harendra Kumar
benchmark/Streamly/Benchmark/Data/Fold.hs+9−2
@@ -215,6 +215,11 @@ classifyWith ::
215 (Monad m, Ord k, Num a) => (a -> k) -> SerialT m a -> m (Map k a)
215 (Monad m, Ord k, Num a) => (a -> k) -> SerialT m a -> m (Map k a)
216classifyWith f = S.fold (FL.classifyWith f FL.sum)
216classifyWith f = S.fold (FL.classifyWith f FL.sum)
217
217
218+{-# INLINE classifyWithInt #-}
219+classifyWithInt ::
220+ (MonadIO m, Num a) => (a -> Int) -> SerialT m a -> m (IntMap a)
221+classifyWithInt f = S.fold (FL.classifyWith f FL.sum)
222+
218{-# INLINE classifyScanWith #-}
223{-# INLINE classifyScanWith #-}
219classifyScanWith ::
224classifyScanWith ::
220 forall m k a. (Monad m, Ord k, Num a) => (a -> k) -> SerialT m a -> m ()
225 forall m k a. (Monad m, Ord k, Num a) => (a -> k) -> SerialT m a -> m ()
@@ -430,6 +435,8 @@ o_n_heap_serial value =
430 $ demuxWith (getKey 64) mp
435 $ demuxWith (getKey 64) mp
431 , benchIOSink value "classifyWith (64 buckets) sum"
436 , benchIOSink value "classifyWith (64 buckets) sum"
432 $ classifyWith (fst . getKey 64)
437 $ classifyWith (fst . getKey 64)
438+ , benchIOSink value "classifyWithInt (64 buckets) sum"
439+ $ classifyWithInt (fst . getKey 64)
433 , benchIOSink value "classifyScanWith (64 buckets) sum"
440 , benchIOSink value "classifyScanWith (64 buckets) sum"
434 $ classifyScanWith (fst . getKey 64)
441 $ classifyScanWith (fst . getKey 64)
435 , benchIOSink value "classifyMutWith (single bucket) sum"
442 , benchIOSink value "classifyMutWith (single bucket) sum"
@@ -438,14 +445,14 @@ o_n_heap_serial value =
438 $ classifyMutWith (fst . getKey 64)
445 $ classifyMutWith (fst . getKey 64)
439 , benchIOSink value "classifyMutWith (max buckets) sum"
446 , benchIOSink value "classifyMutWith (max buckets) sum"
440 $ classifyMutWith (fst . getKey value)
447 $ classifyMutWith (fst . getKey value)
448+ , benchIOSink value "classifyMutWithInt (64 buckets) sum"
449+ $ classifyMutWithInt (fst . getKey 64)
441 , benchIOSink value "classifyMutWithHash (single bucket) sum"
450 , benchIOSink value "classifyMutWithHash (single bucket) sum"
442 $ classifyMutWithHash (fst . getKey 1)
451 $ classifyMutWithHash (fst . getKey 1)
443 , benchIOSink value "classifyMutWithHash (64 buckets) sum"
452 , benchIOSink value "classifyMutWithHash (64 buckets) sum"
444 $ classifyMutWithHash (fst . getKey 64)
453 $ classifyMutWithHash (fst . getKey 64)
445 , benchIOSink value "classifyMutWithHash (max buckets) sum"
454 , benchIOSink value "classifyMutWithHash (max buckets) sum"
446 $ classifyMutWithHash (fst . getKey value)
455 $ classifyMutWithHash (fst . getKey value)
447− , benchIOSink value "classifyMutWithInt (64 buckets) sum"
448− $ classifyMutWithInt (fst . getKey 64)
449 ]
456 ]
450 ]
457 ]
451
458
core/src/Streamly/Internal/Data/Fold.hs+7−5
@@ -1752,9 +1752,11 @@ toMap = foldl' (\kv (k, v) -> IsMap.mapInsert k v kv) IsMap.mapEmpty
1752-- given fold. Useful for map/reduce, bucketizing the input in different bins
1752-- given fold. Useful for map/reduce, bucketizing the input in different bins
1753-- or for generating histograms.
1753-- or for generating histograms.
1754--
1754--
1755+-- >>> import Data.Map.Strict (Map)
1755-- >>> :{
1756-- >>> :{
1756-- let input = Stream.fromList [("ONE",1),("ONE",1.1),("TWO",2), ("TWO",2.2)]
1757-- let input = Stream.fromList [("ONE",1),("ONE",1.1),("TWO",2), ("TWO",2.2)]
1757−-- in Stream.fold (Fold.classifyWith fst (Fold.lmap snd Fold.toList)) input
1758+-- classify = Fold.classifyWith fst (Fold.lmap snd Fold.toList)
1759+-- in Stream.fold classify input :: IO (Map String [Double])
1758-- :}
1760-- :}
1759-- fromList [("ONE",[1.0,1.1]),("TWO",[2.0,2.2])]
1761-- fromList [("ONE",[1.0,1.1]),("TWO",[2.0,2.2])]
1760--
1762--
@@ -1770,15 +1772,15 @@ toMap = foldl' (\kv (k, v) -> IsMap.mapInsert k v kv) IsMap.mapEmpty
1770-- /Pre-release/
1772-- /Pre-release/
1771--
1773--
1772{-# INLINE classifyWith #-}
1774{-# INLINE classifyWith #-}
1773−classifyWith :: (Monad m, Ord k) =>
1775+classifyWith :: (Monad m, IsMap f, Traversable f, Ord (Key f)) =>
1774− (a -> k) -> Fold m a b -> Fold m a (Map k b)
1776+ (a -> Key f) -> Fold m a b -> Fold m a (f b)
1775classifyWith f fld =
1777classifyWith f fld =
1776 let
1778 let
1777 classifier = classifyScanWith f fld
1779 classifier = classifyScanWith f fld
1778− getMap Nothing = pure Map.empty
1780+ getMap Nothing = pure IsMap.mapEmpty
1779 getMap (Just action) = action
1781 getMap (Just action) = action
1780 aggregator =
1782 aggregator =
1781− teeWith Map.union
1783+ teeWith IsMap.mapUnion
1782 (rmapM getMap $ lmap fst last)
1784 (rmapM getMap $ lmap fst last)
1783 (lmap snd $ catMaybes toMap)
1785 (lmap snd $ catMaybes toMap)
1784 in postscan classifier aggregator
1786 in postscan classifier aggregator