on master help sign in
Add a Functor instance for Unfold
commit1962f8b672915f17ceafc2297b3d854c0708ec03 authorHarendra Kumar <harendra@composewell.com> dateWed 17 Feb 2021, 19:26 (6 years ago) parent1a51cab
2 files changed+25−13
src/Streamly/Internal/Data/Unfold.hs+1−13 src/Streamly/Internal/Data/Unfold/Types.hs+24
src/Streamly/Internal/Data/Unfold.hs+1−13
@@ -159,7 +159,7 @@ import Streamly.Internal.Data.IOFinalizer
159159 (newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
160160import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
161161import 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)
163163import System.Mem (performMajorGC)
164164
165165import qualified Prelude
@@ -288,18 +288,6 @@ fold (Unfold ustep inject) (Fold fstep initial extract) a = do
288288 Skip s -> go SPEC fs s
289289 Stop -> extract fs
290290
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−
303291{-# INLINE_NORMAL mapM #-}
304292mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
305293mapM f (Unfold ustep uinject) = Unfold step uinject
src/Streamly/Internal/Data/Unfold/Types.hs+24
@@ -9,6 +9,7 @@
99module Streamly.Internal.Data.Unfold.Types
1010 ( Unfold (..)
1111 , lmap
12+ , map
1213 )
1314where
1415
@@ -16,6 +17,8 @@ where
1617
1718import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
1819
20+import Prelude hiding (map)
21+
1922------------------------------------------------------------------------------
2023-- Monadic Unfolds
2124------------------------------------------------------------------------------
@@ -39,3 +42,24 @@ data Unfold m a b =
3942{-# INLINE_NORMAL lmap #-}
4043lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
4144lmap 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