on master help sign in
Add rmapM to Consumer module
2d9654fHarendra Kumar
src/Streamly/Internal/Data/Consumer/Type.hs+19−1
@@ -25,6 +25,7 @@ module Streamly.Internal.Data.Consumer.Type
25
25
26 -- Combinators
26 -- Combinators
27 , lmapM
27 , lmapM
28+ , rmapM
28
29
29 -- Consumers
30 -- Consumers
30 , drainBy
31 , drainBy
@@ -32,8 +33,9 @@ module Streamly.Internal.Data.Consumer.Type
32 )
33 )
33where
34where
34
35
36+import Control.Monad ((>=>))
35import Fusion.Plugin.Types (Fuse(..))
37import Fusion.Plugin.Types (Fuse(..))
36−import Streamly.Internal.Data.Fold.Step (Step(..))
38+import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep)
37
39
38import Prelude hiding (take)
40import Prelude hiding (take)
39
41
@@ -64,6 +66,22 @@ lmapM f (Consumer step inject extract) = Consumer step1 inject extract
64
66
65 step1 x a = f a >>= step x
67 step1 x a = f a >>= step x
66
68
69+------------------------------------------------------------------------------
70+-- Mapping on the output
71+------------------------------------------------------------------------------
72+
73+-- | Map a monadic function on the output of a fold.
74+--
75+-- /Internal/
76+{-# INLINE rmapM #-}
77+rmapM :: Monad m => (b -> m c) -> Consumer m x a b -> Consumer m x a c
78+rmapM f (Consumer step inject extract) = Consumer step1 inject1 (extract >=> f)
79+
80+ where
81+
82+ inject1 x = inject x >>= mapMStep f
83+ step1 s a = step s a >>= mapMStep f
84+
67------------------------------------------------------------------------------
85------------------------------------------------------------------------------
68-- Consumers
86-- Consumers
69------------------------------------------------------------------------------
87------------------------------------------------------------------------------