{-# LANGUAGE UndecidableInstances #-}

#include "inline.hs"

-- |
-- Module      : Streamly.Internal.Data.Stream.StreamK
-- Copyright   : (c) 2017 Composewell Technologies
--
-- License     : BSD3
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
--
-- Continuation passing style (CPS) stream implementation. The symbol 'K' below
-- denotes a function as well as a Kontinuation.
--
-- @
-- import qualified Streamly.Internal.Data.Stream.StreamK as K
-- @
--
module Streamly.Internal.Data.Stream.StreamK
    (
    -- * The stream type
      Stream(..)

    -- * Construction Primitives
    , mkStream
    , nil
    , nilM
    , cons
    , (.:)

    -- * Elimination Primitives
    , foldStream
    , foldStreamShared

    -- * Transformation Primitives
    , unShare

    -- * Deconstruction
    , uncons

    -- * Generation
    -- ** Unfolds
    , unfoldr
    , unfoldrM

    -- ** Specialized Generation
    , repeat
    , repeatM
    , replicate
    , replicateM
    , fromIndices
    , fromIndicesM
    , iterate
    , iterateM

    -- ** Conversions
    , fromPure
    , fromEffect
    , fromFoldable
    , fromList

    -- * foldr/build
    , foldrS
    , foldrSM
    , buildS
    , augmentS

    -- * Elimination
    -- ** General Folds
    , foldr
    , foldr1
    , foldrM
    , foldrT

    , foldl'
    , foldlM'
    , foldlS
    , foldlT
    , foldlx'
    , foldlMx'
    , fold

    -- ** Specialized Folds
    , drain
    , null
    , head
    , tail
    , init
    , elem
    , notElem
    , all
    , any
    , last
    , minimum
    , minimumBy
    , maximum
    , maximumBy
    , findIndices
    , lookup
    , findM
    , find
    , (!!)

    -- ** Map and Fold
    , mapM_

    -- ** Conversions
    , toList
    , hoist

    -- * Transformation
    -- ** By folding (scans)
    , scanl'
    , scanlx'

    -- ** Filtering
    , filter
    , take
    , takeWhile
    , drop
    , dropWhile

    -- ** Mapping
    , map
    , mapM
    , sequence

    -- ** Inserting
    , intersperseM
    , intersperse
    , insertBy

    -- ** Deleting
    , deleteBy

    -- ** Reordering
    , reverse

    -- ** Map and Filter
    , mapMaybe

    -- ** Zipping
    , zipWith
    , zipWithM

    -- ** Merging
    , mergeBy
    , mergeByM

    -- ** Nesting
    , concatMapWith
    , concatMap
    , bindWith
    , concatPairsWith
    , apWith
    , apSerial
    , apSerialDiscardFst
    , apSerialDiscardSnd

    -- ** Transformation comprehensions
    , the

    -- * Semigroup Style Composition
    , serial

    -- * Utilities
    , consM
    , withLocal
    , mfix
    )
where

import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad (void, join)
import Streamly.Internal.Data.SVar.Type (adaptState, defState)

import qualified Streamly.Internal.Data.Fold.Type as FL

import Prelude
       hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
               take, filter, all, any, takeWhile, drop, dropWhile, minimum,
               maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
               foldr1, (!!), replicate, reverse, concatMap, iterate)

import Streamly.Internal.Data.Stream.StreamK.Type

-- $setup
-- >>> :m

-------------------------------------------------------------------------------
-- Generation
-------------------------------------------------------------------------------

