Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose toHashMapIO from the Data.Fold.Prelude module in "streamly" #2380

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion benchmark/Streamly/Benchmark/Data/Fold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Streamly.Internal.Data.IsMap.HashMap ()
import Streamly.Internal.Data.Array.Mut (MutArray)

import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Data.Fold.Prelude as Fold
import qualified Streamly.Internal.Data.Fold.Container as FL
import qualified Streamly.Internal.Data.Unfold as Unfold
import qualified Streamly.Internal.Data.Pipe as Pipe
Expand Down Expand Up @@ -251,7 +252,7 @@ toIntMapIO f = Stream.fold (FL.toContainerIO f FL.sum)
{-# INLINE toHashMapIO #-}
toHashMapIO :: (MonadIO m, Ord k, Num a, Hashable k) =>
(a -> k) -> Stream m a -> m (HashMap k a)
toHashMapIO f = Stream.fold (FL.toContainerIO f FL.sum)
toHashMapIO f = Stream.fold (Fold.toHashMapIO f FL.sum)

-------------------------------------------------------------------------------
-- unzip
Expand Down
47 changes: 47 additions & 0 deletions src/Streamly/Data/Fold/Prelude.hs
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
2 changes: 1 addition & 1 deletion streamly.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ library
-- Internal modules, listed roughly in bottom up
-- dependency order To view dependency graph:
-- graphmod | dot -Tps > deps.ps

Streamly.Internal.Data.IsMap.HashMap
, Streamly.Internal.Data.Cont
, Streamly.Internal.Data.Stream.MkType
Expand Down Expand Up @@ -396,6 +395,7 @@ library
-- Exposed modules
, Streamly.Data.Stream.MkType
, Streamly.Data.Stream.Prelude
, Streamly.Data.Fold.Prelude

-- Network/IO
, Streamly.Network.Socket
Expand Down
Loading