{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# OPTIONS_GHC -Wno-orphans #-}
#endif
#include "inline.hs"
module Streamly.Internal.Data.Stream.Prelude
(
fromStreamS
, toStreamS
, drain
, fromList
, toList
, foldrM
, foldrMx
, foldr
, foldlx'
, foldlMx'
, foldl'
, runFold
, parselMx'
, foldlS
, foldlT
, scanlx'
, scanlMx'
, postscanlx'
, postscanlMx'
, eqBy
, cmpBy
, minimum
, maximum
, K.concatMapBy
, K.concatMap
, foldWith
, foldMapWith
, forEachWith
)
where
import Control.Monad.Catch (MonadThrow)
import Control.Monad.Trans (MonadTrans(..))
import Prelude hiding (foldr, minimum, maximum)
import qualified Prelude
import Streamly.Internal.Data.Fold.Types (Fold (..))
import Streamly.Internal.Data.Parser.Types (Step)
#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 Streamly.Internal.Data.Stream.StreamK (IsStream(..))
import qualified Streamly.Internal.Data.Stream.StreamK as K
import qualified Streamly.Internal.Data.Stream.StreamD as D
{-# INLINE fromStreamS #-}
fromStreamS :: (IsStream t, Monad m) => S.Stream m a -> t m a
fromStreamS :: Stream m a -> t m a
fromStreamS = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
Stream m a -> t m a
fromStream (Stream m a -> t m a)
-> (Stream m a -> Stream m a) -> Stream m a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
S.toStreamK
{-# INLINE toStreamS #-}
toStreamS :: (IsStream t, Monad m) => t m a -> S.Stream m a
toStreamS :: t m a -> Stream m a
toStreamS = Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
S.fromStreamK (Stream m a -> Stream m a)
-> (t m a -> Stream m a) -> t m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a -> Stream m a
toStream
{-# INLINE_EARLY drain #-}
drain :: (IsStream t, Monad m) => t m a -> m ()
drain :: t m a -> m ()
drain t m a
m = Stream m a -> m ()
forall (m :: * -> *) a. Monad m => Stream m a -> m ()
D.drain (Stream m a -> m ()) -> Stream m a -> m ()
forall a b. (a -> b) -> a -> b
$ Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
D.fromStreamK (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a -> Stream m a
toStream t m a
m)
{-# RULES "drain fallback to CPS" [1]
forall a. D.drain (D.fromStreamK a) = K.drain a #-}
{-# INLINE_EARLY fromList #-}
fromList :: (Monad m, IsStream t) => [a] -> t m a
fromList :: [a] -> t m a
fromList = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamS (Stream m a -> t m a) -> ([a] -> Stream m a) -> [a] -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Stream m a
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
S.fromList
{-# RULES "fromList fallback to StreamK" [1]
forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
{-# INLINE toList #-}
toList :: (Monad m, IsStream t) => t m a -> m [a]
toList :: t m a -> m [a]
toList t m a
m = Stream m a -> m [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList (Stream m a -> m [a]) -> Stream m a -> m [a]
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE foldrM #-}
foldrM :: (Monad m, IsStream t) => (a -> m b -> m b) -> m b -> t m a -> m b
foldrM :: (a -> m b -> m b) -> m b -> t m a -> m b
foldrM a -> m b -> m b
step m b
acc t m a
m = (a -> m b -> m b) -> m b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
S.foldrM a -> m b -> m b
step m b
acc (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE foldrMx #-}
foldrMx :: (Monad m, IsStream t)
=> (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
foldrMx :: (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
foldrMx a -> m x -> m x
step m x
final m x -> m b
project t m a
m = (a -> m x -> m x) -> m x -> (m x -> m b) -> Stream m a -> m b
forall (m :: * -> *) a x b.
Monad m =>
(a -> m x -> m x) -> m x -> (m x -> m b) -> Stream m a -> m b
D.foldrMx a -> m x -> m x
step m x
final m x -> m b
project (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m
{-# INLINE foldr #-}
foldr :: (Monad m, IsStream t) => (a -> b -> b) -> b -> t m a -> m b
foldr :: (a -> b -> b) -> b -> t m a -> m b
foldr a -> b -> b
f b
z = (a -> m b -> m b) -> m b -> t 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
foldrM (\a
a m b
b -> m b
b m b -> (b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (b -> b) -> b -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> b
f a
a) (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
z)
{-# INLINE foldlMx' #-}
foldlMx' :: (IsStream t, Monad m)
=> (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
foldlMx' :: (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
foldlMx' x -> a -> m x
step m x
begin x -> m b
done t m a
m = (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
S.foldlMx' x -> a -> m x
step m x
begin x -> m b
done (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE parselMx' #-}
parselMx'
:: (IsStream t, MonadThrow m)
=> (s -> a -> m (Step s b))
-> m s
-> (s -> m b)
-> t m a
-> m b
parselMx' :: (s -> a -> m (Step s b)) -> m s -> (s -> m b) -> t m a -> m b
parselMx' s -> a -> m (Step s b)
step m s
initial s -> m b
extract t m a
m =
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Stream m a -> m b
forall (m :: * -> *) s a b.
MonadThrow m =>
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Stream m a -> m b
D.parselMx' s -> a -> m (Step s b)
step m s
initial s -> m b
extract (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m
{-# INLINE foldlx' #-}
foldlx' :: (IsStream t, Monad m)
=> (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
foldlx' :: (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
foldlx' x -> a -> x
step x
begin x -> b
done t m a
m = (x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
S.foldlx' x -> a -> x
step x
begin x -> b
done (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE foldl' #-}
foldl' :: (Monad m, IsStream t) => (b -> a -> b) -> b -> t m a -> m b
foldl' :: (b -> a -> b) -> b -> t m a -> m b
foldl' b -> a -> b
step b
begin t m a
m = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
S.foldl' b -> a -> b
step b
begin (Stream m a -> m b) -> Stream m a -> m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE foldlS #-}
foldlS :: IsStream t => (t m b -> a -> t m b) -> t m b -> t m a -> t m b
foldlS :: (t m b -> a -> t m b) -> t m b -> t m a -> t m b
foldlS = (t m b -> a -> t m b) -> t m b -> t m a -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) b a.
IsStream t =>
(t m b -> a -> t m b) -> t m b -> t m a -> t m b
K.foldlS
{-# INLINE foldlT #-}
foldlT :: (Monad m, IsStream t, Monad (s m), MonadTrans s)
=> (s m b -> a -> s m b) -> s m b -> t m a -> s m b
foldlT :: (s m b -> a -> s m b) -> s m b -> t m a -> s m b
foldlT s m b -> a -> s m b
f s m b
z t m a
s = (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
forall (m :: * -> *) (s :: (* -> *) -> * -> *) b a.
(Monad m, Monad (s m), MonadTrans s) =>
(s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
S.foldlT s m b -> a -> s m b
f s m b
z (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
s)
{-# INLINE runFold #-}
runFold :: (Monad m, IsStream t) => Fold m a b -> t m a -> m b
runFold :: Fold m a b -> t m a -> m b
runFold (Fold s -> a -> m s
step m s
begin s -> m b
done) = (s -> a -> m s) -> m s -> (s -> m b) -> t 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
foldlMx' s -> a -> m s
step m s
begin s -> m b
done
{-# INLINE postscanlMx' #-}
postscanlMx' :: (IsStream t, Monad m)
=> (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
postscanlMx' :: (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
postscanlMx' x -> a -> m x
step m x
begin x -> m b
done t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
D.postscanlMx' x -> a -> m x
step m x
begin x -> m b
done (Stream m a -> Stream m b) -> Stream m a -> Stream m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m
{-# INLINE postscanlx' #-}
postscanlx' :: (IsStream t, Monad m)
=> (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
postscanlx' :: (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
postscanlx' x -> a -> x
step x
begin x -> b
done t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
D.postscanlx' x -> a -> x
step x
begin x -> b
done (Stream m a -> Stream m b) -> Stream m a -> Stream m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m
{-# INLINE scanlMx' #-}
scanlMx' :: (IsStream t, Monad m)
=> (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
scanlMx' :: (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
scanlMx' x -> a -> m x
step m x
begin x -> m b
done t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
D.scanlMx' x -> a -> m x
step m x
begin x -> m b
done (Stream m a -> Stream m b) -> Stream m a -> Stream m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m
{-# INLINE scanlx' #-}
scanlx' :: (IsStream t, Monad m)
=> (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
scanlx' :: (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
scanlx' x -> a -> x
step x
begin x -> b
done t m a
m =
Stream m b -> t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
fromStreamS (Stream m b -> t m b) -> Stream m b -> t m b
forall a b. (a -> b) -> a -> b
$ (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
S.scanlx' x -> a -> x
step x
begin x -> b
done (Stream m a -> Stream m b) -> Stream m a -> Stream m b
forall a b. (a -> b) -> a -> b
$ t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m
{-# INLINE 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
f t m a
m1 t m b
m2 = (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
D.eqBy a -> b -> Bool
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m1) (t m b -> Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m b
m2)
{-# INLINE 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
f t m a
m1 t m b
m2 = (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
D.cmpBy a -> b -> Ordering
f (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m a
m1) (t m b -> Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD t m b
m2)
{-# INLINE minimum #-}
minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
minimum :: t m a -> m (Maybe a)
minimum t m a
m = Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Monad m, Ord a) =>
Stream m a -> m (Maybe a)
S.minimum (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m)
{-# INLINE maximum #-}
maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
maximum :: t m a -> m (Maybe a)
maximum t m a
m = Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Monad m, Ord a) =>
Stream m a -> m (Maybe a)
S.maximum (t m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
toStreamS t m a
m)
{-# INLINABLE foldWith #-}
foldWith :: (IsStream t, Foldable f)
=> (t m a -> t m a -> t m a) -> f (t m a) -> t m a
foldWith :: (t m a -> t m a -> t m a) -> f (t m a) -> t m a
foldWith t m a -> t m a -> t m a
f = (t m a -> t m a -> t m a) -> t m a -> f (t m a) -> t m a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr t m a -> t m a -> t m a
f t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil
{-# INLINABLE foldMapWith #-}
foldMapWith :: (IsStream t, Foldable f)
=> (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
foldMapWith :: (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
foldMapWith t m b -> t m b -> t m b
f a -> t m b
g = (a -> t m b -> t m b) -> t m b -> f a -> t m b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (t m b -> t m b -> t m b
f (t m b -> t m b -> t m b) -> (a -> t m b) -> a -> t m b -> t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> t m b
g) t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil
{-# INLINABLE forEachWith #-}
forEachWith :: (IsStream t, Foldable f)
=> (t m b -> t m b -> t m b) -> f a -> (a -> t m b) -> t m b
forEachWith :: (t m b -> t m b -> t m b) -> f a -> (a -> t m b) -> t m b
forEachWith t m b -> t m b -> t m b
f f a
xs a -> t m b
g = (a -> t m b -> t m b) -> t m b -> f a -> t m b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (t m b -> t m b -> t m b
f (t m b -> t m b -> t m b) -> (a -> t m b) -> a -> t m b -> t m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> t m b
g) t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil f a
xs