on a87bf85 help sign in
47 lines 1.5 kB history raw
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--
9module Streamly.Data.Fold.Prelude
10 ( module Streamly.Data.Fold
11 , toHashMapIO
12 )
13where
15import Control.Monad.IO.Class (MonadIO)
16import Data.HashMap.Strict (HashMap)
17import Data.Hashable (Hashable)
18import Streamly.Data.Fold
19import Streamly.Internal.Data.Fold.Container (toContainerIO)
20import Streamly.Internal.Data.IsMap.HashMap ()
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 #-}
45toHashMapIO :: (MonadIO m, Hashable k, Ord k) =>
46 (a -> k) -> Fold m a b -> Fold m a (HashMap k b)
47toHashMapIO = toContainerIO