@@ -126,6 +126,7 @@ module Streamly.Internal.Data.Unfold
126126where
127127
128128import Control.Exception (Exception, mask_)
129+import Control.Monad ((>=>))
129130import Control.Monad.IO.Class (MonadIO(..))
130131import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
131132import Data.Void (Void)
@@ -173,7 +174,7 @@ lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
173174-- /Internal/
174175{-# INLINE_NORMAL lmapM #-}
175176lmapM :: 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)
177178
178179-- XXX change the signature to the following?
179180-- supply :: a -> Unfold m a b -> Unfold m Void b
@@ -289,7 +290,7 @@ mapM f (Unfold ustep uinject) = Unfold step uinject
289290 case r of
290291 Yield x s -> f x >>= \a -> return $ Yield a s
291292 Skip s -> return $ Skip s
292− Stop -> return $ Stop
293+ Stop -> return Stop
293294
294295{-# INLINE_NORMAL mapMWithInput #-}
295296mapMWithInput :: 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
305306 case r of
306307 Yield x s -> f inp x >>= \a -> return $ Yield a (inp, s)
307308 Skip s -> return $ Skip (inp, s)
308− Stop -> return $ Stop
309+ Stop -> return Stop
309310
310311-------------------------------------------------------------------------------
311312-- Convert streams into unfolds
@@ -437,7 +438,7 @@ replicateM n = Unfold step inject
437438 step (x, i) = return $
438439 if i <= 0
439440 then Stop
440− else Yield x (x, (i - 1))
441+ else Yield x (x, i - 1)
441442
442443-- | Generates an infinite stream repeating the seed.
443444--
@@ -536,7 +537,7 @@ enumerateFromStepIntegral = Unfold step inject
536537 where
537538 inject (from, stride) = from `seq` stride `seq` return (from, stride)
538539 {-# INLINE_LATE step #-}
539− step !(x, stride) = return $ Yield x $! (x + stride, stride)
540+ step (x, stride) = return $ Yield x $! (x + stride, stride)
540541
541542-- We are assuming that "to" is constrained by the type to be within
542543-- max/min bounds.
@@ -850,8 +851,8 @@ gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
850851--
851852{-# INLINE_NORMAL _before #-}
852853_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− (\_ -> return ()) undefined unf
854+_before action = gbracket (\x -> action x >> return x) (fmap Right)
855+ (\_ -> return ()) undefined
855856
856857-- | Run a side effect before the unfold yields its first element.
857858--
@@ -864,8 +865,7 @@ before action (Unfold step1 inject1) = Unfold step inject
864865
865866 inject x = do
866867 _ <- action x
867− st <- inject1 x
868− return st
868+ inject1 x
869869
870870 {-# INLINE_LATE step #-}
871871 step st = do
@@ -932,10 +932,10 @@ afterIO action (Unfold step1 inject1) = Unfold step inject
932932
933933{-# INLINE_NORMAL _onException #-}
934934_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
935−_onException action unf =
935+_onException action =
936936 gbracket return MC.try
937937 (\_ -> return ())
938− (nilM (\(a, (e :: MC.SomeException)) -> action a >> MC.throwM e)) unf
938+ (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
939939
940940-- | Run a side effect whenever the unfold aborts due to an exception.
941941--
@@ -960,9 +960,9 @@ onException action (Unfold step1 inject1) = Unfold step inject
960960
961961{-# INLINE_NORMAL _finally #-}
962962_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
963−_finally action unf =
963+_finally action =
964964 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))
966966
967967-- | Run a side effect whenever the unfold stops normally or aborts due to an
968968-- exception.
@@ -1019,9 +1019,9 @@ finallyIO action (Unfold step1 inject1) = Unfold step inject
10191019{-# INLINE_NORMAL _bracket #-}
10201020_bracket :: MonadCatch m
10211021 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1022−_bracket bef aft unf =
1023− gbracket bef MC.try aft (nilM (\(a, (e :: MC.SomeException)) -> aft a >>
1024− MC.throwM e)) unf
1022+_bracket bef aft =
1023+ gbracket bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
1024+ MC.throwM e))
10251025
10261026-- | @bracket before after between@ runs the @before@ action and then unfolds
10271027-- 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
10941094{-# INLINE_NORMAL handle #-}
10951095handle :: (MonadCatch m, Exception e)
10961096 => Unfold m e b -> Unfold m a b -> Unfold m a b
1097−handle exc unf =
1098− gbracket return MC.try (\_ -> return ()) (discardFirst exc) unf
1097+handle exc =
1098+ gbracket return MC.try (\_ -> return ()) (discardFirst exc)