@@ -356,6 +356,9 @@ module Streamly.Internal.Data.Fold.Type
357 , filterM
357 , filterM
358 , catMaybes
358 , catMaybes
360 -- ** Trimming
363 -- ** Trimming
@@ -396,6 +399,7 @@ where
396#include "inline.hs"
399#include "inline.hs"
397import Control.Monad ((>=>))
400import Control.Monad ((>=>))
398import Data.Bifunctor (Bifunctor(..))
401import Data.Bifunctor (Bifunctor(..))
402+import Data.Either (fromLeft, fromRight, isLeft, isRight)
399import Data.Maybe (isJust, fromJust)
403import Data.Maybe (isJust, fromJust)
400import Fusion.Plugin.Types (Fuse(..))
404import Fusion.Plugin.Types (Fuse(..))
401import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep, chainStepM)
405import Streamly.Internal.Data.Fold.Step (Step(..), mapMStep, chainStepM)
@@ -1172,6 +1176,35 @@ filterM f (Fold step begin done) = Fold step' begin done
1172catMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
1176catMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
1173catMaybes = filter isJust . lmap fromJust
1177catMaybes = filter isJust . lmap fromJust
1179+------------------------------------------------------------------------------
1181+------------------------------------------------------------------------------
1183+-- | Discard 'Right's and unwrap 'Left's in an 'Either' stream.
1187+{-# INLINE lefts #-}
1188+lefts :: (Monad m) => Fold m a c -> Fold m (Either a b) c
1189+lefts = filter isLeft . lmap (fromLeft undefined)
1191+-- | Discard 'Left's and unwrap 'Right's in an 'Either' stream.
1195+{-# INLINE rights #-}
1196+rights :: (Monad m) => Fold m b c -> Fold m (Either a b) c
1197+rights = filter isRight . lmap (fromRight undefined)
1199+-- | Remove the either wrapper and flatten both lefts and as well as rights in
1200+-- the output stream.
1205+both :: Fold m a b -> Fold m (Either a a) b
1206+both = lmap (either id id)
1175------------------------------------------------------------------------------
1208------------------------------------------------------------------------------
1176-- Parsing
1209-- Parsing
1177------------------------------------------------------------------------------
1210------------------------------------------------------------------------------