on master help sign in
Expose toHashMapIO from the Data.Fold.Prelude module in "streamly" (#2380)
commit511043b8920a9164305a7d0e205c33284ab726b8 authorRanjeet Ranjan <ranjeetr@composewell.com> dateWed 5 Jul 2023, 00:02 (3 years ago) parent668f2ef
3 files changed+50−2
benchmark/Streamly/Benchmark/Data/Fold.hs+2−1 src/Streamly/Data/Fold/Prelude.hs+47 streamly.cabal+1−1
benchmark/Streamly/Benchmark/Data/Fold.hs+2−1
@@ -31,6 +31,7 @@ import Streamly.Internal.Data.IsMap.HashMap ()
3131import Streamly.Internal.Data.Array.Mut (MutArray)
3232
3333import qualified Streamly.Internal.Data.Fold as FL
34+import qualified Streamly.Data.Fold.Prelude as Fold
3435import qualified Streamly.Internal.Data.Fold.Container as FL
3536import qualified Streamly.Internal.Data.Unfold as Unfold
3637import qualified Streamly.Internal.Data.Pipe as Pipe
@@ -251,7 +252,7 @@ toIntMapIO f = Stream.fold (FL.toContainerIO f FL.sum)
251252{-# INLINE toHashMapIO #-}
252253toHashMapIO :: (MonadIO m, Ord k, Num a, Hashable k) =>
253254 (a -> k) -> Stream m a -> m (HashMap k a)
254−toHashMapIO f = Stream.fold (FL.toContainerIO f FL.sum)
255+toHashMapIO f = Stream.fold (Fold.toHashMapIO f FL.sum)
255256
256257-------------------------------------------------------------------------------
257258-- unzip
src/Streamly/Data/Fold/Prelude.hsnew file+47
@@ -0,0 +1,47 @@
1+-- |
2+-- Module : Streamly.Data.Fold.Prelude
3+-- Copyright : (c) 2021 Composewell Technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : released
7+-- Portability : GHC
8+--
9+module Streamly.Data.Fold.Prelude
10+ ( module Streamly.Data.Fold
11+ , toHashMapIO
12+ )
13+where
14+
15+import Control.Monad.IO.Class (MonadIO)
16+import Data.HashMap.Strict (HashMap)
17+import Data.Hashable (Hashable)
18+import Streamly.Data.Fold
19+import Streamly.Internal.Data.Fold.Container (toContainerIO)
20+import Streamly.Internal.Data.IsMap.HashMap ()
21+
22+-- | Split the input stream based on a hashable component of the key field and
23+-- fold each split using the given fold. Useful for map/reduce, bucketizing
24+-- the input in different bins or for generating histograms.
25+--
26+-- Example:
27+--
28+-- >>> import Data.HashMap.Strict (HashMap, fromList)
29+-- >>> import qualified Streamly.Data.Fold.Prelude as Fold
30+-- >>> import qualified Streamly.Data.Stream as Stream
31+-- >>> import Streamly.Data.Fold.Prelude (toHashMapIO)
32+-- >>> :{
33+-- do
34+-- let input = Stream.fromList [("ONE",1),("ONE",1.1),("TWO",2), ("TWO",2.2)]
35+-- classify = toHashMapIO fst (Fold.lmap snd Fold.toList)
36+-- x <- Stream.fold classify input :: IO (HashMap String [Double])
37+-- let y = fromList [("ONE",[1.0,1.1]),("TWO",[2.0,2.2])]
38+-- return (x == y)
39+-- :}
40+-- True
41+--
42+-- /Pre-release/
43+--
44+{-# INLINE toHashMapIO #-}
45+toHashMapIO :: (MonadIO m, Hashable k, Ord k) =>
46+ (a -> k) -> Fold m a b -> Fold m a (HashMap k b)
47+toHashMapIO = toContainerIO
streamly.cabal+1−1
@@ -353,7 +353,6 @@ library
353353 -- Internal modules, listed roughly in bottom up
354354 -- dependency order To view dependency graph:
355355 -- graphmod | dot -Tps > deps.ps
356−
357356 Streamly.Internal.Data.IsMap.HashMap
358357 , Streamly.Internal.Data.Cont
359358 , Streamly.Internal.Data.Stream.MkType
@@ -396,6 +395,7 @@ library
396395 -- Exposed modules
397396 , Streamly.Data.Stream.MkType
398397 , Streamly.Data.Stream.Prelude
398+ , Streamly.Data.Fold.Prelude
399399
400400 -- Network/IO
401401 , Streamly.Network.Socket