on master help sign in
Add a Functor instance for Unfold
1962f8bHarendra Kumar
src/Streamly/Internal/Data/Unfold.hs+1−13
@@ -159,7 +159,7 @@ import Streamly.Internal.Data.IOFinalizer
159 (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
159 (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
160import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
160import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
161import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
161import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
162−import Streamly.Internal.Data.Unfold.Types (Unfold(..), lmap)
162+import Streamly.Internal.Data.Unfold.Types (Unfold(..), lmap, map)
163import System.Mem (performMajorGC)
163import System.Mem (performMajorGC)
164
164
165import qualified Prelude
165import qualified Prelude
@@ -288,18 +288,6 @@ fold (Unfold ustep inject) (Fold fstep initial extract) a = do
288 Skip s -> go SPEC fs s
288 Skip s -> go SPEC fs s
289 Stop -> extract fs
289 Stop -> extract fs
290
290
291−{-# INLINE_NORMAL map #-}
292−map :: Monad m => (b -> c) -> Unfold m a b -> Unfold m a c
293−map f (Unfold ustep uinject) = Unfold step uinject
294− where
295− {-# INLINE_LATE step #-}
296− step st = do
297− r <- ustep st
298− return $ case r of
299− Yield x s -> Yield (f x) s
300− Skip s -> Skip s
301− Stop -> Stop
302−
303{-# INLINE_NORMAL mapM #-}
291{-# INLINE_NORMAL mapM #-}
304mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
292mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
305mapM f (Unfold ustep uinject) = Unfold step uinject
293mapM f (Unfold ustep uinject) = Unfold step uinject
src/Streamly/Internal/Data/Unfold/Types.hs+24
@@ -9,6 +9,7 @@
9module Streamly.Internal.Data.Unfold.Types
9module Streamly.Internal.Data.Unfold.Types
10 ( Unfold (..)
10 ( Unfold (..)
11 , lmap
11 , lmap
12+ , map
12 )
13 )
13where
14where
14
15
@@ -16,6 +17,8 @@ where
16
17
17import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
18import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
18
19
20+import Prelude hiding (map)
21+
19------------------------------------------------------------------------------
22------------------------------------------------------------------------------
20-- Monadic Unfolds
23-- Monadic Unfolds
21------------------------------------------------------------------------------
24------------------------------------------------------------------------------
@@ -39,3 +42,24 @@ data Unfold m a b =
39{-# INLINE_NORMAL lmap #-}
42{-# INLINE_NORMAL lmap #-}
40lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
43lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
41lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
44lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
45+
46+------------------------------------------------------------------------------
47+-- Functor
48+------------------------------------------------------------------------------
49+
50+-- | Map a function on the output of the unfold (the type @b@).
51+--
52+-- /Internal/
53+{-# INLINE_NORMAL map #-}
54+map :: Functor m => (b -> c) -> Unfold m a b -> Unfold m a c
55+map f (Unfold ustep uinject) = Unfold step uinject
56+
57+ where
58+
59+ {-# INLINE_LATE step #-}
60+ step st = fmap (fmap f) (ustep st)
61+
62+-- | Maps a function on the output of the unfold (the type @b@).
63+instance Functor m => Functor (Unfold m a) where
64+ {-# INLINE fmap #-}
65+ fmap = map