module Streamly.Internal.Data.Stream.IsStream.Eliminate
(
fold
, fold_
, parse
, parseK
, parseD
, parse_
, parseD_
, uncons
, foldrM
, foldr
, foldl'
, foldl1'
, foldlM'
, mapM_
, drain
, last
, length
, sum
, product
, mconcat
, maximumBy
, maximum
, minimumBy
, minimum
, the
, drainN
, drainWhile
, (!!)
, head
, headElse
, tail
, init
, findM
, find
, findIndex
, elemIndex
, lookup
, null
, elem
, notElem
, all
, any
, and
, or
, toList
, toListRev
, toStream
, toStreamRev
, foldAsync
, (|$.)
, (|&.)
, eqBy
, cmpBy
, isPrefixOf
, isInfixOf
, isSuffixOf
, isSubsequenceOf
, stripPrefix
, stripSuffix
, foldx
, foldxM
, foldr1
, runStream
, runN
, runWhile
, toHandle
)
where
#include "inline.hs"
import Control.Monad.Catch (MonadThrow)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Functor.Identity (Identity (..))
import Foreign.Storable (Storable)
import Streamly.Internal.Data.Parser (Parser (..))
import Streamly.Internal.Data.SVar (MonadAsync, defState)
import Streamly.Internal.Data.Stream.IsStream.Common
( fold, fold_, drop, findIndices, reverse, splitOnSeq, take
, takeWhile)
import Streamly.Internal.Data.Stream.Prelude (toStreamS)
import Streamly.Internal.Data.Stream.StreamD (fromStreamD, toStreamD)
import Streamly.Internal.Data.Stream.StreamK (IsStream)
import Streamly.Internal.Data.Stream.Serial (SerialT)
import qualified Streamly.Internal.Data.Array.Foreign.Type as A
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Data.Stream.Parallel as Par
import qualified Streamly.Internal.Data.Stream.Prelude as P
import qualified Streamly.Internal.Data.Stream.StreamD as D
import qualified Streamly.Internal.Data.Stream.StreamK as K
import qualified Streamly.Internal.Data.Parser.ParserD as PRD
import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
import qualified System.IO as IO
#ifdef USE_STREAMK_ONLY
import qualified Streamly.Internal.Data.Stream.StreamK as S
#else
import qualified Streamly.Internal.Data.Stream.StreamD as S
#endif
import Prelude hiding
( drop, take, takeWhile, foldr , foldl, mapM_, sequence, all, any, sum
, product, elem, notElem, maximum, minimum, head, last, tail, length
, null , reverse, init, and, or, lookup, foldr1, (!!) , splitAt, break
, mconcat)
{-# INLINE uncons #-}
uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
uncons :: SerialT m a -> m (Maybe (a, t m a))
uncons SerialT m a
m = t m a -> m (Maybe (a, t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (a, t m a))
K.uncons (SerialT m a -> t m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
(m :: * -> *) a.
(IsStream t1, IsStream t2) =>
t1 m a -> t2 m a
K.adapt SerialT m a
m)
{-# INLINE foldrM #-}
foldrM :: Monad m => (a -> m b -> m b) -> m b -> SerialT m a -> m b
foldrM :: (a -> m b -> m b) -> m b -> SerialT m a -> m b
foldrM = (a -> m b -> m b) -> m b -> SerialT m a -> m b
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a b.
(Monad m, IsStream t) =>
(a -> m b -> m b) -> m b -> t m a -> m b
P.foldrM
{-# INLINE foldr #-}
foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
foldr :: (a -> b -> b) -> b -> SerialT m a -> m b
foldr = (a -> b -> b) -> b -> SerialT m a -> m b
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a b.
(Monad m, IsStream t) =>
(a -> b -> b) -> b -> t m a -> m b
P.foldr
{-# INLINE foldr1 #-}
{-# DEPRECATED foldr1 "Use foldrM instead." #-}
foldr1 :: Monad m => (a -> a -> a) -> SerialT m a -> m (Maybe a)
foldr1 :: (a -> a -> a) -> SerialT m a -> m (Maybe a)
foldr1 a -> a -> a
f SerialT m a
m = (a -> a -> a) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m (Maybe a)
S.foldr1 a -> a -> a
f (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# DEPRECATED foldx "Please use foldl' followed by fmap instead." #-}
{-# INLINE foldx #-}
foldx :: Monad m => (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
foldx :: (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
foldx = (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) x a b.
(IsStream t, Monad m) =>
(x -> a -> x) -> x -> (x -> b) -> t m a -> m b
P.foldlx'
{-# INLINE foldl' #-}
foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
foldl' :: (b -> a -> b) -> b -> SerialT m a -> m b
foldl' = (b -> a -> b) -> b -> SerialT m a -> m b
forall (m :: * -> *) (t :: (* -> *) -> * -> *) b a.
(Monad m, IsStream t) =>
(b -> a -> b) -> b -> t m a -> m b
P.foldl'
{-# INLINE foldl1' #-}
foldl1' :: Monad m => (a -> a -> a) -> SerialT m a -> m (Maybe a)
foldl1' :: (a -> a -> a) -> SerialT m a -> m (Maybe a)
foldl1' a -> a -> a
step SerialT m a
m = do
Maybe (a, SerialT m a)
r <- SerialT m a -> m (Maybe (a, SerialT m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
SerialT m a -> m (Maybe (a, t m a))
uncons SerialT m a
m
case Maybe (a, SerialT m a)
r of
Maybe (a, SerialT m a)
Nothing -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Just (a
h, SerialT m a
t) -> do
a
res <- (a -> a -> a) -> a -> SerialT m a -> m a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SerialT m a -> m b
foldl' a -> a -> a
step a
h SerialT m a
t
Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
res
{-# DEPRECATED foldxM "Please use foldlM' followed by fmap instead." #-}
{-# INLINE foldxM #-}
foldxM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
foldxM :: (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
foldxM = (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) x a b.
(IsStream t, Monad m) =>
(x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
P.foldlMx'
{-# INLINE foldlM' #-}
foldlM' :: Monad m => (b -> a -> m b) -> m b -> SerialT m a -> m b
foldlM' :: (b -> a -> m b) -> m b -> SerialT m a -> m b
foldlM' b -> a -> m b
step m b
begin SerialT m a
m = (b -> a -> m b) -> m b -> Stream m a -> m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
S.foldlM' b -> a -> m b
step m b
begin (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m
{-# INLINE_NORMAL parseD #-}
parseD :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m b
parseD :: Parser m a b -> SerialT m a -> m b
parseD Parser m a b
p = Parser m a b -> Stream m a -> m b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Stream m a -> m b
D.parse Parser m a b
p (Stream m a -> m b)
-> (SerialT m a -> Stream m a) -> SerialT m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# INLINE parseK #-}
parseK :: MonadThrow m => PRK.Parser m a b -> SerialT m a -> m b
parseK :: Parser m a b -> SerialT m a -> m b
parseK = Parser m a b -> SerialT m a -> m b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> SerialT m a -> m b
parse
{-# INLINE [3] parse #-}
parse :: MonadThrow m => Parser m a b -> SerialT m a -> m b
parse :: Parser m a b -> SerialT m a -> m b
parse = Parser m a b -> SerialT m a -> m b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> SerialT m a -> m b
parseD (Parser m a b -> SerialT m a -> m b)
-> (Parser m a b -> Parser m a b)
-> Parser m a b
-> SerialT m a
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser m a b -> Parser m a b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
PRK.fromParserK
{-# INLINE_NORMAL parseD_ #-}
parseD_ :: MonadThrow m => PRD.Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseD_ :: Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseD_ Parser m a b
parser SerialT m a
strm = do
(b
b, Stream m a
strmD) <- Parser m a b -> Stream m a -> m (b, Stream m a)
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Stream m a -> m (b, Stream m a)
D.parse_ Parser m a b
parser (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD SerialT m a
strm)
(b, SerialT m a) -> m (b, SerialT m a)
forall (m :: * -> *) a. Monad m => a -> m a
return ((b, SerialT m a) -> m (b, SerialT m a))
-> (b, SerialT m a) -> m (b, SerialT m a)
forall a b. (a -> b) -> a -> b
$! (b
b, Stream m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD Stream m a
strmD)
{-# INLINE [3] parse_ #-}
parse_ :: MonadThrow m => Parser m a b -> SerialT m a -> m (b, SerialT m a)
parse_ :: Parser m a b -> SerialT m a -> m (b, SerialT m a)
parse_ = Parser m a b -> SerialT m a -> m (b, SerialT m a)
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> SerialT m a -> m (b, SerialT m a)
parseD_ (Parser m a b -> SerialT m a -> m (b, SerialT m a))
-> (Parser m a b -> Parser m a b)
-> Parser m a b
-> SerialT m a
-> m (b, SerialT m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser m a b -> Parser m a b
forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
PRK.fromParserK
{-# INLINE mapM_ #-}
mapM_ :: Monad m => (a -> m b) -> SerialT m a -> m ()
mapM_ :: (a -> m b) -> SerialT m a -> m ()
mapM_ a -> m b
f SerialT m a
m = (a -> m b) -> Stream m a -> m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> m ()
S.mapM_ a -> m b
f (Stream m a -> m ()) -> Stream m a -> m ()
forall a b. (a -> b) -> a -> b
$ SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m
{-# INLINE drain #-}
drain :: Monad m => SerialT m a -> m ()
drain :: SerialT m a -> m ()
drain = SerialT m a -> m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m ()
P.drain
{-# INLINE drainN #-}
drainN :: Monad m => Int -> SerialT m a -> m ()
drainN :: Int -> SerialT m a -> m ()
drainN Int
n = SerialT m a -> m ()
forall (m :: * -> *) a. Monad m => SerialT m a -> m ()
drain (SerialT m a -> m ())
-> (SerialT m a -> SerialT m a) -> SerialT m a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Int -> t m a -> t m a
take Int
n
{-# DEPRECATED runN "Please use \"drainN\" instead" #-}
{-# INLINE runN #-}
runN :: Monad m => Int -> SerialT m a -> m ()
runN :: Int -> SerialT m a -> m ()
runN = Int -> SerialT m a -> m ()
forall (m :: * -> *) a. Monad m => Int -> SerialT m a -> m ()
drainN
{-# INLINE drainWhile #-}
drainWhile :: Monad m => (a -> Bool) -> SerialT m a -> m ()
drainWhile :: (a -> Bool) -> SerialT m a -> m ()
drainWhile a -> Bool
p = SerialT m a -> m ()
forall (m :: * -> *) a. Monad m => SerialT m a -> m ()
drain (SerialT m a -> m ())
-> (SerialT m a -> SerialT m a) -> SerialT m a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> Bool) -> t m a -> t m a
takeWhile a -> Bool
p
{-# DEPRECATED runWhile "Please use \"drainWhile\" instead" #-}
{-# INLINE runWhile #-}
runWhile :: Monad m => (a -> Bool) -> SerialT m a -> m ()
runWhile :: (a -> Bool) -> SerialT m a -> m ()
runWhile = (a -> Bool) -> SerialT m a -> m ()
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> SerialT m a -> m ()
drainWhile
{-# DEPRECATED runStream "Please use \"drain\" instead" #-}
{-# INLINE runStream #-}
runStream :: Monad m => SerialT m a -> m ()
runStream :: SerialT m a -> m ()
runStream = SerialT m a -> m ()
forall (m :: * -> *) a. Monad m => SerialT m a -> m ()
drain
{-# INLINE null #-}
null :: Monad m => SerialT m a -> m Bool
null :: SerialT m a -> m Bool
null = Stream m a -> m Bool
forall (m :: * -> *) a. Monad m => Stream m a -> m Bool
S.null (Stream m a -> m Bool)
-> (SerialT m a -> Stream m a) -> SerialT m a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS
{-# INLINE head #-}
head :: Monad m => SerialT m a -> m (Maybe a)
head :: SerialT m a -> m (Maybe a)
head = Stream m a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => Stream m a -> m (Maybe a)
S.head (Stream m a -> m (Maybe a))
-> (SerialT m a -> Stream m a) -> SerialT m a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS
{-# INLINE headElse #-}
headElse :: Monad m => a -> SerialT m a -> m a
headElse :: a -> SerialT m a -> m a
headElse a
x = a -> Stream m a -> m a
forall (m :: * -> *) a. Monad m => a -> Stream m a -> m a
D.headElse a
x (Stream m a -> m a)
-> (SerialT m a -> Stream m a) -> SerialT m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# INLINE tail #-}
tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
tail :: SerialT m a -> m (Maybe (t m a))
tail SerialT m a
m = t m a -> m (Maybe (t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (t m a))
K.tail (SerialT m a -> t m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
(m :: * -> *) a.
(IsStream t1, IsStream t2) =>
t1 m a -> t2 m a
K.adapt SerialT m a
m)
{-# INLINE init #-}
init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
init :: SerialT m a -> m (Maybe (t m a))
init SerialT m a
m = t m a -> m (Maybe (t m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> m (Maybe (t m a))
K.init (SerialT m a -> t m a
forall (t1 :: (* -> *) -> * -> *) (t2 :: (* -> *) -> * -> *)
(m :: * -> *) a.
(IsStream t1, IsStream t2) =>
t1 m a -> t2 m a
K.adapt SerialT m a
m)
{-# INLINE last #-}
last :: Monad m => SerialT m a -> m (Maybe a)
last :: SerialT m a -> m (Maybe a)
last SerialT m a
m = Stream m a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => Stream m a -> m (Maybe a)
S.last (Stream m a -> m (Maybe a)) -> Stream m a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m
{-# INLINE elem #-}
elem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
elem :: a -> SerialT m a -> m Bool
elem a
e SerialT m a
m = a -> Stream m a -> m Bool
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.elem a
e (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE notElem #-}
notElem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
notElem :: a -> SerialT m a -> m Bool
notElem a
e SerialT m a
m = a -> Stream m a -> m Bool
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.notElem a
e (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE length #-}
length :: Monad m => SerialT m a -> m Int
length :: SerialT m a -> m Int
length = (Int -> a -> Int) -> Int -> SerialT m a -> m Int
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SerialT m a -> m b
foldl' (\Int
n a
_ -> Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
0
{-# INLINE all #-}
all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
all :: (a -> Bool) -> SerialT m a -> m Bool
all a -> Bool
p SerialT m a
m = (a -> Bool) -> Stream m a -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> m Bool
S.all a -> Bool
p (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE any #-}
any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
any :: (a -> Bool) -> SerialT m a -> m Bool
any a -> Bool
p SerialT m a
m = (a -> Bool) -> Stream m a -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> m Bool
S.any a -> Bool
p (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE and #-}
and :: Monad m => SerialT m Bool -> m Bool
and :: SerialT m Bool -> m Bool
and = (Bool -> Bool) -> SerialT m Bool -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> SerialT m a -> m Bool
all (Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
==Bool
True)
{-# INLINE or #-}
or :: Monad m => SerialT m Bool -> m Bool
or :: SerialT m Bool -> m Bool
or = (Bool -> Bool) -> SerialT m Bool -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> SerialT m a -> m Bool
any (Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
==Bool
True)
{-# INLINE sum #-}
sum :: (Monad m, Num a) => SerialT m a -> m a
sum :: SerialT m a -> m a
sum = (a -> a -> a) -> a -> SerialT m a -> m a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SerialT m a -> m b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE product #-}
product :: (Monad m, Num a) => SerialT m a -> m a
product :: SerialT m a -> m a
product = (a -> a -> a) -> a -> SerialT m a -> m a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SerialT m a -> m b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
{-# INLINE mconcat #-}
mconcat :: (Monad m, Monoid a) => SerialT m a -> m a
mconcat :: SerialT m a -> m a
mconcat = (a -> a -> a) -> a -> SerialT m a -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> SerialT m a -> m b
foldr a -> a -> a
forall a. Monoid a => a -> a -> a
mappend a
forall a. Monoid a => a
mempty
{-# INLINE minimum #-}
minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
minimum :: SerialT m a -> m (Maybe a)
minimum SerialT m a
m = Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Monad m, Ord a) =>
Stream m a -> m (Maybe a)
S.minimum (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE minimumBy #-}
minimumBy :: Monad m => (a -> a -> Ordering) -> SerialT m a -> m (Maybe a)
minimumBy :: (a -> a -> Ordering) -> SerialT m a -> m (Maybe a)
minimumBy a -> a -> Ordering
cmp SerialT m a
m = (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> Ordering) -> Stream m a -> m (Maybe a)
S.minimumBy a -> a -> Ordering
cmp (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE maximum #-}
maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
maximum :: SerialT m a -> m (Maybe a)
maximum = SerialT m a -> m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m, Ord a) =>
t m a -> m (Maybe a)
P.maximum
{-# INLINE maximumBy #-}
maximumBy :: Monad m => (a -> a -> Ordering) -> SerialT m a -> m (Maybe a)
maximumBy :: (a -> a -> Ordering) -> SerialT m a -> m (Maybe a)
maximumBy a -> a -> Ordering
cmp SerialT m a
m = (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> a -> Ordering) -> Stream m a -> m (Maybe a)
S.maximumBy a -> a -> Ordering
cmp (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE the #-}
the :: (Eq a, Monad m) => SerialT m a -> m (Maybe a)
the :: SerialT m a -> m (Maybe a)
the SerialT m a
m = Stream m a -> m (Maybe a)
forall a (m :: * -> *).
(Eq a, Monad m) =>
Stream m a -> m (Maybe a)
S.the (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE (!!) #-}
(!!) :: Monad m => SerialT m a -> Int -> m (Maybe a)
SerialT m a
m !! :: SerialT m a -> Int -> m (Maybe a)
!! Int
i = SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m Stream m a -> Int -> m (Maybe a)
forall (m :: * -> *) a. Monad m => Stream m a -> Int -> m (Maybe a)
S.!! Int
i
{-# INLINE lookup #-}
lookup :: (Monad m, Eq a) => a -> SerialT m (a, b) -> m (Maybe b)
lookup :: a -> SerialT m (a, b) -> m (Maybe b)
lookup a
a SerialT m (a, b)
m = a -> Stream m (a, b) -> m (Maybe b)
forall (m :: * -> *) a b.
(Monad m, Eq a) =>
a -> Stream m (a, b) -> m (Maybe b)
S.lookup a
a (SerialT m (a, b) -> Stream m (a, b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m (a, b)
m)
{-# INLINE find #-}
find :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe a)
find :: (a -> Bool) -> SerialT m a -> m (Maybe a)
find a -> Bool
p SerialT m a
m = (a -> Bool) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> m (Maybe a)
S.find a -> Bool
p (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE findM #-}
findM :: Monad m => (a -> m Bool) -> SerialT m a -> m (Maybe a)
findM :: (a -> m Bool) -> SerialT m a -> m (Maybe a)
findM a -> m Bool
p SerialT m a
m = (a -> m Bool) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
S.findM a -> m Bool
p (SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS SerialT m a
m)
{-# INLINE findIndex #-}
findIndex :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe Int)
findIndex :: (a -> Bool) -> SerialT m a -> m (Maybe Int)
findIndex a -> Bool
p = SerialT m Int -> m (Maybe Int)
forall (m :: * -> *) a. Monad m => SerialT m a -> m (Maybe a)
head (SerialT m Int -> m (Maybe Int))
-> (SerialT m a -> SerialT m Int) -> SerialT m a -> m (Maybe Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> SerialT m a -> SerialT m Int
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
(a -> Bool) -> t m a -> t m Int
findIndices a -> Bool
p
{-# INLINE elemIndex #-}
elemIndex :: (Monad m, Eq a) => a -> SerialT m a -> m (Maybe Int)
elemIndex :: a -> SerialT m a -> m (Maybe Int)
elemIndex a
a = (a -> Bool) -> SerialT m a -> m (Maybe Int)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> SerialT m a -> m (Maybe Int)
findIndex (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a)
{-# INLINE toList #-}
toList :: Monad m => SerialT m a -> m [a]
toList :: SerialT m a -> m [a]
toList = SerialT m a -> m [a]
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
t m a -> m [a]
P.toList
{-# INLINE toListRev #-}
toListRev :: Monad m => SerialT m a -> m [a]
toListRev :: SerialT m a -> m [a]
toListRev = Stream m a -> m [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
D.toListRev (Stream m a -> m [a])
-> (SerialT m a -> Stream m a) -> SerialT m a -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD
{-# DEPRECATED toHandle
"Please use Streamly.FileSystem.Handle module (see the changelog)" #-}
toHandle :: MonadIO m => IO.Handle -> SerialT m String -> m ()
toHandle :: Handle -> SerialT m String -> m ()
toHandle Handle
h = SerialT m String -> m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
t m String -> m ()
go
where
go :: t m String -> m ()
go t m String
m1 =
let stop :: m ()
stop = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
single :: String -> m ()
single String
a = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
IO.hPutStrLn Handle
h String
a)
yieldk :: String -> t m String -> m ()
yieldk String
a t m String
r = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
IO.hPutStrLn Handle
h String
a) m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> t m String -> m ()
go t m String
r
in State Stream m String
-> (String -> t m String -> m ())
-> (String -> m ())
-> m ()
-> t m String
-> m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a r.
IsStream t =>
State Stream m a
-> (a -> t m a -> m r) -> (a -> m r) -> m r -> t m a -> m r
K.foldStream State Stream m String
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState String -> t m String -> m ()
yieldk String -> m ()
forall (m :: * -> *). MonadIO m => String -> m ()
single m ()
stop t m String
m1
{-# INLINE toStream #-}
toStream :: Monad m => SerialT m a -> m (SerialT Identity a)
toStream :: SerialT m a -> m (SerialT Identity a)
toStream = (a -> SerialT Identity a -> SerialT Identity a)
-> SerialT Identity a -> SerialT m a -> m (SerialT Identity a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> SerialT m a -> m b
foldr a -> SerialT Identity a -> SerialT Identity a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
K.cons SerialT Identity a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil
{-# INLINE toStreamRev #-}
toStreamRev :: Monad m => SerialT m a -> m (SerialT Identity a)
toStreamRev :: SerialT m a -> m (SerialT Identity a)
toStreamRev = (SerialT Identity a -> a -> SerialT Identity a)
-> SerialT Identity a -> SerialT m a -> m (SerialT Identity a)
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> SerialT m a -> m b
foldl' ((a -> SerialT Identity a -> SerialT Identity a)
-> SerialT Identity a -> a -> SerialT Identity a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> SerialT Identity a -> SerialT Identity a
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
K.cons) SerialT Identity a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil
{-# INLINE (|$.) #-}
(|$.) :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
|$. :: (t m a -> m b) -> t m a -> m b
(|$.) t m a -> m b
f = t m a -> m b
f (t m a -> m b) -> (t m a -> t m a) -> t m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, MonadAsync m) =>
t m a -> t m a
Par.mkParallel
infixr 0 |$.
{-# INLINE foldAsync #-}
foldAsync :: (IsStream t, MonadAsync m) => (t m a -> m b) -> (t m a -> m b)
foldAsync :: (t m a -> m b) -> t m a -> m b
foldAsync = (t m a -> m b) -> t m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadAsync m) =>
(t m a -> m b) -> t m a -> m b
(|$.)
{-# INLINE (|&.) #-}
(|&.) :: (IsStream t, MonadAsync m) => t m a -> (t m a -> m b) -> m b
t m a
x |&. :: t m a -> (t m a -> m b) -> m b
|&. t m a -> m b
f = t m a -> m b
f (t m a -> m b) -> t m a -> m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadAsync m) =>
(t m a -> m b) -> t m a -> m b
|$. t m a
x
infixl 1 |&.
{-# INLINE isPrefixOf #-}
isPrefixOf :: (Eq a, IsStream t, Monad m) => t m a -> t m a -> m Bool
isPrefixOf :: t m a -> t m a -> m Bool
isPrefixOf t m a
m1 t m a
m2 = Stream m a -> Stream m a -> m Bool
forall a (m :: * -> *).
(Eq a, Monad m) =>
Stream m a -> Stream m a -> m Bool
D.isPrefixOf (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m1) (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m2)
{-# INLINE isInfixOf #-}
isInfixOf :: (MonadIO m, Eq a, Enum a, Storable a)
=> SerialT m a -> SerialT m a -> m Bool
isInfixOf :: SerialT m a -> SerialT m a -> m Bool
isInfixOf SerialT m a
infx SerialT m a
stream = do
Array a
arr <- Fold m a (Array a) -> SerialT m a -> m (Array a)
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> SerialT m a -> m b
fold Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold m a (Array a)
A.write SerialT m a
infx
Bool
r <- SerialT m () -> m Bool
forall (m :: * -> *) a. Monad m => SerialT m a -> m Bool
null (SerialT m () -> m Bool) -> SerialT m () -> m Bool
forall a b. (a -> b) -> a -> b
$ Int -> SerialT m () -> SerialT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Int -> t m a -> t m a
drop Int
1 (SerialT m () -> SerialT m ()) -> SerialT m () -> SerialT m ()
forall a b. (a -> b) -> a -> b
$ Array a -> Fold m a () -> SerialT m a -> SerialT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, MonadIO m, Storable a, Enum a, Eq a) =>
Array a -> Fold m a b -> t m a -> t m b
splitOnSeq Array a
arr Fold m a ()
forall (m :: * -> *) a. Monad m => Fold m a ()
FL.drain SerialT m a
stream
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Bool
not Bool
r)
{-# INLINE isSuffixOf #-}
isSuffixOf :: (Monad m, Eq a) => SerialT m a -> SerialT m a -> m Bool
isSuffixOf :: SerialT m a -> SerialT m a -> m Bool
isSuffixOf SerialT m a
suffix SerialT m a
stream = SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> t m a
reverse SerialT m a
suffix SerialT m a -> SerialT m a -> m Bool
forall a (t :: (* -> *) -> * -> *) (m :: * -> *).
(Eq a, IsStream t, Monad m) =>
t m a -> t m a -> m Bool
`isPrefixOf` SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> t m a
reverse SerialT m a
stream
{-# INLINE isSubsequenceOf #-}
isSubsequenceOf :: (Eq a, IsStream t, Monad m) => t m a -> t m a -> m Bool
isSubsequenceOf :: t m a -> t m a -> m Bool
isSubsequenceOf t m a
m1 t m a
m2 = Stream m a -> Stream m a -> m Bool
forall a (m :: * -> *).
(Eq a, Monad m) =>
Stream m a -> Stream m a -> m Bool
D.isSubsequenceOf (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m1) (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m2)
{-# INLINE stripPrefix #-}
stripPrefix
:: (Eq a, IsStream t, Monad m)
=> t m a -> t m a -> m (Maybe (t m a))
stripPrefix :: t m a -> t m a -> m (Maybe (t m a))
stripPrefix t m a
m1 t m a
m2 = (Stream m a -> t m a) -> Maybe (Stream m a) -> Maybe (t m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamD (Maybe (Stream m a) -> Maybe (t m a))
-> m (Maybe (Stream m a)) -> m (Maybe (t m a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Stream m a -> Stream m a -> m (Maybe (Stream m a))
forall a (m :: * -> *).
(Eq a, Monad m) =>
Stream m a -> Stream m a -> m (Maybe (Stream m a))
D.stripPrefix (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m1) (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamD t m a
m2)
{-# INLINE stripSuffix #-}
stripSuffix
:: (Monad m, Eq a)
=> SerialT m a -> SerialT m a -> m (Maybe (SerialT m a))
stripSuffix :: SerialT m a -> SerialT m a -> m (Maybe (SerialT m a))
stripSuffix SerialT m a
m1 SerialT m a
m2 = (SerialT m a -> SerialT m a)
-> Maybe (SerialT m a) -> Maybe (SerialT m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> t m a
reverse (Maybe (SerialT m a) -> Maybe (SerialT m a))
-> m (Maybe (SerialT m a)) -> m (Maybe (SerialT m a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SerialT m a -> SerialT m a -> m (Maybe (SerialT m a))
forall a (t :: (* -> *) -> * -> *) (m :: * -> *).
(Eq a, IsStream t, Monad m) =>
t m a -> t m a -> m (Maybe (t m a))
stripPrefix (SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> t m a
reverse SerialT m a
m1) (SerialT m a -> SerialT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> t m a
reverse SerialT m a
m2)
{-# INLINABLE eqBy #-}
eqBy :: (IsStream t, Monad m) => (a -> b -> Bool) -> t m a -> t m b -> m Bool
eqBy :: (a -> b -> Bool) -> t m a -> t m b -> m Bool
eqBy = (a -> b -> Bool) -> t m a -> t m b -> m Bool
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b -> Bool) -> t m a -> t m b -> m Bool
P.eqBy
{-# INLINABLE cmpBy #-}
cmpBy
:: (IsStream t, Monad m)
=> (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
cmpBy :: (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
cmpBy = (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
(IsStream t, Monad m) =>
(a -> b -> Ordering) -> t m a -> t m b -> m Ordering
P.cmpBy