on master help sign in
Expose toHashMapIO from the Data.Fold.Prelude module in "streamly" (#2380)
511043bRanjeet Ranjan
benchmark/Streamly/Benchmark/Data/Fold.hs+2−1
@@ -31,6 +31,7 @@ import Streamly.Internal.Data.IsMap.HashMap ()
31import Streamly.Internal.Data.Array.Mut (MutArray)
31import Streamly.Internal.Data.Array.Mut (MutArray)
32
32
33import qualified Streamly.Internal.Data.Fold as FL
33import qualified Streamly.Internal.Data.Fold as FL
34+import qualified Streamly.Data.Fold.Prelude as Fold
34import qualified Streamly.Internal.Data.Fold.Container as FL
35import qualified Streamly.Internal.Data.Fold.Container as FL
35import qualified Streamly.Internal.Data.Unfold as Unfold
36import qualified Streamly.Internal.Data.Unfold as Unfold
36import qualified Streamly.Internal.Data.Pipe as Pipe
37import qualified Streamly.Internal.Data.Pipe as Pipe
@@ -251,7 +252,7 @@ toIntMapIO f = Stream.fold (FL.toContainerIO f FL.sum)
251{-# INLINE toHashMapIO #-}
252{-# INLINE toHashMapIO #-}
252toHashMapIO :: (MonadIO m, Ord k, Num a, Hashable k) =>
253toHashMapIO :: (MonadIO m, Ord k, Num a, Hashable k) =>
253 (a -> k) -> Stream m a -> m (HashMap k a)
254 (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)
255
256
256-------------------------------------------------------------------------------
257-------------------------------------------------------------------------------
257-- unzip
258-- 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
353 -- Internal modules, listed roughly in bottom up
353 -- Internal modules, listed roughly in bottom up
354 -- dependency order To view dependency graph:
354 -- dependency order To view dependency graph:
355 -- graphmod | dot -Tps > deps.ps
355 -- graphmod | dot -Tps > deps.ps
356−
357 Streamly.Internal.Data.IsMap.HashMap
356 Streamly.Internal.Data.IsMap.HashMap
358 , Streamly.Internal.Data.Cont
357 , Streamly.Internal.Data.Cont
359 , Streamly.Internal.Data.Stream.MkType
358 , Streamly.Internal.Data.Stream.MkType
@@ -396,6 +395,7 @@ library
396 -- Exposed modules
395 -- Exposed modules
397 , Streamly.Data.Stream.MkType
396 , Streamly.Data.Stream.MkType
398 , Streamly.Data.Stream.Prelude
397 , Streamly.Data.Stream.Prelude
398+ , Streamly.Data.Fold.Prelude
399
399
400 -- Network/IO
400 -- Network/IO
401 , Streamly.Network.Socket
401 , Streamly.Network.Socket