@@ -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