{-# INLINE unfoldrM #-}
unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> Stream m a
unfoldrM :: (b -> m (Maybe (a, b))) -> b -> Stream m a
unfoldrM = (m a -> Stream m a -> Stream m a)
-> (b -> m (Maybe (a, b))) -> b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(m a -> Stream m a -> Stream m a)
-> (b -> m (Maybe (a, b))) -> b -> Stream m a
unfoldrMWith m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM
{-
-- Generalization of concurrent streams/SVar via unfoldr.
--
-- Unfold a value into monadic actions and then run the resulting monadic
-- actions to generate a stream. Since the step of generating the monadic
-- action and running them are decoupled we can run the monadic actions
-- cooncurrently. For example, the seed could be a list of monadic actions or a
-- pure stream of monadic actions.
--
-- We can have different flavors of this depending on the stream type t. The
-- concurrent version could be async or ahead etc. Depending on how we queue
-- back the feedback portion b, it could be DFS or BFS style.
--
unfoldrA :: (IsStream t, MonadAsync m) =>
    (b -> Maybe (m a, b)) -> b -> Stream m a
unfoldrA = undefined
-}

-------------------------------------------------------------------------------
-- Special generation
-------------------------------------------------------------------------------

repeatM :: Monad m => m a -> Stream m a
repeatM :: m a -> Stream m a
repeatM = (m a -> Stream m a -> Stream m a) -> m a -> Stream m a
forall (m :: * -> *) a (t :: (* -> *) -> * -> *).
(m a -> t m a -> t m a) -> m a -> t m a
repeatMWith m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM

{-# INLINE replicateM #-}
replicateM :: Monad m => Int -> m a -> Stream m a
replicateM :: Int -> m a -> Stream m a
replicateM = (m a -> Stream m a -> Stream m a) -> Int -> m a -> Stream m a
forall (m :: * -> *) a.
(m a -> Stream m a -> Stream m a) -> Int -> m a -> Stream m a
replicateMWith m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM
{-# INLINE replicate #-}
replicate :: Int -> a -> Stream m a
replicate :: Int -> a -> Stream m a
replicate Int
n a
a = Int -> Stream m a
forall t (m :: * -> *). (Ord t, Num t) => t -> Stream m a
go Int
n
    where
    go :: t -> Stream m a
go t
cnt = if t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 then Stream m a
forall (m :: * -> *) a. Stream m a
nil else a
a a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` t -> Stream m a
go (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- t
1)

{-# INLINE fromIndicesM #-}
fromIndicesM :: Monad m => (Int -> m a) -> Stream m a
fromIndicesM :: (Int -> m a) -> Stream m a
fromIndicesM = (m a -> Stream m a -> Stream m a) -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
(m a -> Stream m a -> Stream m a) -> (Int -> m a) -> Stream m a
fromIndicesMWith m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM
{-# INLINE fromIndices #-}
fromIndices :: (Int -> a) -> Stream m a
fromIndices :: (Int -> a) -> Stream m a
fromIndices Int -> a
gen = Int -> Stream m a
forall (m :: * -> *). Int -> Stream m a
go Int
0
  where
    go :: Int -> Stream m a
go Int
n = Int -> a
gen Int
n a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Int -> Stream m a
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

{-# INLINE iterate #-}
iterate :: (a -> a) -> a -> Stream m a
iterate :: (a -> a) -> a -> Stream m a
iterate a -> a
step = a -> Stream m a
forall (m :: * -> *). a -> Stream m a
go
    where
        go :: a -> Stream m a
go !a
s = a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons a
s (a -> Stream m a
go (a -> a
step a
s))

{-# INLINE iterateM #-}
iterateM :: Monad m => (a -> m a) -> m a -> Stream m a
iterateM :: (a -> m a) -> m a -> Stream m a
iterateM = (m a -> Stream m a -> Stream m a)
-> (a -> m a) -> m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(m a -> Stream m a -> Stream m a)
-> (a -> m a) -> m a -> Stream m a
iterateMWith m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM

-------------------------------------------------------------------------------
-- Conversions
-------------------------------------------------------------------------------

{-# INLINE fromList #-}
fromList :: [a] -> Stream m a
fromList :: [a] -> Stream m a
fromList = [a] -> Stream m a
forall (f :: * -> *) a (m :: * -> *).
Foldable f =>
f a -> Stream m a
fromFoldable

-------------------------------------------------------------------------------
-- Elimination by Folding
-------------------------------------------------------------------------------

-- | Lazy right associative fold.
{-# INLINE foldr #-}
foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
foldr :: (a -> b -> b) -> b -> Stream m a -> m b
foldr a -> b -> b
step b
acc = (a -> m b -> m b) -> m b -> Stream m a -> m b
forall a (m :: * -> *) b.
(a -> m b -> m b) -> m b -> Stream m a -> m b
foldrM (\a
x m b
xs -> m b
xs m b -> (b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> b
step a
x b
b)) (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
acc)

-- | Right associative fold to an arbitrary transformer monad.
{-# INLINE foldrT #-}
foldrT :: (Monad m, Monad (s m), MonadTrans s)
    => (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b
foldrT :: (a -> s m b -> s m b) -> s m b -> Stream m a -> s m b
foldrT a -> s m b -> s m b
step s m b
final = Stream m a -> s m b
go
  where
    go :: Stream m a -> s m b
go Stream m a
m1 = do
        Maybe (a, Stream m a)
res <- m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a)))
-> m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a))
forall a b. (a -> b) -> a -> b
$ Stream m a -> m (Maybe (a, Stream m a))
forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m a
m1
        case Maybe (a, Stream m a)
res of
            Just (a
h, Stream m a
t) -> a -> s m b -> s m b
step a
h (Stream m a -> s m b
go Stream m a
t)
            Maybe (a, Stream m a)
Nothing -> s m b
final

{-# INLINE foldr1 #-}
foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
foldr1 :: (a -> a -> a) -> Stream m a -> m (Maybe a)
foldr1 a -> a -> a
step Stream m a
m = do
    Maybe (a, Stream m a)
r <- Stream m a -> m (Maybe (a, Stream m a))
forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m a
m
    case Maybe (a, Stream m a)
r of
        Maybe (a, Stream 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, Stream m a
t) -> (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (a -> Stream m a -> m a
forall (m :: * -> *). Monad m => a -> Stream m a -> m a
go a
h Stream m a
t)
    where
    go :: a -> Stream m a -> m a
go a
p Stream m a
m1 =
        let stp :: m a
stp = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
p
            single :: a -> m a
single a
a = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ a -> a -> a
step a
a a
p
            yieldk :: a -> Stream m a -> m a
yieldk a
a Stream m a
r = (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> a -> a
step a
p) (a -> Stream m a -> m a
go a
a Stream m a
r)
         in State Stream m a
-> (a -> Stream m a -> m a)
-> (a -> m a)
-> m a
-> Stream m a
-> m a
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m a
yieldk a -> m a
forall (m :: * -> *). Monad m => a -> m a
single m a
stp Stream m a
m1

-- XXX replace the recursive "go" with explicit continuations.
-- | Like 'foldx', but with a monadic step function.
{-# INLINABLE foldlMx' #-}
foldlMx' :: Monad m
    => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
foldlMx' :: (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> m b
foldlMx' x -> a -> m x
step m x
begin x -> m b
done = m x -> Stream m a -> m b
go m x
begin
    where
    go :: m x -> Stream m a -> m b
go !m x
acc Stream m a
m1 =
        let stop :: m b
stop = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
done
            single :: a -> m b
single a
a = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
b -> x -> a -> m x
step x
b a
a m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
done
            yieldk :: a -> Stream m a -> m b
yieldk a
a Stream m a
r = m x
acc m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
b -> x -> a -> m x
step x
b a
a m x -> (x -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
x -> m x -> Stream m a -> m b
go (x -> m x
forall (m :: * -> *) a. Monad m => a -> m a
return x
x) Stream m a
r
         in State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m b
yieldk a -> m b
single m b
stop Stream m a
m1

{-# INLINABLE fold #-}
fold :: Monad m => FL.Fold m a b -> Stream m a -> m b
fold :: Fold m a b -> Stream m a -> m b
fold (FL.Fold s -> a -> m (Step s b)
step m (Step s b)
begin s -> m b
done) Stream m a
m = do
    Step s b
res <- m (Step s b)
begin
    case Step s b
res of
        FL.Partial s
fs -> s -> Stream m a -> m b
go s
fs Stream m a
m
        FL.Done b
fb -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
fb

    where
    go :: s -> Stream m a -> m b
go !s
acc Stream m a
m1 =
        let stop :: m b
stop = s -> m b
done s
acc
            single :: a -> m b
single a
a = s -> a -> m (Step s b)
step s
acc a
a
              m (Step s b) -> (Step s b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                        FL.Partial s
s -> s -> m b
done s
s
                        FL.Done b
b1 -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b1
            yieldk :: a -> Stream m a -> m b
yieldk a
a Stream m a
r = s -> a -> m (Step s b)
step s
acc a
a
              m (Step s b) -> (Step s b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                        FL.Partial s
s -> s -> Stream m a -> m b
go s
s Stream m a
r
                        FL.Done b
b1 -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b1
         in State Stream m a
-> (a -> Stream m a -> m b)
-> (a -> m b)
-> m b
-> Stream m a
-> m b
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m b
yieldk a -> m b
single m b
stop Stream m a
m1

-- | Like 'foldl'' but with a monadic step function.
{-# INLINE foldlM' #-}
foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
foldlM' :: (b -> a -> m b) -> m b -> Stream m a -> m b
foldlM' b -> a -> m b
step m b
begin = (b -> a -> m b) -> m b -> (b -> 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
foldlMx' b -> a -> m b
step m b
begin b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | Lazy left fold to an arbitrary transformer monad.
{-# INLINE foldlT #-}
foldlT :: (Monad m, Monad (s m), MonadTrans s)
    => (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
foldlT :: (s m b -> a -> s m b) -> s m b -> Stream m a -> s m b
foldlT s m b -> a -> s m b
step = s m b -> Stream m a -> s m b
go
  where
    go :: s m b -> Stream m a -> s m b
go s m b
acc Stream m a
m1 = do
        Maybe (a, Stream m a)
res <- m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a)))
-> m (Maybe (a, Stream m a)) -> s m (Maybe (a, Stream m a))
forall a b. (a -> b) -> a -> b
$ Stream m a -> m (Maybe (a, Stream m a))
forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m a
m1
        case Maybe (a, Stream m a)
res of
            Just (a
h, Stream m a
t) -> s m b -> Stream m a -> s m b
go (s m b -> a -> s m b
step s m b
acc a
h) Stream m a
t
            Maybe (a, Stream m a)
Nothing -> s m b
acc

------------------------------------------------------------------------------
-- Specialized folds
------------------------------------------------------------------------------

{-# INLINE head #-}
head :: Monad m => Stream m a -> m (Maybe a)
-- head = foldrM (\x _ -> return $ Just x) (return Nothing)
head :: Stream m a -> m (Maybe a)
head Stream m a
m =
    let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
        yieldk :: a -> p -> m (Maybe a)
yieldk a
a p
_ = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
    in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a p. Monad m => a -> p -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop Stream m a
m

{-# INLINE elem #-}
elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
elem :: a -> Stream m a -> m Bool
elem a
e = Stream m a -> m Bool
forall (m :: * -> *). Monad m => Stream m a -> m Bool
go
    where
    go :: Stream m a -> m Bool
go Stream m a
m1 =
        let stop :: m Bool
stop      = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            single :: a -> m Bool
single a
a  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e)
            yieldk :: a -> Stream m a -> m Bool
yieldk a
a Stream m a
r = if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e then Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True else Stream m a -> m Bool
go Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> Stream m a
-> m Bool
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single m Bool
stop Stream m a
m1

{-# INLINE notElem #-}
notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
notElem :: a -> Stream m a -> m Bool
notElem a
e = Stream m a -> m Bool
forall (m :: * -> *). Monad m => Stream m a -> m Bool
go
    where
    go :: Stream m a -> m Bool
go Stream m a
m1 =
        let stop :: m Bool
stop      = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
            single :: a -> m Bool
single a
a  = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
e)
            yieldk :: a -> Stream m a -> m Bool
yieldk a
a Stream m a
r = if a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e then Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False else Stream m a -> m Bool
go Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> Stream m a
-> m Bool
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single m Bool
stop Stream m a
m1

{-# INLINABLE all #-}
all :: Monad m => (a -> Bool) -> Stream m a -> m Bool
all :: (a -> Bool) -> Stream m a -> m Bool
all a -> Bool
p = Stream m a -> m Bool
forall (m :: * -> *). Monad m => Stream m a -> m Bool
go
    where
    go :: Stream m a -> m Bool
go Stream m a
m1 =
        let single :: a -> m Bool
single a
a   | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            yieldk :: a -> Stream m a -> m Bool
yieldk a
a Stream m a
r | a -> Bool
p a
a       = Stream m a -> m Bool
go Stream m a
r
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
         in State Stream m a
-> (a -> Stream m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> Stream m a
-> m Bool
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True) Stream m a
m1

{-# INLINABLE any #-}
any :: Monad m => (a -> Bool) -> Stream m a -> m Bool
any :: (a -> Bool) -> Stream m a -> m Bool
any a -> Bool
p = Stream m a -> m Bool
forall (m :: * -> *). Monad m => Stream m a -> m Bool
go
    where
    go :: Stream m a -> m Bool
go Stream m a
m1 =
        let single :: a -> m Bool
single a
a   | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            yieldk :: a -> Stream m a -> m Bool
yieldk a
a Stream m a
r | a -> Bool
p a
a       = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise = Stream m a -> m Bool
go Stream m a
r
         in State Stream m a
-> (a -> Stream m a -> m Bool)
-> (a -> m Bool)
-> m Bool
-> Stream m a
-> m Bool
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m Bool
yieldk a -> m Bool
forall (m :: * -> *). Monad m => a -> m Bool
single (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False) Stream m a
m1

-- | Extract the last element of the stream, if any.
{-# INLINE last #-}
last :: Monad m => Stream m a -> m (Maybe a)
last :: Stream m a -> m (Maybe a)
last = (Maybe a -> a -> Maybe a)
-> Maybe a -> (Maybe a -> Maybe a) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a b x.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> m b
foldlx' (\Maybe a
_ a
y -> a -> Maybe a
forall a. a -> Maybe a
Just a
y) Maybe a
forall a. Maybe a
Nothing Maybe a -> Maybe a
forall a. a -> a
id

{-# INLINE minimum #-}
minimum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
minimum :: Stream m a -> m (Maybe a)
minimum = Maybe a -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Ord a, Monad m) =>
Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing
    where
    go :: Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
Nothing Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop Stream m a
m1

    go (Just a
res) Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
                else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) Stream m a
r
                else Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop Stream m a
m1

{-# INLINE minimumBy #-}
minimumBy
    :: (Monad m)
    => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
minimumBy :: (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
minimumBy a -> a -> Ordering
cmp = Maybe a -> Stream m a -> m (Maybe a)
forall (m :: * -> *).
Monad m =>
Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing
    where
    go :: Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
Nothing Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop Stream m a
m1

    go (Just a
res) Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
                Ordering
_  -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
                Ordering
_  -> Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop Stream m a
m1

{-# INLINE maximum #-}
maximum :: (Monad m, Ord a) => Stream m a -> m (Maybe a)
maximum :: Stream m a -> m (Maybe a)
maximum = Maybe a -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Ord a, Monad m) =>
Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing
    where
    go :: Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
Nothing Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop Stream m a
m1

    go (Just a
res) Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
                else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r =
                if a
res a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
a
                then Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
                else Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop Stream m a
m1

{-# INLINE maximumBy #-}
maximumBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
maximumBy :: (a -> a -> Ordering) -> Stream m a -> m (Maybe a)
maximumBy a -> a -> Ordering
cmp = Maybe a -> Stream m a -> m (Maybe a)
forall (m :: * -> *).
Monad m =>
Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
forall a. Maybe a
Nothing
    where
    go :: Maybe a -> Stream m a -> m (Maybe a)
go Maybe a
Nothing Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            single :: a -> m (Maybe a)
single a
a  = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single m (Maybe a)
forall a. m (Maybe a)
stop Stream m a
m1

    go (Just a
res) Stream m a
m1 =
        let stop :: m (Maybe a)
stop      = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
            single :: a -> m (Maybe a)
single a
a  = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
res)
                Ordering
_  -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r = case a -> a -> Ordering
cmp a
res a
a of
                Ordering
GT -> Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
res) Stream m a
r
                Ordering
_  -> Maybe a -> Stream m a -> m (Maybe a)
go (a -> Maybe a
forall a. a -> Maybe a
Just a
a) Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single m (Maybe a)
stop Stream m a
m1

{-# INLINE (!!) #-}
(!!) :: Monad m => Stream m a -> Int -> m (Maybe a)
Stream m a
m !! :: Stream m a -> Int -> m (Maybe a)
!! Int
i = Int -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a a.
(Ord a, Monad m, Num a) =>
a -> Stream m a -> m (Maybe a)
go Int
i Stream m a
m
    where
    go :: a -> Stream m a -> m (Maybe a)
go a
n Stream m a
m1 =
      let single :: a -> m (Maybe a)
single a
a | a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = 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
a
                   | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
          yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
x | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
                     | a
n a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = 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
a
                     | Bool
otherwise = a -> Stream m a -> m (Maybe a)
go (a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) Stream m a
x
      in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) Stream m a
m1

{-# INLINE lookup #-}
lookup :: (Monad m, Eq a) => a -> Stream m (a, b) -> m (Maybe b)
lookup :: a -> Stream m (a, b) -> m (Maybe b)
lookup a
e = Stream m (a, b) -> m (Maybe b)
forall (m :: * -> *) a. Monad m => Stream m (a, a) -> m (Maybe a)
go
    where
    go :: Stream m (a, a) -> m (Maybe a)
go Stream m (a, a)
m1 =
        let single :: (a, a) -> m (Maybe a)
single (a
a, a
b) | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e = 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
b
                          | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: (a, a) -> Stream m (a, a) -> m (Maybe a)
yieldk (a
a, a
b) Stream m (a, a)
x | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
e = 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
b
                            | Bool
otherwise = Stream m (a, a) -> m (Maybe a)
go Stream m (a, a)
x
        in State Stream m (a, a)
-> ((a, a) -> Stream m (a, a) -> m (Maybe a))
-> ((a, a) -> m (Maybe a))
-> m (Maybe a)
-> Stream m (a, a)
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m (a, a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState (a, a) -> Stream m (a, a) -> m (Maybe a)
yieldk (a, a) -> m (Maybe a)
forall (m :: * -> *) a. Monad m => (a, a) -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) Stream m (a, a)
m1

{-# INLINE findM #-}
findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
findM :: (a -> m Bool) -> Stream m a -> m (Maybe a)
findM a -> m Bool
p = Stream m a -> m (Maybe a)
go
    where
    go :: Stream m a -> m (Maybe a)
go Stream m a
m1 =
        let single :: a -> m (Maybe a)
single a
a = do
                Bool
b <- a -> m Bool
p a
a
                if Bool
b then 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
a else Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
x = do
                Bool
b <- a -> m Bool
p a
a
                if Bool
b then 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
a else Stream m a -> m (Maybe a)
go Stream m a
x
        in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
single (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing) Stream m a
m1

{-# INLINE find #-}
find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
find :: (a -> Bool) -> Stream m a -> m (Maybe a)
find a -> Bool
p = (a -> m Bool) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
findM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)

{-# INLINE findIndices #-}
findIndices :: (a -> Bool) -> Stream m a -> Stream m Int
findIndices :: (a -> Bool) -> Stream m a -> Stream m Int
findIndices a -> Bool
p = Int -> Stream m a -> Stream m Int
forall a (m :: * -> *). Num a => a -> Stream m a -> Stream m a
go Int
0
    where
    go :: a -> Stream m a -> Stream m a
go a
offset Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a | a -> Bool
p a
a = a -> m r
sng a
offset
                     | Bool
otherwise = m r
stp
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
x | a -> Bool
p a
a = a -> Stream m a -> m r
yld a
offset (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> Stream m a -> Stream m a
go (a
offset a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) Stream m a
x
                       | Bool
otherwise = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$
                            a -> Stream m a -> Stream m a
go (a
offset a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) Stream m a
x
        in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

------------------------------------------------------------------------------
-- Map and Fold
------------------------------------------------------------------------------

-- | Apply a monadic action to each element of the stream and discard the
-- output of the action.
{-# INLINE mapM_ #-}
mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
mapM_ :: (a -> m b) -> Stream m a -> m ()
mapM_ a -> m b
f = Stream m a -> m ()
go
    where
    go :: Stream m a -> m ()
go Stream m a
m1 =
        let stop :: m ()
stop = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            single :: a -> m ()
single a
a = m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (a -> m b
f a
a)
            yieldk :: a -> Stream m a -> m ()
yieldk a
a Stream m a
r = a -> m b
f a
a m b -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream m a -> m ()
go Stream m a
r
         in State Stream m a
-> (a -> Stream m a -> m ())
-> (a -> m ())
-> m ()
-> Stream m a
-> m ()
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m ()
yieldk a -> m ()
single m ()
stop Stream m a
m1

{-# INLINE mapM #-}
mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
mapM :: (a -> m b) -> Stream m a -> Stream m b
mapM = (m b -> Stream m b -> Stream m b)
-> (a -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
(m b -> Stream m b -> Stream m b)
-> (a -> m b) -> Stream m a -> Stream m b
mapMWith m b -> Stream m b -> Stream m b
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
consM

------------------------------------------------------------------------------
-- Converting folds
------------------------------------------------------------------------------

{-# INLINABLE toList #-}
toList :: Monad m => Stream m a -> m [a]
toList :: Stream m a -> m [a]
toList = (a -> [a] -> [a]) -> [a] -> Stream m a -> m [a]
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
foldr (:) []

-- Based on suggestions by David Feuer and Pranay Sashank
{-# INLINE hoist #-}
hoist :: (Monad m, Monad n)
    => (forall x. m x -> n x) -> Stream m a -> Stream n a
hoist :: (forall x. m x -> n x) -> Stream m a -> Stream n a
hoist forall x. m x -> n x
f Stream m a
str =
    (forall r.
 State Stream n a
 -> (a -> Stream n a -> n r) -> (a -> n r) -> n r -> n r)
-> Stream n a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream n a
  -> (a -> Stream n a -> n r) -> (a -> n r) -> n r -> n r)
 -> Stream n a)
-> (forall r.
    State Stream n a
    -> (a -> Stream n a -> n r) -> (a -> n r) -> n r -> n r)
-> Stream n a
forall a b. (a -> b) -> a -> b
$ \State Stream n a
st a -> Stream n a -> n r
yld a -> n r
sng n r
stp ->
            let single :: a -> m (n r)
single = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return (n r -> m (n r)) -> (a -> n r) -> a -> m (n r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> n r
sng
                yieldk :: a -> Stream m a -> m (n r)
yieldk a
a Stream m a
s = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return (n r -> m (n r)) -> n r -> m (n r)
forall a b. (a -> b) -> a -> b
$ a -> Stream n a -> n r
yld a
a ((forall x. m x -> n x) -> Stream m a -> Stream n a
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Stream m a -> Stream n a
hoist forall x. m x -> n x
f Stream m a
s)
                stop :: m (n r)
stop = n r -> m (n r)
forall (m :: * -> *) a. Monad m => a -> m a
return n r
stp
                state :: State Stream n b
state = State Stream n a -> State Stream n b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream n a
st
             in n (n r) -> n r
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (n (n r) -> n r) -> (m (n r) -> n (n r)) -> m (n r) -> n r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (n r) -> n (n r)
forall x. m x -> n x
f (m (n r) -> n r) -> m (n r) -> n r
forall a b. (a -> b) -> a -> b
$ State Stream m a
-> (a -> Stream m a -> m (n r))
-> (a -> m (n r))
-> m (n r)
-> Stream m a
-> m (n r)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
forall (n :: * -> *) b. State Stream n b
state a -> Stream m a -> m (n r)
forall (m :: * -> *). Monad m => a -> Stream m a -> m (n r)
yieldk a -> m (n r)
single m (n r)
stop Stream m a
str

-------------------------------------------------------------------------------
-- Transformation by folding (Scans)
-------------------------------------------------------------------------------

{-# INLINE scanlx' #-}
scanlx' :: (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
scanlx' :: (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
scanlx' x -> a -> x
step x
begin x -> b
done Stream m a
m =
    b -> Stream m b -> Stream m b
forall a (m :: * -> *). a -> Stream m a -> Stream m a
cons (x -> b
done x
begin) (Stream m b -> Stream m b) -> Stream m b -> Stream m b
forall a b. (a -> b) -> a -> b
$ Stream m a -> x -> Stream m b
forall (m :: * -> *). Stream m a -> x -> Stream m b
go Stream m a
m x
begin
    where
    go :: Stream m a -> x -> Stream m b
go Stream m a
m1 !x
acc = (forall r.
 State Stream m b
 -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m b
  -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
 -> Stream m b)
-> (forall r.
    State Stream m b
    -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = b -> m r
sng (x -> b
done (x -> b) -> x -> b
forall a b. (a -> b) -> a -> b
$ x -> a -> x
step x
acc a
a)
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r =
                let s :: x
s = x -> a -> x
step x
acc a
a
                in b -> Stream m b -> m r
yld (x -> b
done x
s) (Stream m a -> x -> Stream m b
go Stream m a
r x
s)
        in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

{-# INLINE scanl' #-}
scanl' :: (b -> a -> b) -> b -> Stream m a -> Stream m b
scanl' :: (b -> a -> b) -> b -> Stream m a -> Stream m b
scanl' b -> a -> b
step b
begin = (b -> a -> b) -> b -> (b -> b) -> Stream m a -> Stream m b
forall x a b (m :: * -> *).
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
scanlx' b -> a -> b
step b
begin b -> b
forall a. a -> a
id

-------------------------------------------------------------------------------
-- Filtering
-------------------------------------------------------------------------------

{-# INLINE filter #-}
filter :: (a -> Bool) -> Stream m a -> Stream m a
filter :: (a -> Bool) -> Stream m a -> Stream m a
filter a -> Bool
p = Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a
go
    where
    go :: Stream m a -> Stream m a
go Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = a -> m r
sng a
a
                       | Bool
otherwise = m r
stp
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r | a -> Bool
p a
a       = a -> Stream m a -> m r
yld a
a (Stream m a -> Stream m a
go Stream m a
r)
                       | Bool
otherwise = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
r
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

{-# INLINE take #-}
take :: Int -> Stream m a -> Stream m a
take :: Int -> Stream m a -> Stream m a
take = Int -> Stream m a -> Stream m a
forall p (m :: * -> *) a.
(Ord p, Num p) =>
p -> Stream m a -> Stream m a
go
    where
    go :: p -> Stream m a -> Stream m a
go p
n1 Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = a -> Stream m a -> m r
yld a
a (p -> Stream m a -> Stream m a
go (p
n1 p -> p -> p
forall a. Num a => a -> a -> a
- p
1) Stream m a
r)
        in if p
n1 p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= p
0
           then m r
stp
           else State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
sng m r
stp Stream m a
m1

{-# INLINE takeWhile #-}
takeWhile :: (a -> Bool) -> Stream m a -> Stream m a
takeWhile :: (a -> Bool) -> Stream m a -> Stream m a
takeWhile a -> Bool
p = Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a
go
    where
    go :: Stream m a -> Stream m a
go Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = a -> m r
sng a
a
                       | Bool
otherwise = m r
stp
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r | a -> Bool
p a
a       = a -> Stream m a -> m r
yld a
a (Stream m a -> Stream m a
go Stream m a
r)
                       | Bool
otherwise = m r
stp
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

{-# INLINE drop #-}
drop :: Int -> Stream m a -> Stream m a
drop :: Int -> Stream m a -> Stream m a
drop Int
n Stream m a
m = Stream m a -> Stream m a
forall (m :: * -> *) a. Stream m a -> Stream m a
unShare (Int -> Stream m a -> Stream m a
forall p (m :: * -> *) a.
(Ord p, Num p) =>
p -> Stream m a -> Stream m a
go Int
n Stream m a
m)
    where
    go :: p -> Stream m a -> Stream m a
go p
n1 Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: p -> m r
single p
_ = m r
stp
            yieldk :: p -> Stream m a -> m r
yieldk p
_ Stream m a
r = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ p -> Stream m a -> Stream m a
go (p
n1 p -> p -> p
forall a. Num a => a -> a -> a
- p
1) Stream m a
r
        -- Somehow "<=" check performs better than a ">"
        in if p
n1 p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= p
0
           then State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
m1
           else State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
forall p. p -> Stream m a -> m r
yieldk a -> m r
forall p. p -> m r
single m r
stp Stream m a
m1

{-# INLINE dropWhile #-}
dropWhile :: (a -> Bool) -> Stream m a -> Stream m a
dropWhile :: (a -> Bool) -> Stream m a -> Stream m a
dropWhile a -> Bool
p = Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a
go
    where
    go :: Stream m a -> Stream m a
go Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a   | a -> Bool
p a
a       = m r
stp
                       | Bool
otherwise = a -> m r
sng a
a
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r | a -> Bool
p a
a = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
r
                       | Bool
otherwise = a -> Stream m a -> m r
yld a
a Stream m a
r
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

-------------------------------------------------------------------------------
-- Mapping
-------------------------------------------------------------------------------

-- Be careful when modifying this, this uses a consM (|:) deliberately to allow
-- other stream types to overload it.
{-# INLINE sequence #-}
sequence :: Monad m => Stream m (m a) -> Stream m a
sequence :: Stream m (m a) -> Stream m a
sequence = Stream m (m a) -> Stream m a
forall (m :: * -> *) a. Monad m => Stream m (m a) -> Stream m a
go
    where
    go :: Stream m (m a) -> Stream m a
go Stream m (m a)
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: m a -> m r
single m a
ma = m a
ma m a -> (a -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m r
sng
            yieldk :: m a -> Stream m (m a) -> m r
yieldk m a
ma Stream m (m a)
r = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
ma m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` Stream m (m a) -> Stream m a
go Stream m (m a)
r
         in State Stream m (m a)
-> (m a -> Stream m (m a) -> m r)
-> (m a -> m r)
-> m r
-> Stream m (m a)
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m a -> State Stream m (m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m a
st) m a -> Stream m (m a) -> m r
yieldk m a -> m r
single m r
stp Stream m (m a)
m1

-------------------------------------------------------------------------------
-- Inserting
-------------------------------------------------------------------------------

{-# INLINE intersperseM #-}
intersperseM :: Monad m => m a -> Stream m a -> Stream m a
intersperseM :: m a -> Stream m a -> Stream m a
intersperseM m a
a = Stream m a -> Stream m a
prependingStart
    where
    prependingStart :: Stream m a -> Stream m a
prependingStart Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let yieldk :: a -> Stream m a -> m r
yieldk a
i Stream m a
x =
                State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` Stream m a -> Stream m a
go Stream m a
x
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
sng m r
stp Stream m a
m1
    go :: Stream m a -> Stream m a
go Stream m a
m2 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
i = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
a m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
i
            yieldk :: a -> Stream m a -> m r
yieldk a
i Stream m a
x =
                State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStreamShared
                    State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (Stream m a -> m r) -> Stream m a -> m r
forall a b. (a -> b) -> a -> b
$ m a
a m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
`consM` Stream m a -> Stream m a
go Stream m a
x
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m2

{-# INLINE intersperse #-}
intersperse :: Monad m => a -> Stream m a -> Stream m a
intersperse :: a -> Stream m a -> Stream m a
intersperse a
a = m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
intersperseM (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a)

{-# INLINE insertBy #-}
insertBy :: (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
insertBy :: (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
insertBy a -> a -> Ordering
cmp a
x = Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a
go
  where
    go :: Stream m a -> Stream m a
go Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
_ m r
_ ->
        let single :: a -> m r
single a
a = case a -> a -> Ordering
cmp a
x a
a of
                Ordering
GT -> a -> Stream m a -> m r
yld a
a (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
x)
                Ordering
_  -> a -> Stream m a -> m r
yld a
x (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
a)
            stop :: m r
stop = a -> Stream m a -> m r
yld a
x Stream m a
forall (m :: * -> *) a. Stream m a
nil
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = case a -> a -> Ordering
cmp a
x a
a of
                Ordering
GT -> a -> Stream m a -> m r
yld a
a (Stream m a -> Stream m a
go Stream m a
r)
                Ordering
_  -> a -> Stream m a -> m r
yld a
x (a
a a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
r)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stop Stream m a
m1

------------------------------------------------------------------------------
-- Deleting
------------------------------------------------------------------------------

{-# INLINE deleteBy #-}
deleteBy :: (a -> a -> Bool) -> a -> Stream m a -> Stream m a
deleteBy :: (a -> a -> Bool) -> a -> Stream m a -> Stream m a
deleteBy a -> a -> Bool
eq a
x = Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a
go
  where
    go :: Stream m a -> Stream m a
go Stream m a
m1 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = if a -> a -> Bool
eq a
x a
a then m r
stp else a -> m r
sng a
a
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = if a -> a -> Bool
eq a
x a
a
              then State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
r
              else a -> Stream m a -> m r
yld a
a (Stream m a -> Stream m a
go Stream m a
r)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

-------------------------------------------------------------------------------
-- Map and Filter
-------------------------------------------------------------------------------

{-# INLINE mapMaybe #-}
mapMaybe :: (a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe :: (a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe a -> Maybe b
f = Stream m a -> Stream m b
forall (m :: * -> *). Stream m a -> Stream m b
go
  where
    go :: Stream m a -> Stream m b
go Stream m a
m1 = (forall r.
 State Stream m b
 -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m b
  -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
 -> Stream m b)
-> (forall r.
    State Stream m b
    -> (b -> Stream m b -> m r) -> (b -> m r) -> m r -> m r)
-> Stream m b
forall a b. (a -> b) -> a -> b
$ \State Stream m b
st b -> Stream m b -> m r
yld b -> m r
sng m r
stp ->
        let single :: a -> m r
single a
a = m r -> (b -> m r) -> Maybe b -> m r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m r
stp b -> m r
sng (a -> Maybe b
f a
a)
            yieldk :: a -> Stream m a -> m r
yieldk a
a Stream m a
r = case a -> Maybe b
f a
a of
                Just b
b  -> b -> Stream m b -> m r
yld b
b (Stream m b -> m r) -> Stream m b -> m r
forall a b. (a -> b) -> a -> b
$ Stream m a -> Stream m b
go Stream m a
r
                Maybe b
Nothing -> State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
r
        in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m b -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m b
st) a -> Stream m a -> m r
yieldk a -> m r
single m r
stp Stream m a
m1

------------------------------------------------------------------------------
-- Serial Zipping
------------------------------------------------------------------------------

-- | Zip two streams serially using a pure zipping function.
--
-- @since 0.1.0
{-# INLINE zipWith #-}
zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith a -> b -> c
f = Stream m a -> Stream m b -> Stream m c
forall (m :: * -> *). Stream m a -> Stream m b -> Stream m c
go

    where

    go :: Stream m a -> Stream m b -> Stream m c
go Stream m a
mx Stream m b
my = (forall r.
 State Stream m c
 -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
-> Stream m c
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m c
  -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
 -> Stream m c)
-> (forall r.
    State Stream m c
    -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
-> Stream m c
forall a b. (a -> b) -> a -> b
$ \State Stream m c
st c -> Stream m c -> m r
yld c -> m r
sng m r
stp -> do
        let merge :: a -> Stream m a -> m r
merge a
a Stream m a
ra =
                let single2 :: b -> m r
single2 b
b = c -> m r
sng (a -> b -> c
f a
a b
b)
                    yield2 :: b -> Stream m b -> m r
yield2 b
b Stream m b
rb = c -> Stream m c -> m r
yld (a -> b -> c
f a
a b
b) (Stream m a -> Stream m b -> Stream m c
go Stream m a
ra Stream m b
rb)
                 in State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m c -> State Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) b -> Stream m b -> m r
yield2 b -> m r
single2 m r
stp Stream m b
my
        let single1 :: a -> m r
single1 a
a = a -> Stream m a -> m r
merge a
a Stream m a
forall (m :: * -> *) a. Stream m a
nil
            yield1 :: a -> Stream m a -> m r
yield1 = a -> Stream m a -> m r
merge
        State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m c -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) a -> Stream m a -> m r
yield1 a -> m r
single1 m r
stp Stream m a
mx

-- | Zip two streams serially using a monadic zipping function.
--
-- @since 0.1.0
{-# INLINE zipWithM #-}
zipWithM :: Monad m =>
    (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM :: (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f = Stream m a -> Stream m b -> Stream m c
go

    where

    go :: Stream m a -> Stream m b -> Stream m c
go Stream m a
mx Stream m b
my = (forall r.
 State Stream m c
 -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
-> Stream m c
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m c
  -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
 -> Stream m c)
-> (forall r.
    State Stream m c
    -> (c -> Stream m c -> m r) -> (c -> m r) -> m r -> m r)
-> Stream m c
forall a b. (a -> b) -> a -> b
$ \State Stream m c
st c -> Stream m c -> m r
yld c -> m r
sng m r
stp -> do
        let merge :: a -> Stream m a -> m r
merge a
a Stream m a
ra =
                let single2 :: b -> m r
single2 b
b   = a -> b -> m c
f a
a b
b m c -> (c -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> m r
sng
                    yield2 :: b -> Stream m b -> m r
yield2 b
b Stream m b
rb = a -> b -> m c
f a
a b
b m c -> (c -> m r) -> m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
x -> c -> Stream m c -> m r
yld c
x (Stream m a -> Stream m b -> Stream m c
go Stream m a
ra Stream m b
rb)
                 in State Stream m b
-> (b -> Stream m b -> m r)
-> (b -> m r)
-> m r
-> Stream m b
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m c -> State Stream m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) b -> Stream m b -> m r
yield2 b -> m r
single2 m r
stp Stream m b
my
        let single1 :: a -> m r
single1 a
a = a -> Stream m a -> m r
merge a
a Stream m a
forall (m :: * -> *) a. Stream m a
nil
            yield1 :: a -> Stream m a -> m r
yield1 = a -> Stream m a -> m r
merge
        State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream (State Stream m c -> State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State Stream m c
st) a -> Stream m a -> m r
yield1 a -> m r
single1 m r
stp Stream m a
mx

------------------------------------------------------------------------------
-- Merging
------------------------------------------------------------------------------

{-# INLINE mergeByM #-}
mergeByM :: Monad m =>
    (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
mergeByM :: (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
mergeByM a -> a -> m Ordering
cmp = Stream m a -> Stream m a -> Stream m a
go

    where

    go :: Stream m a -> Stream m a -> Stream m a
go Stream m a
mx Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp -> do
        let stop :: m r
stop = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
my
            single :: a -> m r
single a
x = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
rx Stream m a
my)
        State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

    goX0 :: a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
_ -> do
        let stop :: m r
stop = a -> m r
sng a
x
            single :: a -> m r
single a
y = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
x)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
y)
            yield :: a -> Stream m a -> m r
yield a
y Stream m a
ry = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a
goX0 a
x Stream m a
ry)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a
y a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
ry)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
my

    goX :: a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
mx Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> do
        let stop :: m r
stop = a -> Stream m a -> m r
yld a
x Stream m a
mx
            single :: a -> m r
single a
y = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a
x a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
mx)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a
goY0 Stream m a
mx a
y)
            yield :: a -> Stream m a -> m r
yield a
y Stream m a
ry = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
mx Stream m a
ry)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
mx a
y Stream m a
ry)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
my

    goY0 :: Stream m a -> a -> Stream m a
goY0 Stream m a
mx a
y = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
_ -> do
        let stop :: m r
stop = a -> m r
sng a
y
            single :: a -> m r
single a
x = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
x)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
y)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a
x a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
rx)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a
goY0 Stream m a
rx a
y)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

    goY :: Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
mx a
y Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> do
        let stop :: m r
stop = a -> Stream m a -> m r
yld a
y Stream m a
my
            single :: a -> m r
single a
x = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a
y a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
my)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = do
                Ordering
r <- a -> a -> m Ordering
cmp a
x a
y
                case Ordering
r of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
rx Stream m a
my)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
rx a
y Stream m a
my)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

{-# INLINE mergeBy #-}
mergeBy :: (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
-- XXX GHC: This has slightly worse performance than replacing "r <- cmp x y"
-- with "let r = cmp x y" in the monadic version. The definition below is
-- exactly the same as mergeByM except this change.
-- mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
mergeBy :: (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
mergeBy a -> a -> Ordering
cmp = Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *). Stream m a -> Stream m a -> Stream m a
go

    where

    go :: Stream m a -> Stream m a -> Stream m a
go Stream m a
mx Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp -> do
        let stop :: m r
stop = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
my
            single :: a -> m r
single a
x = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> Stream m a -> Stream m a
forall (m :: * -> *). a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp (a -> Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *). a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
rx Stream m a
my)
        State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

    goX0 :: a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
_ -> do
        let stop :: m r
stop = a -> m r
sng a
x
            single :: a -> m r
single a
y = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
x)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
y)
            yield :: a -> Stream m a -> m r
yield a
y Stream m a
ry = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a
goX0 a
x Stream m a
ry)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a
y a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
ry)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
my

    goX :: a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
mx Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> do
        let stop :: m r
stop = a -> Stream m a -> m r
yld a
x Stream m a
mx
            single :: a -> m r
single a
y = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a
x a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
mx)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a
forall (m :: * -> *). Stream m a -> a -> Stream m a
goY0 Stream m a
mx a
y)
            yield :: a -> Stream m a -> m r
yield a
y Stream m a
ry = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
mx Stream m a
ry)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
mx a
y Stream m a
ry)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
my

    goY0 :: Stream m a -> a -> Stream m a
goY0 Stream m a
mx a
y = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
_ -> do
        let stop :: m r
stop = a -> m r
sng a
y
            single :: a -> m r
single a
x = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
x)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a -> Stream m a
forall a (m :: * -> *). a -> Stream m a
fromPure a
y)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a
x a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
rx)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a
goY0 Stream m a
rx a
y)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

    goY :: Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
mx a
y Stream m a
my = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
_ m r
_ -> do
        let stop :: m r
stop = a -> Stream m a -> m r
yld a
y Stream m a
my
            single :: a -> m r
single a
x = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a
forall (m :: * -> *). a -> Stream m a -> Stream m a
goX0 a
x Stream m a
my)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (a
y a -> Stream m a -> Stream m a
forall a (m :: * -> *). a -> Stream m a -> Stream m a
`cons` Stream m a
my)
            yield :: a -> Stream m a -> m r
yield a
x Stream m a
rx = do
                case a -> a -> Ordering
cmp a
x a
y of
                    Ordering
GT -> a -> Stream m a -> m r
yld a
y (a -> Stream m a -> Stream m a -> Stream m a
goX a
x Stream m a
rx Stream m a
my)
                    Ordering
_  -> a -> Stream m a -> m r
yld a
x (Stream m a -> a -> Stream m a -> Stream m a
goY Stream m a
rx a
y Stream m a
my)
         in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yield a -> m r
single m r
stop Stream m a
mx

------------------------------------------------------------------------------
-- Transformation comprehensions
------------------------------------------------------------------------------

{-# INLINE the #-}
the :: (Eq a, Monad m) => Stream m a -> m (Maybe a)
the :: Stream m a -> m (Maybe a)
the Stream m a
m = do
    Maybe (a, Stream m a)
r <- Stream m a -> m (Maybe (a, Stream m a))
forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
uncons Stream m a
m
    case Maybe (a, Stream m a)
r of
        Maybe (a, Stream 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, Stream m a
t) -> a -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m (Maybe a)
go a
h Stream m a
t
    where
    go :: a -> Stream m a -> m (Maybe a)
go a
h Stream m a
m1 =
        let single :: a -> m (Maybe a)
single a
a   | a
h a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a    = 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
h
                       | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
            yieldk :: a -> Stream m a -> m (Maybe a)
yieldk a
a Stream m a
r | a
h a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a    = a -> Stream m a -> m (Maybe a)
go a
h Stream m a
r
                       | Bool
otherwise = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
         in State Stream m a
-> (a -> Stream m a -> m (Maybe a))
-> (a -> m (Maybe a))
-> m (Maybe a)
-> Stream m a
-> m (Maybe a)
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState a -> Stream m a -> m (Maybe a)
yieldk a -> m (Maybe a)
forall (m :: * -> *). Monad m => a -> m (Maybe a)
single (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
h) Stream m a
m1

------------------------------------------------------------------------------
-- Alternative & MonadPlus
------------------------------------------------------------------------------

_alt :: Stream m a -> Stream m a -> Stream m a
_alt :: Stream m a -> Stream m a -> Stream m a
_alt Stream m a
m1 Stream m a
m2 = (forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall (m :: * -> *) a.
(forall r.
 State Stream m a
 -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
mkStream ((forall r.
  State Stream m a
  -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
 -> Stream m a)
-> (forall r.
    State Stream m a
    -> (a -> Stream m a -> m r) -> (a -> m r) -> m r -> m r)
-> Stream m a
forall a b. (a -> b) -> a -> b
$ \State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp ->
    let stop :: m r
stop  = State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stp Stream m a
m2
    in State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
forall (m :: * -> *) a r.
State Stream m a
-> (a -> Stream m a -> m r)
-> (a -> m r)
-> m r
-> Stream m a
-> m r
foldStream State Stream m a
st a -> Stream m a -> m r
yld a -> m r
sng m r
stop Stream m a
m1

------------------------------------------------------------------------------
-- MonadError
------------------------------------------------------------------------------

{-
-- XXX handle and test cross thread state transfer
withCatchError
    :: MonadError e m
    => Stream m a -> (e -> Stream m a) -> Stream m a
withCatchError m h =
    mkStream $ \_ stp sng yld ->
        let run x = unStream x Nothing stp sng yieldk
            handle r = r `catchError` \e -> run $ h e
            yieldk a r = yld a (withCatchError r h)
        in handle $ run m
-}