on master help sign in
Add a Functor instance for Producer
commit34770c44a24caea803855719ed2107b305a1b348 authorHarendra Kumar <harendra@composewell.com> dateSun 21 Feb 2021, 02:39 (6 years ago) parent02d18f0
1 file changed+22−1
src/Streamly/Internal/Data/Producer/Type.hs+22−1
src/Streamly/Internal/Data/Producer/Type.hs+22−1
@@ -34,7 +34,7 @@ where
3434
3535import Fusion.Plugin.Types (Fuse(..))
3636import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
37−import Prelude hiding (concat)
37+import Prelude hiding (concat, map)
3838
3939------------------------------------------------------------------------------
4040-- Type
@@ -112,6 +112,27 @@ translate f g (Producer step inject extract) =
112112lmap :: (a -> a) -> Producer m a b -> Producer m a b
113113lmap f (Producer step inject extract) = Producer step (inject . f) extract
114114
115+------------------------------------------------------------------------------
116+-- Functor
117+------------------------------------------------------------------------------
118+
119+-- | Map a function on the output of the producer (the type @b@).
120+--
121+-- /Internal/
122+{-# INLINE_NORMAL map #-}
123+map :: Functor m => (b -> c) -> Producer m a b -> Producer m a c
124+map f (Producer ustep uinject uextract) = Producer step uinject uextract
125+
126+ where
127+
128+ {-# INLINE_LATE step #-}
129+ step st = fmap (fmap f) (ustep st)
130+
131+-- | Maps a function on the output of the producer (the type @b@).
132+instance Functor m => Functor (Producer m a) where
133+ {-# INLINE fmap #-}
134+ fmap = map
135+
115136------------------------------------------------------------------------------
116137-- Nesting
117138------------------------------------------------------------------------------