on master help sign in
Generalize either unfold and add nil unfold
2427715Harendra Kumar
core/src/Streamly/Internal/Data/Unfold.hs+23−11
@@ -126,6 +126,7 @@ module Streamly.Internal.Data.Unfold
126 , function
126 , function
127 , identity
127 , identity
128 , nilM
128 , nilM
129+ , nil
129 , consM
130 , consM
130
131
131 -- ** From Values
132 -- ** From Values
@@ -292,6 +293,7 @@ import qualified Streamly.Internal.Data.Fold.Type as FL
292import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
293import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
293import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
294import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
294import qualified Streamly.Internal.Data.Stream.Type as Stream
295import qualified Streamly.Internal.Data.Stream.Type as Stream
296+import qualified Prelude
295
297
296import Streamly.Internal.Data.Unfold.Enumeration
298import Streamly.Internal.Data.Unfold.Enumeration
297import Streamly.Internal.Data.Unfold.Type
299import Streamly.Internal.Data.Unfold.Type
@@ -448,25 +450,30 @@ foldMany (Fold fstep initial extract) (Unfold ustep inject1) =
448-- Either
450-- Either
449-------------------------------------------------------------------------------
451-------------------------------------------------------------------------------
450
452
451−-- | Make an unfold operate on values wrapped in an @Either a a@ type. 'Right
453+-- | Choose left or right unfold based on an either input.
452−-- a' translates to 'Right b' and 'Left a' translates to 'Left b'.
453--
454--
454−-- /Internal/
455+-- /Pre-release/
455{-# INLINE_NORMAL either #-}
456{-# INLINE_NORMAL either #-}
457+either :: Applicative m =>
456−either :: Applicative m => Unfold m a b -> Unfold m (Either a a) (Either b b)
458+ Unfold m a c -> Unfold m b c -> Unfold m (Either a b) c
457−either (Unfold step1 inject1) = Unfold step inject
459+either (Unfold stepL injectL) (Unfold stepR injectR) = Unfold step inject
458
460
459 where
461 where
460
462
461− inject (Left a) = (, Left) <$> inject1 a
463+ inject (Left x) = Left <$> injectL x
462− inject (Right a) = (, Right) <$> inject1 a
464+ inject (Right x) = Right <$> injectR x
463
465
464 {-# INLINE_LATE step #-}
466 {-# INLINE_LATE step #-}
465− step (st, f) = do
467+ step (Left st) = do
466 (\case
468 (\case
467− Yield x s -> Yield (f x) (s, f)
469+ Yield x s -> Yield x (Left s)
470+ Skip s -> Skip (Left s)
471+ Stop -> Stop) <$> stepL st
472+ step (Right st) = do
473+ (\case
474+ Yield x s -> Yield x (Right s)
468− Skip s -> Skip (s, f)
475+ Skip s -> Skip (Right s)
469− Stop -> Stop) <$> step1 st
476+ Stop -> Stop) <$> stepR st
470
477
471-- postscan2 :: Monad m => Refold m a b c -> Unfold m a b -> Unfold m a c
478-- postscan2 :: Monad m => Refold m a b c -> Unfold m a b -> Unfold m a c
472
479
@@ -616,6 +623,11 @@ nilM f = Unfold step pure
616 {-# INLINE_LATE step #-}
623 {-# INLINE_LATE step #-}
617 step x = f x $> Stop
624 step x = f x $> Stop
618
625
626+-- | An empty stream.
627+{-# INLINE nil #-}
628+nil :: Applicative m => Unfold m a b
629+nil = Unfold (Prelude.const (pure Stop)) pure
630+
619-- | Prepend a monadic single element generator function to an 'Unfold'. The
631-- | Prepend a monadic single element generator function to an 'Unfold'. The
620-- same seed is used in the action as well as the unfold.
632-- same seed is used in the action as well as the unfold.
621--
633--