{-# OPTIONS_GHC -Wno-orphans #-}
module Streamly.Internal.Data.Stream.Common
(
fromList
, toList
, foldr
, foldl'
, fold
, eqBy
, cmpBy
)
where
#include "inline.hs"
import Streamly.Internal.Data.Fold.Type (Fold (..))
import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
import Prelude hiding (foldr, repeat)
{-# INLINE_EARLY fromList #-}
fromList :: Monad m => [a] -> K.StreamK m a
fromList :: forall (m :: * -> *) a. Monad m => [a] -> StreamK m a
fromList = forall (m :: * -> *) a. Monad m => Stream m a -> StreamK m a
D.toStreamK forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList
{-# RULES "fromList fallback to StreamK" [1]
forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
{-# INLINE toList #-}
toList :: Monad m => K.StreamK m a -> m [a]
toList :: forall (m :: * -> *) a. Monad m => StreamK m a -> m [a]
toList StreamK m a
m = forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
D.toList forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m
{-# INLINE foldrM #-}
foldrM :: Monad m => (a -> m b -> m b) -> m b -> K.StreamK m a -> m b
foldrM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> StreamK m a -> m b
foldrM a -> m b -> m b
step m b
acc StreamK m a
m = forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> Stream m a -> m b
D.foldrM a -> m b -> m b
step m b
acc forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m
{-# INLINE foldr #-}
foldr :: Monad m => (a -> b -> b) -> b -> K.StreamK m a -> m b
foldr :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> StreamK m a -> m b
foldr a -> b -> b
f b
z = forall (m :: * -> *) a b.
Monad m =>
(a -> m b -> m b) -> m b -> StreamK m a -> m b
foldrM (\a
a m b
b -> a -> b -> b
f a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
b) (forall (m :: * -> *) a. Monad m => a -> m a
return b
z)
{-# INLINE foldl' #-}
foldl' ::
Monad m => (b -> a -> b) -> b -> K.StreamK m a -> m b
foldl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> StreamK m a -> m b
foldl' b -> a -> b
step b
begin StreamK m a
m = forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
D.foldl' b -> a -> b
step b
begin forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m
{-# INLINE fold #-}
fold :: Monad m => Fold m a b -> K.StreamK m a -> m b
fold :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> StreamK m a -> m b
fold Fold m a b
fld StreamK m a
m = forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
D.fold Fold m a b
fld forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m
{-# INLINE eqBy #-}
eqBy :: Monad m =>
(a -> b -> Bool) -> K.StreamK m a -> K.StreamK m b -> m Bool
eqBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> StreamK m a -> StreamK m b -> m Bool
eqBy a -> b -> Bool
f StreamK m a
m1 StreamK m b
m2 = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
D.eqBy a -> b -> Bool
f (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m1) (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m b
m2)
{-# INLINE cmpBy #-}
cmpBy
:: Monad m
=> (a -> b -> Ordering) -> K.StreamK m a -> K.StreamK m b -> m Ordering
cmpBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> StreamK m a -> StreamK m b -> m Ordering
cmpBy a -> b -> Ordering
f StreamK m a
m1 StreamK m b
m2 = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
D.cmpBy a -> b -> Ordering
f (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m a
m1) (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
D.fromStreamK StreamK m b
m2)