@@ -126,6 +126,7 @@ module Streamly.Internal.Data.Unfold
128import Control.Exception (Exception, mask_)
128import Control.Exception (Exception, mask_)
129+import Control.Monad ((>=>))
129import Control.Monad.IO.Class (MonadIO(..))
130import Control.Monad.IO.Class (MonadIO(..))
130import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
131import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
131import Data.Void (Void)
132import Data.Void (Void)
@@ -173,7 +174,7 @@ lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
173-- /Internal/
174-- /Internal/
174{-# INLINE_NORMAL lmapM #-}
175{-# INLINE_NORMAL lmapM #-}
175lmapM :: Monad m => (a -> m c) -> Unfold m c b -> Unfold m a b
176lmapM :: Monad m => (a -> m c) -> Unfold m c b -> Unfold m a b
176−lmapM f (Unfold ustep uinject) = Unfold ustep (\x -> f x >>= uinject)
177+lmapM f (Unfold ustep uinject) = Unfold ustep (f >=> uinject)
178-- XXX change the signature to the following?
179-- XXX change the signature to the following?
179-- supply :: a -> Unfold m a b -> Unfold m Void b
180-- supply :: a -> Unfold m a b -> Unfold m Void b
@@ -289,7 +290,7 @@ mapM f (Unfold ustep uinject) = Unfold step uinject
289 case r of
290 case r of
290 Yield x s -> f x >>= \a -> return $ Yield a s
291 Yield x s -> f x >>= \a -> return $ Yield a s
291 Skip s -> return $ Skip s
292 Skip s -> return $ Skip s
292− Stop -> return $ Stop
293+ Stop -> return Stop
294{-# INLINE_NORMAL mapMWithInput #-}
295{-# INLINE_NORMAL mapMWithInput #-}
295mapMWithInput :: Monad m => (a -> b -> m c) -> Unfold m a b -> Unfold m a c
296mapMWithInput :: Monad m => (a -> b -> m c) -> Unfold m a b -> Unfold m a c
@@ -305,7 +306,7 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
305 case r of
306 case r of
306 Yield x s -> f inp x >>= \a -> return $ Yield a (inp, s)
307 Yield x s -> f inp x >>= \a -> return $ Yield a (inp, s)
307 Skip s -> return $ Skip (inp, s)
308 Skip s -> return $ Skip (inp, s)
308− Stop -> return $ Stop
309+ Stop -> return Stop
310-------------------------------------------------------------------------------
311-------------------------------------------------------------------------------
311-- Convert streams into unfolds
312-- Convert streams into unfolds
@@ -437,7 +438,7 @@ replicateM n = Unfold step inject
437 step (x, i) = return $
438 step (x, i) = return $
438 if i <= 0
439 if i <= 0
439 then Stop
440 then Stop
440− else Yield x (x, (i - 1))
441+ else Yield x (x, i - 1)
442-- | Generates an infinite stream repeating the seed.
443-- | Generates an infinite stream repeating the seed.
@@ -536,7 +537,7 @@ enumerateFromStepIntegral = Unfold step inject
537 inject (from, stride) = from `seq` stride `seq` return (from, stride)
538 inject (from, stride) = from `seq` stride `seq` return (from, stride)
538 {-# INLINE_LATE step #-}
539 {-# INLINE_LATE step #-}
539− step !(x, stride) = return $ Yield x $! (x + stride, stride)
540+ step (x, stride) = return $ Yield x $! (x + stride, stride)
541-- We are assuming that "to" is constrained by the type to be within
542-- We are assuming that "to" is constrained by the type to be within
542-- max/min bounds.
543-- max/min bounds.
@@ -850,8 +851,8 @@ gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
851{-# INLINE_NORMAL _before #-}
852{-# INLINE_NORMAL _before #-}
852_before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
853_before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
853−_before action unf = gbracket (\x -> action x >> return x) (fmap Right)
854+_before action = gbracket (\x -> action x >> return x) (fmap Right)
854− (\_ -> return ()) undefined unf
855+ (\_ -> return ()) undefined
856-- | Run a side effect before the unfold yields its first element.
857-- | Run a side effect before the unfold yields its first element.
@@ -864,8 +865,7 @@ before action (Unfold step1 inject1) = Unfold step inject
865 inject x = do
866 inject x = do
866 _ <- action x
867 _ <- action x
867− st <- inject1 x
868+ inject1 x
870 {-# INLINE_LATE step #-}
870 {-# INLINE_LATE step #-}
871 step st = do
871 step st = do
@@ -932,10 +932,10 @@ afterIO action (Unfold step1 inject1) = Unfold step inject
933{-# INLINE_NORMAL _onException #-}
933{-# INLINE_NORMAL _onException #-}
934_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
934_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
935−_onException action unf =
935+_onException action =
936 gbracket return MC.try
936 gbracket return MC.try
937 (\_ -> return ())
937 (\_ -> return ())
938− (nilM (\(a, (e :: MC.SomeException)) -> action a >> MC.throwM e)) unf
938+ (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
940-- | Run a side effect whenever the unfold aborts due to an exception.
940-- | Run a side effect whenever the unfold aborts due to an exception.
@@ -960,9 +960,9 @@ onException action (Unfold step1 inject1) = Unfold step inject
961{-# INLINE_NORMAL _finally #-}
961{-# INLINE_NORMAL _finally #-}
962_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
962_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
963−_finally action unf =
963+_finally action =
964 gbracket return MC.try action
964 gbracket return MC.try action
965− (nilM (\(a, (e :: MC.SomeException)) -> action a >> MC.throwM e)) unf
965+ (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
967-- | Run a side effect whenever the unfold stops normally or aborts due to an
967-- | Run a side effect whenever the unfold stops normally or aborts due to an
968-- exception.
968-- exception.
@@ -1019,9 +1019,9 @@ finallyIO action (Unfold step1 inject1) = Unfold step inject
1019{-# INLINE_NORMAL _bracket #-}
1019{-# INLINE_NORMAL _bracket #-}
1020_bracket :: MonadCatch m
1020_bracket :: MonadCatch m
1021 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1021 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1022−_bracket bef aft unf =
1022+_bracket bef aft =
1023− gbracket bef MC.try aft (nilM (\(a, (e :: MC.SomeException)) -> aft a >>
1023+ gbracket bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
1024− MC.throwM e)) unf
1024+ MC.throwM e))
1026-- | @bracket before after between@ runs the @before@ action and then unfolds
1026-- | @bracket before after between@ runs the @before@ action and then unfolds
1027-- its output using the @between@ unfold. When the @between@ unfold is done or
1027-- its output using the @between@ unfold. When the @between@ unfold is done or
@@ -1094,5 +1094,5 @@ bracketIO bef aft (Unfold step1 inject1) = Unfold step inject
1094{-# INLINE_NORMAL handle #-}
1094{-# INLINE_NORMAL handle #-}
1095handle :: (MonadCatch m, Exception e)
1095handle :: (MonadCatch m, Exception e)
1096 => Unfold m e b -> Unfold m a b -> Unfold m a b
1096 => Unfold m e b -> Unfold m a b -> Unfold m a b
1097−handle exc unf =
1097+handle exc =
1098− gbracket return MC.try (\_ -> return ()) (discardFirst exc) unf
1098+ gbracket return MC.try (\_ -> return ()) (discardFirst exc)