on master help sign in
Add catMaybes, lefts, rights
commitbf91f48e9586b91e26ef4ef84a98378a21335c69 authorHarendra Kumar <harendra@composewell.com> dateMon 31 Aug 2020, 19:29 (6 years ago) parentad141c6
3 files changed+42−12
src/Streamly/Internal/BaseCompat.hs+15 src/Streamly/Internal/Data/Stream/IsStream.hs+26 src/Streamly/Internal/FileSystem/Dir.hs+1−12
src/Streamly/Internal/BaseCompat.hs+15
@@ -11,10 +11,15 @@ module Streamly.Internal.BaseCompat
1111 (
1212 (#.)
1313 , errorWithoutStackTrace
14+ , fromLeft
15+ , fromRight
1416 )
1517where
1618
1719import Data.Coerce (Coercible, coerce)
20+#if MIN_VERSION_base(4,10,0)
21+import Data.Either (fromRight, fromLeft)
22+#endif
1823
1924{-# INLINE (#.) #-}
2025(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c)
@@ -25,3 +30,13 @@ import Data.Coerce (Coercible, coerce)
2530errorWithoutStackTrace :: String -> a
2631errorWithoutStackTrace = error
2732#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
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--
src/Streamly/Internal/FileSystem/Dir.hs+1−12
@@ -62,6 +62,7 @@ import Data.Either (isRight, isLeft)
6262import Prelude hiding (read)
6363
6464-- import Streamly.Data.Fold (Fold)
65+import Streamly.Internal.BaseCompat (fromLeft, fromRight)
6566import Streamly.Internal.Data.Unfold.Types (Unfold(..))
6667-- import Streamly.Internal.Memory.Array.Types
6768-- (Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
@@ -79,18 +80,6 @@ import qualified Streamly.Internal.Data.Stream.IsStream as S
7980-- import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
8081import qualified System.Directory as Dir
8182
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−
9483{-
9584{-# INLINABLE readArrayUpto #-}
9685readArrayUpto :: Int -> Handle -> IO (Array Word8)