-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose toHashMapIO from the Data.Fold.Prelude module in "streamly" (#…
- Loading branch information
1 parent
668f2ef
commit 511043b
Showing
3 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
-- | | ||
-- Module : Streamly.Data.Fold.Prelude | ||
-- Copyright : (c) 2021 Composewell Technologies | ||
-- License : BSD-3-Clause | ||
-- Maintainer : [email protected] | ||
-- Stability : released | ||
-- Portability : GHC | ||
-- | ||
module Streamly.Data.Fold.Prelude | ||
( module Streamly.Data.Fold | ||
, toHashMapIO | ||
) | ||
where | ||
|
||
import Control.Monad.IO.Class (MonadIO) | ||
import Data.HashMap.Strict (HashMap) | ||
import Data.Hashable (Hashable) | ||
import Streamly.Data.Fold | ||
import Streamly.Internal.Data.Fold.Container (toContainerIO) | ||
import Streamly.Internal.Data.IsMap.HashMap () | ||
|
||
-- | Split the input stream based on a hashable component of the key field and | ||
-- fold each split using the given fold. Useful for map/reduce, bucketizing | ||
-- the input in different bins or for generating histograms. | ||
-- | ||
-- Example: | ||
-- | ||
-- >>> import Data.HashMap.Strict (HashMap, fromList) | ||
-- >>> import qualified Streamly.Data.Fold.Prelude as Fold | ||
-- >>> import qualified Streamly.Data.Stream as Stream | ||
-- >>> import Streamly.Data.Fold.Prelude (toHashMapIO) | ||
-- >>> :{ | ||
-- do | ||
-- let input = Stream.fromList [("ONE",1),("ONE",1.1),("TWO",2), ("TWO",2.2)] | ||
-- classify = toHashMapIO fst (Fold.lmap snd Fold.toList) | ||
-- x <- Stream.fold classify input :: IO (HashMap String [Double]) | ||
-- let y = fromList [("ONE",[1.0,1.1]),("TWO",[2.0,2.2])] | ||
-- return (x == y) | ||
-- :} | ||
-- True | ||
-- | ||
-- /Pre-release/ | ||
-- | ||
{-# INLINE toHashMapIO #-} | ||
toHashMapIO :: (MonadIO m, Hashable k, Ord k) => | ||
(a -> k) -> Fold m a b -> Fold m a (HashMap k b) | ||
toHashMapIO = toContainerIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters