on master help sign in
Add either stream utilities for folds
c94f540Harendra Kumar
core/src/Streamly/Internal/Data/Fold.hs+5
@@ -149,6 +149,11 @@ module Streamly.Internal.Data.Fold
149 , mapMaybe
149 , mapMaybe
150 -- , mapMaybeM
150 -- , mapMaybeM
151
151
152+ -- Either streams
153+ , lefts
154+ , rights
155+ , both
156+
152 -- ** Scanning Filters
157 -- ** Scanning Filters
153 , findIndices
158 , findIndices
154 {-
159 {-
core/src/Streamly/Internal/Data/Fold/Type.hs+33
@@ -356,6 +356,9 @@ module Streamly.Internal.Data.Fold.Type
356 , filter
356 , filter
357 , filterM
357 , filterM
358 , catMaybes
358 , catMaybes
359+ , lefts
360+ , rights
361+ , both
359
362
360 -- ** Trimming
363 -- ** Trimming
361 , take
364 , take
@@ -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
1174
1178
1179+------------------------------------------------------------------------------
1180+-- Either streams
1181+------------------------------------------------------------------------------
1182+
1183+-- | Discard 'Right's and unwrap 'Left's in an 'Either' stream.
1184+--
1185+-- /Pre-release/
1186+--
1187+{-# INLINE lefts #-}
1188+lefts :: (Monad m) => Fold m a c -> Fold m (Either a b) c
1189+lefts = filter isLeft . lmap (fromLeft undefined)
1190+
1191+-- | Discard 'Left's and unwrap 'Right's in an 'Either' stream.
1192+--
1193+-- /Pre-release/
1194+--
1195+{-# INLINE rights #-}
1196+rights :: (Monad m) => Fold m b c -> Fold m (Either a b) c
1197+rights = filter isRight . lmap (fromRight undefined)
1198+
1199+-- | Remove the either wrapper and flatten both lefts and as well as rights in
1200+-- the output stream.
1201+--
1202+-- /Pre-release/
1203+--
1204+{-# INLINE both #-}
1205+both :: Fold m a b -> Fold m (Either a a) b
1206+both = lmap (either id id)
1207+
1175------------------------------------------------------------------------------
1208------------------------------------------------------------------------------
1176-- Parsing
1209-- Parsing
1177------------------------------------------------------------------------------
1210------------------------------------------------------------------------------