on master help sign in
Use Applicative instead of Monad where possible

Only refactoring, no functional change. In Unfold, StreamD and StreamK. Also fix a few hlint issues.

commitd28e2313f1999494e687afa5236330b7c45ed4a6 authorHarendra Kumar <harendra@composewell.com> dateSun 26 Sep 2021, 08:50 (5 years ago) parented2c32f
4 files changed+172−179
.hlint.ignore−1 src/Streamly/Internal/Data/Stream/StreamD/Type.hs+61−57 src/Streamly/Internal/Data/Stream/StreamK/Type.hs+17−18 src/Streamly/Internal/Data/Unfold.hs+94−103
.hlint.ignore−1
@@ -3,7 +3,6 @@ src/Streamly/Internal/Data/Stream/PreludeCommon.hs
33src/Streamly/Internal/Data/Stream/Serial.hs
44src/Streamly/Internal/Data/Stream/Zip.hs
55src/Streamly/Internal/Data/Stream/StreamK/Type.hs
6−src/Streamly/Internal/Data/Stream/StreamD/Type.hs
76src/Streamly/Internal/Data/Pipe/Type.hs
87src/Streamly/Internal/Data/SmallArray/Type.hs
98src/Streamly/Internal/Unicode/Stream.hs
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+61−57
@@ -92,6 +92,7 @@ import Control.Applicative (liftA2)
9292import Control.Monad (when)
9393import Control.Monad.Catch (MonadThrow, throwM)
9494import Control.Monad.Trans.Class (lift, MonadTrans)
95+import Data.Functor (($>))
9596import Data.Functor.Identity (Identity(..))
9697import Fusion.Plugin.Types (Fuse(..))
9798import GHC.Base (build)
@@ -134,22 +135,22 @@ pattern Stream step state <- (unShare -> UnStream step state)
134135
135136-- | An empty 'Stream' with a side effect.
136137{-# INLINE_NORMAL nilM #-}
137−nilM :: Monad m => m b -> Stream m a
138−nilM m = Stream (\_ _ -> m >> return Stop) ()
138+nilM :: Applicative m => m b -> Stream m a
139+nilM m = Stream (\_ _ -> m $> Stop) ()
139140
140141{-# INLINE_NORMAL consM #-}
141−consM :: Monad m => m a -> Stream m a -> Stream m a
142+consM :: Applicative m => m a -> Stream m a -> Stream m a
142143consM m (Stream step state) = Stream step1 Nothing
144+
143145 where
146+
144147 {-# INLINE_LATE step1 #-}
145− step1 _ Nothing = m >>= \x -> return $ Yield x (Just state)
148+ step1 _ Nothing = (`Yield` Just state) <$> m
146149 step1 gst (Just st) = do
147− r <- step gst st
148− return $
149− case r of
150+ (\case
150151 Yield a s -> Yield a (Just s)
151152 Skip s -> Skip (Just s)
152− Stop -> Stop
153+ Stop -> Stop) <$> step gst st
153154
154155-- | Does not fuse, has the same performance as the StreamK version.
155156{-# INLINE_NORMAL uncons #-}
@@ -172,17 +173,18 @@ data UnfoldState s = UnfoldNothing | UnfoldJust s
172173-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
173174--
174175{-# INLINE_NORMAL unfold #-}
175−unfold :: Monad m => Unfold m a b -> a -> Stream m b
176+unfold :: Applicative m => Unfold m a b -> a -> Stream m b
176177unfold (Unfold ustep inject) seed = Stream step UnfoldNothing
177− where
178+
179+ where
180+
178181 {-# INLINE_LATE step #-}
179− step _ UnfoldNothing = inject seed >>= return . Skip . UnfoldJust
182+ step _ UnfoldNothing = Skip . UnfoldJust <$> inject seed
180183 step _ (UnfoldJust st) = do
181− r <- ustep st
182− return $ case r of
184+ (\case
183185 Yield x s -> Yield x (UnfoldJust s)
184186 Skip s -> Skip (UnfoldJust s)
185− Stop -> Stop
187+ Stop -> Stop) <$> ustep st
186188
187189------------------------------------------------------------------------------
188190-- From Values
@@ -199,12 +201,14 @@ fromPure x = Stream (\_ s -> pure $ step undefined s) True
199201
200202-- | Create a singleton 'Stream' from a monadic action.
201203{-# INLINE_NORMAL fromEffect #-}
202−fromEffect :: Monad m => m a -> Stream m a
204+fromEffect :: Applicative m => m a -> Stream m a
203205fromEffect m = Stream step True
204− where
206+
207+ where
208+
205209 {-# INLINE_LATE step #-}
206− step _ True = m >>= \x -> return $ Yield x False
207− step _ False = return Stop
210+ step _ True = (`Yield` False) <$> m
211+ step _ False = pure Stop
208212
209213------------------------------------------------------------------------------
210214-- From Containers
@@ -226,13 +230,13 @@ fromList = Stream step
226230
227231-- | Convert a CPS encoded StreamK to direct style step encoded StreamD
228232{-# INLINE_LATE fromStreamK #-}
229−fromStreamK :: Monad m => K.Stream m a -> Stream m a
233+fromStreamK :: Applicative m => K.Stream m a -> Stream m a
230234fromStreamK = Stream step
231235 where
232236 step gst m1 =
233− let stop = return Stop
234− single a = return $ Yield a K.nil
235− yieldk a r = return $ Yield a r
237+ let stop = pure Stop
238+ single a = pure $ Yield a K.nil
239+ yieldk a r = pure $ Yield a r
236240 in K.foldStreamShared gst yieldk single stop m1
237241
238242-- | Convert a direct style step encoded StreamD to a CPS encoded StreamK
@@ -261,7 +265,7 @@ toStreamK (Stream step state) = go state
261265------------------------------------------------------------------------------
262266
263267{-# INLINE_NORMAL fold #-}
264−fold :: (Monad m) => Fold m a b -> Stream m a -> m b
268+fold :: Monad m => Fold m a b -> Stream m a -> m b
265269fold fld strm = do
266270 (b, _) <- fold_ fld strm
267271 return b
@@ -349,7 +353,7 @@ foldrMx fstep final convert (Stream step state) = convert $ go SPEC state
349353--
350354{-# INLINE_NORMAL foldr #-}
351355foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
352−foldr f z = foldrM (\a b -> liftA2 f (return a) b) (return z)
356+foldr f z = foldrM (liftA2 f . return) (return z)
353357
354358-- this performs horribly, should not be used
355359{-# INLINE_NORMAL foldrS #-}
@@ -409,8 +413,8 @@ foldlMx' fstep begin done (Stream step state) =
409413
410414{-# INLINE foldlx' #-}
411415foldlx' :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
412−foldlx' fstep begin done m =
413− foldlMx' (\b a -> return (fstep b a)) (return begin) (return . done) m
416+foldlx' fstep begin done =
417+ foldlMx' (\b a -> return (fstep b a)) (return begin) (return . done)
414418
415419-- Adapted from the vector package.
416420-- XXX implement in terms of foldlMx'?
@@ -576,17 +580,18 @@ instance Functor m => Functor (Stream m) where
576580
577581-- Adapted from the vector package.
578582{-# INLINE_NORMAL take #-}
579−take :: Monad m => Int -> Stream m a -> Stream m a
583+take :: Applicative m => Int -> Stream m a -> Stream m a
580584take n (Stream step state) = n `seq` Stream step' (state, 0)
581− where
585+
586+ where
587+
582588 {-# INLINE_LATE step' #-}
583589 step' gst (st, i) | i < n = do
584− r <- step gst st
585− return $ case r of
590+ (\case
586591 Yield x s -> Yield x (s, i + 1)
587592 Skip s -> Skip (s, i)
588− Stop -> Stop
589− step' _ (_, _) = return Stop
593+ Stop -> Stop) <$> step gst st
594+ step' _ (_, _) = pure Stop
590595
591596-- Adapted from the vector package.
592597{-# INLINE_NORMAL takeWhileM #-}
@@ -613,17 +618,20 @@ takeWhile f = takeWhileM (return . f)
613618
614619{-# INLINE_NORMAL concatAp #-}
615620concatAp :: Functor f => Stream f (a -> b) -> Stream f a -> Stream f b
616−concatAp (Stream stepa statea) (Stream stepb stateb) = Stream step' (Left statea)
617− where
621+concatAp (Stream stepa statea) (Stream stepb stateb) =
622+ Stream step' (Left statea)
623+
624+ where
625+
618626 {-# INLINE_LATE step' #-}
619627 step' gst (Left st) = fmap
620− (\r -> case r of
628+ (\case
621629 Yield f s -> Skip (Right (f, s, stateb))
622630 Skip s -> Skip (Left s)
623631 Stop -> Stop)
624632 (stepa (adaptState gst) st)
625633 step' gst (Right (f, os, st)) = fmap
626− (\r -> case r of
634+ (\case
627635 Yield a s -> Yield (f a) (Right (f, os, s))
628636 Skip s -> Skip (Right (f,os, s))
629637 Stop -> Skip (Left os))
@@ -639,19 +647,17 @@ apSequence (Stream stepa statea) (Stream stepb stateb) =
639647 {-# INLINE_LATE step #-}
640648 step gst (Left st) =
641649 fmap
642− (\r ->
643− case r of
644− Yield _ s -> Skip (Right (s, stateb))
645− Skip s -> Skip (Left s)
646− Stop -> Stop)
650+ (\case
651+ Yield _ s -> Skip (Right (s, stateb))
652+ Skip s -> Skip (Left s)
653+ Stop -> Stop)
647654 (stepa (adaptState gst) st)
648655 step gst (Right (ostate, st)) =
649656 fmap
650− (\r ->
651− case r of
652− Yield b s -> Yield b (Right (ostate, s))
653− Skip s -> Skip (Right (ostate, s))
654− Stop -> Skip (Left ostate))
657+ (\case
658+ Yield b s -> Yield b (Right (ostate, s))
659+ Skip s -> Skip (Right (ostate, s))
660+ Stop -> Skip (Left ostate))
655661 (stepb gst st)
656662
657663{-# INLINE_NORMAL apDiscardSnd #-}
@@ -664,19 +670,17 @@ apDiscardSnd (Stream stepa statea) (Stream stepb stateb) =
664670 {-# INLINE_LATE step #-}
665671 step gst (Left st) =
666672 fmap
667− (\r ->
668− case r of
669− Yield b s -> Skip (Right (s, stateb, b))
670− Skip s -> Skip (Left s)
671− Stop -> Stop)
673+ (\case
674+ Yield b s -> Skip (Right (s, stateb, b))
675+ Skip s -> Skip (Left s)
676+ Stop -> Stop)
672677 (stepa gst st)
673678 step gst (Right (ostate, st, b)) =
674679 fmap
675− (\r ->
676− case r of
677− Yield _ s -> Yield b (Right (ostate, s, b))
678− Skip s -> Skip (Right (ostate, s, b))
679− Stop -> Skip (Left ostate))
680+ (\case
681+ Yield _ s -> Yield b (Right (ostate, s, b))
682+ Skip s -> Skip (Right (ostate, s, b))
683+ Stop -> Skip (Left ostate))
680684 (stepb (adaptState gst) st)
681685
682686instance Applicative f => Applicative (Stream f) where
@@ -728,7 +732,7 @@ unfoldMany (Unfold istep inject) (Stream ostep ost) =
728732 i <- inject a
729733 i `seq` return (Skip (ConcatMapUInner o' i))
730734 Skip o' -> return $ Skip (ConcatMapUOuter o')
731− Stop -> return $ Stop
735+ Stop -> return Stop
732736
733737 step _ (ConcatMapUInner o i) = do
734738 r <- istep i
src/Streamly/Internal/Data/Stream/StreamK/Type.hs+17−18
@@ -179,7 +179,7 @@ fromYieldK k = mkStream $ \_ _ sng _ -> k sng
179179
180180-- | Add a yield function at the head of the stream.
181181consK :: YieldK m a -> Stream m a -> Stream m a
182−consK k r = mkStream $ \_ yld _ _ -> k (\x -> yld x r)
182+consK k r = mkStream $ \_ yld _ _ -> k (`yld` r)
183183
184184-- XXX Build a stream from a repeating callback function.
185185
@@ -241,8 +241,8 @@ nil = mkStream $ \_ _ _ stp -> stp
241241--
242242-- /Pre-release/
243243{-# INLINE_NORMAL nilM #-}
244−nilM :: Monad m => m b -> Stream m a
245−nilM m = mkStream $ \_ _ _ stp -> m >> stp
244+nilM :: Applicative m => m b -> Stream m a
245+nilM m = mkStream $ \_ _ _ stp -> m *> stp
246246
247247{-# INLINE_NORMAL fromPure #-}
248248fromPure :: a -> Stream m a
@@ -259,8 +259,8 @@ infixr 5 `consM`
259259-- SPECIALIZE in the instance definition.
260260{-# INLINE consM #-}
261261{-# SPECIALIZE consM :: IO a -> Stream IO a -> Stream IO a #-}
262−consM :: (Monad m) => m a -> Stream m a -> Stream m a
263−consM m r = MkStream $ \_ yld _ _ -> m >>= \a -> yld a r
262+consM :: Monad m => m a -> Stream m a -> Stream m a
263+consM m r = MkStream $ \_ yld _ _ -> m >>= (`yld` r)
264264
265265-- XXX specialize to IO?
266266{-# INLINE consMBy #-}
@@ -1270,30 +1270,29 @@ fromFoldableM = Prelude.foldr consM nil
12701270-------------------------------------------------------------------------------
12711271
12721272{-# INLINE uncons #-}
1273−uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
1273+uncons :: Applicative m => Stream m a -> m (Maybe (a, Stream m a))
12741274uncons m =
1275− let stop = return Nothing
1276− single a = return (Just (a, nil))
1277− yieldk a r = return (Just (a, r))
1275+ let stop = pure Nothing
1276+ single a = pure (Just (a, nil))
1277+ yieldk a r = pure (Just (a, r))
12781278 in foldStream defState yieldk single stop m
12791279
12801280{-# INLINE tail #-}
1281−tail :: Monad m => Stream m a -> m (Maybe (Stream m a))
1281+tail :: Applicative m => Stream m a -> m (Maybe (Stream m a))
12821282tail =
1283− let stop = return Nothing
1284− single _ = return $ Just nil
1285− yieldk _ r = return $ Just r
1283+ let stop = pure Nothing
1284+ single _ = pure $ Just nil
1285+ yieldk _ r = pure $ Just r
12861286 in foldStream defState yieldk single stop
12871287
12881288{-# INLINE init #-}
1289−init :: Monad m => Stream m a -> m (Maybe (Stream m a))
1289+init :: Applicative m => Stream m a -> m (Maybe (Stream m a))
12901290init = go1
12911291 where
12921292 go1 m1 = do
1293− r <- uncons m1
1294− case r of
1295− Nothing -> return Nothing
1296− Just (h, t) -> return . Just $ go h t
1293+ (\case
1294+ Nothing -> Nothing
1295+ Just (h, t) -> Just $ go h t) <$> uncons m1
12971296 go p m1 = mkStream $ \_ yld sng stp ->
12981297 let single _ = sng p
12991298 yieldk a x = yld p $ go a x
src/Streamly/Internal/Data/Unfold.hs+94−103
@@ -250,6 +250,7 @@ import Control.Exception (Exception, mask_)
250250import Control.Monad.Catch (MonadCatch)
251251import Control.Monad.IO.Class (MonadIO(..))
252252import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
253+import Data.Functor (($>))
253254import GHC.Types (SPEC(..))
254255import Streamly.Internal.Control.Concurrent (MonadAsync)
255256import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -417,25 +418,20 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
417418--
418419-- /Internal/
419420{-# INLINE_NORMAL either #-}
420−either :: Monad m => Unfold m a b -> Unfold m (Either a a) (Either b b)
421+either :: Applicative m => Unfold m a b -> Unfold m (Either a a) (Either b b)
421422either (Unfold step1 inject1) = Unfold step inject
422423
423424 where
424425
425− inject (Left a) = do
426− r <- inject1 a
427− return (r, Left)
428− inject (Right a) = do
429− r <- inject1 a
430− return (r, Right)
426+ inject (Left a) = (, Left) <$> inject1 a
427+ inject (Right a) = (, Right) <$> inject1 a
431428
432429 {-# INLINE_LATE step #-}
433430 step (st, f) = do
434− r <- step1 st
435− return $ case r of
431+ (\case
436432 Yield x s -> Yield (f x) (s, f)
437433 Skip s -> Skip (s, f)
438− Stop -> Stop
434+ Stop -> Stop) <$> step1 st
439435
440436-- See StreamD.scanlM' for implementing this.
441437--
@@ -452,30 +448,29 @@ scanlM' = undefined
452448-------------------------------------------------------------------------------
453449
454450{-# INLINE_NORMAL fromStreamD #-}
455−fromStreamD :: Monad m => Unfold m (Stream m a) a
456−fromStreamD = Unfold step return
451+fromStreamD :: Applicative m => Unfold m (Stream m a) a
452+fromStreamD = Unfold step pure
453+
457454 where
458455
459456 {-# INLINE_LATE step #-}
460− step (UnStream step1 state1) = do
461− r <- step1 defState state1
462− return $ case r of
457+ step (UnStream step1 state1) =
458+ (\case
463459 Yield x s -> Yield x (Stream step1 s)
464460 Skip s -> Skip (Stream step1 s)
465− Stop -> Stop
461+ Stop -> Stop) <$> step1 defState state1
466462
467463{-# INLINE_NORMAL fromStreamK #-}
468−fromStreamK :: Monad m => Unfold m (K.Stream m a) a
469−fromStreamK = Unfold step return
464+fromStreamK :: Applicative m => Unfold m (K.Stream m a) a
465+fromStreamK = Unfold step pure
470466
471467 where
472468
473469 {-# INLINE_LATE step #-}
474470 step stream = do
475− r <- K.uncons stream
476− return $ case r of
471+ (\case
477472 Just (x, xs) -> Yield x xs
478− Nothing -> Stop
473+ Nothing -> Stop) <$> K.uncons stream
479474
480475-- XXX Using Unfold.fromStreamD seems to be faster (using cross product test
481476-- case) than using fromStream even if it is implemented using fromStreamD.
@@ -486,7 +481,7 @@ fromStreamK = Unfold step return
486481-- /Since: 0.8.0/
487482--
488483{-# INLINE_NORMAL fromStream #-}
489−fromStream :: (IsStream t, Monad m) => Unfold m (t m a) a
484+fromStream :: (IsStream t, Applicative m) => Unfold m (t m a) a
490485fromStream = lmap IsStream.toStream fromStreamK
491486
492487-------------------------------------------------------------------------------
@@ -497,59 +492,61 @@ fromStream = lmap IsStream.toStream fromStreamK
497492-- effect.
498493--
499494{-# INLINE nilM #-}
500−nilM :: Monad m => (a -> m c) -> Unfold m a b
501−nilM f = Unfold step return
495+nilM :: Applicative m => (a -> m c) -> Unfold m a b
496+nilM f = Unfold step pure
497+
502498 where
499+
503500 {-# INLINE_LATE step #-}
504− step x = f x >> return Stop
501+ step x = f x $> Stop
505502
506503-- | Prepend a monadic single element generator function to an 'Unfold'. The
507504-- same seed is used in the action as well as the unfold.
508505--
509506-- /Pre-release/
510507{-# INLINE_NORMAL consM #-}
511−consM :: Monad m => (a -> m b) -> Unfold m a b -> Unfold m a b
508+consM :: Applicative m => (a -> m b) -> Unfold m a b -> Unfold m a b
512509consM action unf = Unfold step inject
513510
514511 where
515512
516− inject = return . Left
513+ inject = pure . Left
517514
518515 {-# INLINE_LATE step #-}
519− step (Left a) =
520− action a >>= \r -> return $ Yield r (Right (D.unfold unf a))
516+ step (Left a) = (`Yield` Right (D.unfold unf a)) <$> action a
521517 step (Right (UnStream step1 st)) = do
522− res <- step1 defState st
523− case res of
524− Yield x s -> return $ Yield x (Right (Stream step1 s))
525− Skip s -> return $ Skip (Right (Stream step1 s))
526− Stop -> return Stop
518+ (\case
519+ Yield x s -> Yield x (Right (Stream step1 s))
520+ Skip s -> Skip (Right (Stream step1 s))
521+ Stop -> Stop) <$> step1 defState st
527522
528523-- | Convert a list of pure values to a 'Stream'
529524--
530525-- /Since: 0.8.0/
531526--
532527{-# INLINE_LATE fromList #-}
533−fromList :: Monad m => Unfold m [a] a
534−fromList = Unfold step inject
535− where
536− inject = return
528+fromList :: Applicative m => Unfold m [a] a
529+fromList = Unfold step pure
530+
531+ where
532+
537533 {-# INLINE_LATE step #-}
538− step (x:xs) = return $ Yield x xs
539− step [] = return Stop
534+ step (x:xs) = pure $ Yield x xs
535+ step [] = pure Stop
540536
541537-- | Convert a list of monadic values to a 'Stream'
542538--
543539-- /Since: 0.8.0/
544540--
545541{-# INLINE_LATE fromListM #-}
546−fromListM :: Monad m => Unfold m [m a] a
547−fromListM = Unfold step inject
548− where
549− inject = return
542+fromListM :: Applicative m => Unfold m [m a] a
543+fromListM = Unfold step pure
544+
545+ where
546+
550547 {-# INLINE_LATE step #-}
551− step (x:xs) = x >>= \r -> return $ Yield r xs
552− step [] = return Stop
548+ step (x:xs) = (`Yield` xs) <$> x
549+ step [] = pure Stop
553550
554551------------------------------------------------------------------------------
555552-- Specialized Generation
@@ -560,31 +557,31 @@ fromListM = Unfold step inject
560557-- /Since: 0.8.0/
561558--
562559{-# INLINE replicateM #-}
563−replicateM :: Monad m => Int -> Unfold m (m a) a
560+replicateM :: Applicative m => Int -> Unfold m (m a) a
564561replicateM n = Unfold step inject
565562
566563 where
567564
568− inject x = return (x, n)
565+ inject action = pure (action, n)
569566
570567 {-# INLINE_LATE step #-}
571− step (x, i) =
568+ step (action, i) =
572569 if i <= 0
573− then return Stop
574− else do
575− x1 <- x
576− return $ Yield x1 (x, i - 1)
570+ then pure Stop
571+ else (\x -> Yield x (action, i - 1)) <$> action
577572
578573-- | Generates an infinite stream repeating the seed.
579574--
580575-- /Since: 0.8.0/
581576--
582577{-# INLINE repeatM #-}
583−repeatM :: Monad m => Unfold m (m a) a
584−repeatM = Unfold step return
578+repeatM :: Applicative m => Unfold m (m a) a
579+repeatM = Unfold step pure
580+
585581 where
582+
586583 {-# INLINE_LATE step #-}
587− step x = x >>= \x1 -> return $ Yield x1 x
584+ step action = (`Yield` action) <$> action
588585
589586-- | Generates an infinite stream starting with the given seed and applying the
590587-- given function repeatedly.
@@ -592,13 +589,13 @@ repeatM = Unfold step return
592589-- /Since: 0.8.0/
593590--
594591{-# INLINE iterateM #-}
595−iterateM :: Monad m => (a -> m a) -> Unfold m (m a) a
592+iterateM :: Applicative m => (a -> m a) -> Unfold m (m a) a
596593iterateM f = Unfold step id
594+
597595 where
596+
598597 {-# INLINE_LATE step #-}
599− step x = do
600− fx <- f x
601− return $ Yield x fx
598+ step x = Yield x <$> f x
602599
603600-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
604601-- starting from the seed.
@@ -610,13 +607,13 @@ iterateM f = Unfold step id
610607-- /Pre-release/
611608--
612609{-# INLINE_NORMAL fromIndicesM #-}
613−fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
614−fromIndicesM gen = Unfold step return
615− where
610+fromIndicesM :: Applicative m => (Int -> m a) -> Unfold m Int a
611+fromIndicesM gen = Unfold step pure
612+
613+ where
614+
616615 {-# INLINE_LATE step #-}
617− step i = do
618− x <- gen i
619− return $ Yield x (i + 1)
616+ step i = (`Yield` (i + 1)) <$> gen i
620617
621618-------------------------------------------------------------------------------
622619-- Filtering
@@ -630,20 +627,20 @@ fromIndicesM gen = Unfold step return
630627-- /Since: 0.8.0/
631628--
632629{-# INLINE_NORMAL take #-}
633−take :: Monad m => Int -> Unfold m a b -> Unfold m a b
630+take :: Applicative m => Int -> Unfold m a b -> Unfold m a b
634631take n (Unfold step1 inject1) = Unfold step inject
635− where
636− inject x = do
637− s <- inject1 x
638− return (s, 0)
632+
633+ where
634+
635+ inject x = (, 0) <$> inject1 x
636+
639637 {-# INLINE_LATE step #-}
640638 step (st, i) | i < n = do
641− r <- step1 st
642− return $ case r of
639+ (\case
643640 Yield x s -> Yield x (s, i + 1)
644641 Skip s -> Skip (s, i)
645− Stop -> Stop
646− step (_, _) = return Stop
642+ Stop -> Stop) <$> step1 st
643+ step (_, _) = pure Stop
647644
648645-- | Same as 'filter' but with a monadic predicate.
649646--
@@ -676,31 +673,25 @@ filter f = filterM (return . f)
676673-- /Since: 0.8.0/
677674--
678675{-# INLINE_NORMAL drop #-}
679−drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
676+drop :: Applicative m => Int -> Unfold m a b -> Unfold m a b
680677drop n (Unfold step inject) = Unfold step' inject'
681678
682679 where
683680
684− inject' a = do
685− b <- inject a
686− return (b, n)
681+ inject' a = (, n) <$> inject a
687682
688683 {-# INLINE_LATE step' #-}
689684 step' (st, i)
690685 | i > 0 = do
691− r <- step st
692− return
693− $ case r of
694− Yield _ s -> Skip (s, i - 1)
695− Skip s -> Skip (s, i)
696− Stop -> Stop
686+ (\case
687+ Yield _ s -> Skip (s, i - 1)
688+ Skip s -> Skip (s, i)
689+ Stop -> Stop) <$> step st
697690 | otherwise = do
698− r <- step st
699− return
700− $ case r of
701− Yield x s -> Yield x (s, 0)
702− Skip s -> Skip (s, 0)
703− Stop -> Stop
691+ (\case
692+ Yield x s -> Yield x (s, 0)
693+ Skip s -> Skip (s, 0)
694+ Stop -> Stop) <$> step st
704695
705696-- | @dropWhileM f unf@ drops elements from the stream generated by @unf@ while
706697-- the condition holds true. The condition function @f@ is /monadic/ in nature.
@@ -793,10 +784,10 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
793784 return $ Skip (Left r)
794785 step (Left st) = do
795786 res <- estep st
796− case res of
797− Yield x s -> return $ Yield x (Left s)
798− Skip s -> return $ Skip (Left s)
799− Stop -> return Stop
787+ return $ case res of
788+ Yield x s -> Yield x (Left s)
789+ Skip s -> Skip (Left s)
790+ Stop -> Stop
800791
801792-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
802793-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
@@ -858,10 +849,10 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
858849 return $ Skip (Left r)
859850 step (Left st) = do
860851 res <- estep st
861− case res of
862− Yield x s -> return $ Yield x (Left s)
863− Skip s -> return $ Skip (Left s)
864− Stop -> return Stop
852+ return $ case res of
853+ Yield x s -> Yield x (Left s)
854+ Skip s -> Skip (Left s)
855+ Stop -> Stop
865856
866857-- | Run a side effect @a -> m c@ on the input @a@ before unfolding it using
867858-- @Unfold m a b@.
@@ -964,10 +955,10 @@ onException action (Unfold step1 inject1) = Unfold step inject
964955 {-# INLINE_LATE step #-}
965956 step (st, v) = do
966957 res <- step1 st `MC.onException` action v
967− case res of
968− Yield x s -> return $ Yield x (s, v)
969− Skip s -> return $ Skip (s, v)
970− Stop -> return Stop
958+ return $ case res of
959+ Yield x s -> Yield x (s, v)
960+ Skip s -> Skip (s, v)
961+ Stop -> Stop
971962
972963{-# INLINE_NORMAL _finally #-}
973964_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b