on master help sign in
Make classifyWith work with IsMap
commit1a76a066541ae65fcfab3f856f63c2c71c37bcea authorHarendra Kumar <harendra@composewell.com> dateSat 26 Mar 2022, 18:18 (5 years ago) parentd78715c
2 files changed+16−7
benchmark/Streamly/Benchmark/Data/Fold.hs+9−2 core/src/Streamly/Internal/Data/Fold.hs+7−5
benchmark/Streamly/Benchmark/Data/Fold.hs+9−2
@@ -215,6 +215,11 @@ classifyWith ::
215215 (Monad m, Ord k, Num a) => (a -> k) -> SerialT m a -> m (Map k a)
216216classifyWith f = S.fold (FL.classifyWith f FL.sum)
217217
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+
218223{-# INLINE classifyScanWith #-}
219224classifyScanWith ::
220225 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 =
430435 $ demuxWith (getKey 64) mp
431436 , benchIOSink value "classifyWith (64 buckets) sum"
432437 $ classifyWith (fst . getKey 64)
438+ , benchIOSink value "classifyWithInt (64 buckets) sum"
439+ $ classifyWithInt (fst . getKey 64)
433440 , benchIOSink value "classifyScanWith (64 buckets) sum"
434441 $ classifyScanWith (fst . getKey 64)
435442 , benchIOSink value "classifyMutWith (single bucket) sum"
@@ -438,14 +445,14 @@ o_n_heap_serial value =
438445 $ classifyMutWith (fst . getKey 64)
439446 , benchIOSink value "classifyMutWith (max buckets) sum"
440447 $ classifyMutWith (fst . getKey value)
448+ , benchIOSink value "classifyMutWithInt (64 buckets) sum"
449+ $ classifyMutWithInt (fst . getKey 64)
441450 , benchIOSink value "classifyMutWithHash (single bucket) sum"
442451 $ classifyMutWithHash (fst . getKey 1)
443452 , benchIOSink value "classifyMutWithHash (64 buckets) sum"
444453 $ classifyMutWithHash (fst . getKey 64)
445454 , benchIOSink value "classifyMutWithHash (max buckets) sum"
446455 $ classifyMutWithHash (fst . getKey value)
447− , benchIOSink value "classifyMutWithInt (64 buckets) sum"
448− $ classifyMutWithInt (fst . getKey 64)
449456 ]
450457 ]
451458
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
17521752-- given fold. Useful for map/reduce, bucketizing the input in different bins
17531753-- or for generating histograms.
17541754--
1755+-- >>> import Data.Map.Strict (Map)
17551756-- >>> :{
17561757-- 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])
17581760-- :}
17591761-- fromList [("ONE",[1.0,1.1]),("TWO",[2.0,2.2])]
17601762--
@@ -1770,15 +1772,15 @@ toMap = foldl' (\kv (k, v) -> IsMap.mapInsert k v kv) IsMap.mapEmpty
17701772-- /Pre-release/
17711773--
17721774{-# INLINE classifyWith #-}
1773−classifyWith :: (Monad m, Ord k) =>
1774− (a -> k) -> Fold m a b -> Fold m a (Map k b)
1775+classifyWith :: (Monad m, IsMap f, Traversable f, Ord (Key f)) =>
1776+ (a -> Key f) -> Fold m a b -> Fold m a (f b)
17751777classifyWith f fld =
17761778 let
17771779 classifier = classifyScanWith f fld
1778− getMap Nothing = pure Map.empty
1780+ getMap Nothing = pure IsMap.mapEmpty
17791781 getMap (Just action) = action
17801782 aggregator =
1781− teeWith Map.union
1783+ teeWith IsMap.mapUnion
17821784 (rmapM getMap $ lmap fst last)
17831785 (lmap snd $ catMaybes toMap)
17841786 in postscan classifier aggregator