on master help sign in
Add a Functor instance for Producer
34770c4Harendra Kumar
src/Streamly/Internal/Data/Producer/Type.hs+22−1
@@ -34,7 +34,7 @@ where
34
34
35import Fusion.Plugin.Types (Fuse(..))
35import Fusion.Plugin.Types (Fuse(..))
36import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
36import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
37−import Prelude hiding (concat)
37+import Prelude hiding (concat, map)
38
38
39------------------------------------------------------------------------------
39------------------------------------------------------------------------------
40-- Type
40-- Type
@@ -112,6 +112,27 @@ translate f g (Producer step inject extract) =
112lmap :: (a -> a) -> Producer m a b -> Producer m a b
112lmap :: (a -> a) -> Producer m a b -> Producer m a b
113lmap f (Producer step inject extract) = Producer step (inject . f) extract
113lmap f (Producer step inject extract) = Producer step (inject . f) extract
114
114
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+
115------------------------------------------------------------------------------
136------------------------------------------------------------------------------
116-- Nesting
137-- Nesting
117------------------------------------------------------------------------------
138------------------------------------------------------------------------------