Skip to content

Commit

Permalink
Expose toHashMapIO from the Data.Fold.Prelude module in "streamly" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan authored Jul 4, 2023
1 parent 668f2ef commit 511043b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
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

0 comments on commit 511043b

Please sign in to comment.