@@ -183,6 +183,9 @@ module Streamly.Internal.Data.Unfold
184 , mapMWithInput
184 , mapMWithInput
186+ -- ** Either Wrapped Input
186 -- ** Filtering
189 -- ** Filtering
187 , takeWhileM
190 , takeWhileM
188 , takeWhile
191 , takeWhile
@@ -254,7 +257,7 @@ import Streamly.Internal.Data.SVar
254import Streamly.Internal.Data.Unfold.Type
257import Streamly.Internal.Data.Unfold.Type
255import Prelude
258import Prelude
256 hiding (map, mapM, takeWhile, take, filter, const, zipWith
259 hiding (map, mapM, takeWhile, take, filter, const, zipWith
257− , drop, dropWhile)
260+ , drop, dropWhile, either)
260-- >>> import qualified Streamly.Data.Fold as Fold
263-- >>> import qualified Streamly.Data.Fold as Fold
@@ -424,6 +427,36 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
424 Skip s -> return $ Skip (inp, s)
427 Skip s -> return $ Skip (inp, s)
425 Stop -> return Stop
428 Stop -> return Stop
431+-------------------------------------------------------------------------------
433+-------------------------------------------------------------------------------
435+-- | Make an unfold operate on values wrapped in an @Either a a@ type. 'Right
436+-- a' translates to 'Right b' and 'Left a' translates to 'Left b'.
439+{-# INLINE_NORMAL either #-}
440+either :: Monad m => Unfold m a b -> Unfold m (Either a a) (Either b b)
441+either (Unfold step1 inject1) = Unfold step inject
445+ inject (Left a) = do
448+ inject (Right a) = do
452+ {-# INLINE_LATE step #-}
456+ Yield x s -> Yield (f x) (s, f)
457+ Skip s -> Skip (s, f)
427-------------------------------------------------------------------------------
460-------------------------------------------------------------------------------
428-- Convert streams into unfolds
461-- Convert streams into unfolds
429-------------------------------------------------------------------------------
462-------------------------------------------------------------------------------