@@ -505,10 +505,13 @@ module Streamly.Internal.Data.Stream.IsStream
505505 , runStateT
506506
507507 -- * Maybe Streams
508+ , catMaybes
508509 , mapMaybe
509510 , mapMaybeM
510511
511512 -- * Either Streams
513+ , lefts
514+ , rights
512515 , iterateMapLeftsWith
513516
514517 -- * Diagnostics
@@ -538,12 +541,14 @@ import Control.Monad.Trans.Reader (ReaderT)
538541import Control.Monad.Trans.State.Strict (StateT)
539542import Control.Monad.Trans.Class (MonadTrans(..))
540543import Control.Monad.Trans.Control (MonadBaseControl)
544+import Data.Either (isLeft, isRight)
541545import Data.Functor.Identity (Identity (..))
542546import Data.Kind (Type)
543547import Data.Heap (Entry(..))
544548import Data.Maybe (isJust, fromJust, isNothing)
545549import Data.Void (Void)
546550import Foreign.Storable (Storable)
551+import Streamly.Internal.BaseCompat (fromLeft, fromRight)
547552import Streamly.Internal.Data.Stream.Enumeration
548553 (Enumerable(..), enumerate, enumerateTo)
549554import Streamly.Internal.Data.Fold.Types (Fold (..), Fold2 (..))
@@ -2563,6 +2568,13 @@ mapMaybeM f = fmap fromJust . filter isJust . K.mapM f
25632568mapMaybeMSerial :: Monad m => (a -> m (Maybe b)) -> SerialT m a -> SerialT m b
25642569mapMaybeMSerial f m = fromStreamD $ D.mapMaybeM f $ toStreamD m
25652570
2571+-- | In a stream of 'Maybe's, discard 'Nothing's and unwrap 'Just's.
2572+--
2573+-- /Internal/
2574+--
2575+catMaybes :: (IsStream t, Monad m, Functor (t m)) => t m (Maybe a) -> t m a
2576+catMaybes = fmap fromJust . filter isJust
2577+
25662578------------------------------------------------------------------------------
25672579-- Transformation by Reordering
25682580------------------------------------------------------------------------------
@@ -3496,6 +3508,20 @@ iterateSmapMWith combine f initial stream =
34963508-- Either streams
34973509------------------------------------------------------------------------------
34983510
3511+-- | Discard 'Right's and unwrap 'Left's in an 'Either' stream.
3512+--
3513+-- /Internal/
3514+--
3515+lefts :: (IsStream t, Monad m, Functor (t m)) => t m (Either a b) -> t m a
3516+lefts = fmap (fromLeft undefined) . filter isLeft
3517+
3518+-- | Discard 'Left's and unwrap 'Right's in an 'Either' stream.
3519+--
3520+-- /Internal/
3521+--
3522+rights :: (IsStream t, Monad m, Functor (t m)) => t m (Either a b) -> t m b
3523+rights = fmap (fromRight undefined) . filter isRight
3524+
34993525-- | In an 'Either' stream iterate on 'Left's. This is a special case of
35003526-- 'iterateMapWith':
35013527--