on master help sign in
Add catMaybes, lefts, rights
bf91f48Harendra Kumar
src/Streamly/Internal/BaseCompat.hs+15
@@ -11,10 +11,15 @@ module Streamly.Internal.BaseCompat
11 (
11 (
12 (#.)
12 (#.)
13 , errorWithoutStackTrace
13 , errorWithoutStackTrace
14+ , fromLeft
15+ , fromRight
14 )
16 )
15where
17where
16
18
17import Data.Coerce (Coercible, coerce)
19import Data.Coerce (Coercible, coerce)
20+#if MIN_VERSION_base(4,10,0)
21+import Data.Either (fromRight, fromLeft)
22+#endif
18
23
19{-# INLINE (#.) #-}
24{-# INLINE (#.) #-}
20(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
25(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
@@ -25,3 +30,13 @@ import Data.Coerce (Coercible, coerce)
25errorWithoutStackTrace :: String -> a
30errorWithoutStackTrace :: String -> a
26errorWithoutStackTrace = error
31errorWithoutStackTrace = error
27#endif
32#endif
33+
34+#if !(MIN_VERSION_base(4,10,0))
35+fromLeft :: a -> Either a b -> a
36+fromLeft _ (Left a) = a
37+fromLeft a _ = a
38+
39+fromRight :: b -> Either a b -> b
40+fromRight _ (Right b) = b
41+fromRight b _ = b
42+#endif
src/Streamly/Internal/Data/Stream/IsStream.hs+26
@@ -505,10 +505,13 @@ module Streamly.Internal.Data.Stream.IsStream
505 , runStateT
505 , runStateT
506
506
507 -- * Maybe Streams
507 -- * Maybe Streams
508+ , catMaybes
508 , mapMaybe
509 , mapMaybe
509 , mapMaybeM
510 , mapMaybeM
510
511
511 -- * Either Streams
512 -- * Either Streams
513+ , lefts
514+ , rights
512 , iterateMapLeftsWith
515 , iterateMapLeftsWith
513
516
514 -- * Diagnostics
517 -- * Diagnostics
@@ -538,12 +541,14 @@ import Control.Monad.Trans.Reader (ReaderT)
538import Control.Monad.Trans.State.Strict (StateT)
541import Control.Monad.Trans.State.Strict (StateT)
539import Control.Monad.Trans.Class (MonadTrans(..))
542import Control.Monad.Trans.Class (MonadTrans(..))
540import Control.Monad.Trans.Control (MonadBaseControl)
543import Control.Monad.Trans.Control (MonadBaseControl)
544+import Data.Either (isLeft, isRight)
541import Data.Functor.Identity (Identity (..))
545import Data.Functor.Identity (Identity (..))
542import Data.Kind (Type)
546import Data.Kind (Type)
543import Data.Heap (Entry(..))
547import Data.Heap (Entry(..))
544import Data.Maybe (isJust, fromJust, isNothing)
548import Data.Maybe (isJust, fromJust, isNothing)
545import Data.Void (Void)
549import Data.Void (Void)
546import Foreign.Storable (Storable)
550import Foreign.Storable (Storable)
551+import Streamly.Internal.BaseCompat (fromLeft, fromRight)
547import Streamly.Internal.Data.Stream.Enumeration
552import Streamly.Internal.Data.Stream.Enumeration
548 (Enumerable(..), enumerate, enumerateTo)
553 (Enumerable(..), enumerate, enumerateTo)
549import Streamly.Internal.Data.Fold.Types (Fold (..), Fold2 (..))
554import Streamly.Internal.Data.Fold.Types (Fold (..), Fold2 (..))
@@ -2563,6 +2568,13 @@ mapMaybeM f = fmap fromJust . filter isJust . K.mapM f
2563mapMaybeMSerial :: Monad m => (a -> m (Maybe b)) -> SerialT m a -> SerialT m b
2568mapMaybeMSerial :: Monad m => (a -> m (Maybe b)) -> SerialT m a -> SerialT m b
2564mapMaybeMSerial f m = fromStreamD $ D.mapMaybeM f $ toStreamD m
2569mapMaybeMSerial f m = fromStreamD $ D.mapMaybeM f $ toStreamD m
2565
2570
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+
2566------------------------------------------------------------------------------
2578------------------------------------------------------------------------------
2567-- Transformation by Reordering
2579-- Transformation by Reordering
2568------------------------------------------------------------------------------
2580------------------------------------------------------------------------------
@@ -3496,6 +3508,20 @@ iterateSmapMWith combine f initial stream =
3496-- Either streams
3508-- Either streams
3497------------------------------------------------------------------------------
3509------------------------------------------------------------------------------
3498
3510
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+
3499-- | In an 'Either' stream iterate on 'Left's. This is a special case of
3525-- | In an 'Either' stream iterate on 'Left's. This is a special case of
3500-- 'iterateMapWith':
3526-- 'iterateMapWith':
3501--
3527--
src/Streamly/Internal/FileSystem/Dir.hs+1−12
@@ -62,6 +62,7 @@ import Data.Either (isRight, isLeft)
62import Prelude hiding (read)
62import Prelude hiding (read)
63
63
64-- import Streamly.Data.Fold (Fold)
64-- import Streamly.Data.Fold (Fold)
65+import Streamly.Internal.BaseCompat (fromLeft, fromRight)
65import Streamly.Internal.Data.Unfold.Types (Unfold(..))
66import Streamly.Internal.Data.Unfold.Types (Unfold(..))
66-- import Streamly.Internal.Memory.Array.Types
67-- import Streamly.Internal.Memory.Array.Types
67-- (Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
68-- (Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
@@ -79,18 +80,6 @@ import qualified Streamly.Internal.Data.Stream.IsStream as S
79-- import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
80-- import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
80import qualified System.Directory as Dir
81import qualified System.Directory as Dir
81
82
82−#if MIN_VERSION_base(4,10,0)
83−import Data.Either (fromRight, fromLeft)
84−#else
85−fromLeft :: a -> Either a b -> a
86−fromLeft _ (Left a) = a
87−fromLeft a _ = a
88−
89−fromRight :: b -> Either a b -> b
90−fromRight _ (Right b) = b
91−fromRight b _ = b
92−#endif
93−
94{-
83{-
95{-# INLINABLE readArrayUpto #-}
84{-# INLINABLE readArrayUpto #-}
96readArrayUpto :: Int -> Handle -> IO (Array Word8)
85readArrayUpto :: Int -> Handle -> IO (Array Word8)