{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Streaming.Prelude (
Of (..)
, yield
, each
, stdinLn
, readLn
, fromHandle
, readFile
, iterate
, iterateM
, repeat
, repeatM
, replicate
, untilLeft
, untilRight
, cycle
, replicateM
, enumFrom
, enumFromThen
, unfoldr
, stdoutLn
, stdoutLn'
, mapM_
, print
, toHandle
, writeFile
, effects
, erase
, drained
, map
, mapM
, maps
, mapsPost
, mapped
, mappedPost
, for
, with
, subst
, copy
, duplicate
, store
, chain
, sequence
, nubOrd
, nubOrdOn
, nubInt
, nubIntOn
, filter
, filterM
, mapMaybeM
, delay
, intersperse
, take
, takeWhile
, takeWhileM
, drop
, dropWhile
, concat
, scan
, scanM
, scanned
, read
, show
, cons
, slidingWindow
, slidingWindowMin
, slidingWindowMinBy
, slidingWindowMinOn
, slidingWindowMax
, slidingWindowMaxBy
, slidingWindowMaxOn
, wrapEffect
, next
, uncons
, splitAt
, split
, breaks
, break
, breakWhen
, span
, group
, groupBy
, distinguish
, switch
, separate
, unseparate
, eitherToSum
, sumToEither
, sumToCompose
, composeToSum
, fold
, fold_
, foldM
, foldM_
, foldMap
, foldMap_
, all
, all_
, any
, any_
, sum
, sum_
, product
, product_
, head
, head_
, last
, last_
, elem
, elem_
, notElem
, notElem_
, length
, length_
, toList
, toList_
, mconcat
, mconcat_
, minimum
, minimum_
, maximum
, maximum_
, foldrM
, foldrT
, zip
, zipWith
, zip3
, zipWith3
, unzip
, partitionEithers
, partition
, merge
, mergeOn
, mergeBy
, catMaybes
, mapMaybe
, lazily
, strictly
, fst'
, snd'
, mapOf
, _first
, _second
, reread
, Stream
) where
import Streaming.Internal
import Control.Applicative (Applicative (..))
import Control.Concurrent (threadDelay)
import Control.Exception (throwIO, try)
import Control.Monad hiding (filterM, mapM, mapM_, foldM, foldM_, replicateM, sequence)
import Control.Monad.Trans
import Data.Functor (Functor (..), (<$))
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Functor.Of
import Data.Functor.Sum
import Data.Monoid (Monoid (mappend, mempty))
import Data.Ord (Ordering (..), comparing)
import Foreign.C.Error (Errno(Errno), ePIPE)
import Text.Read (readMaybe)
import qualified Data.Foldable as Foldable
import qualified Data.IntSet as IntSet
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Word (Word64)
import qualified GHC.IO.Exception as G
import qualified Prelude
import qualified System.IO as IO
import Prelude hiding (map, mapM, mapM_, filter, drop, dropWhile, take, mconcat
, sum, product, iterate, repeat, cycle, replicate, splitAt
, takeWhile, enumFrom, enumFromTo, enumFromThen, length
, print, zipWith, zip, zipWith3, zip3, unzip, seq, show, read
, readLn, sequence, concat, span, break, readFile, writeFile
, minimum, maximum, elem, notElem, all, any, head
, last, foldMap)
lazily :: Of a b -> (a,b)
lazily :: forall a b. Of a b -> (a, b)
lazily = \(a
a:>b
b) -> (a
a,b
b)
{-# INLINE lazily #-}
strictly :: (a,b) -> Of a b
strictly :: forall a b. (a, b) -> Of a b
strictly = \(a
a,b
b) -> a
a forall a b. a -> b -> Of a b
:> b
b
{-# INLINE strictly #-}
fst' :: Of a b -> a
fst' :: forall a b. Of a b -> a
fst' (a
a :> b
_) = a
a
{-# INLINE fst' #-}
snd' :: Of a b -> b
snd' :: forall a b. Of a b -> b
snd' (a
_ :> b
b) = b
b
{-# INLINE snd' #-}
mapOf :: (a -> b) -> Of a r -> Of b r
mapOf :: forall a b r. (a -> b) -> Of a r -> Of b r
mapOf a -> b
f (a
a :> r
b) = a -> b
f a
a forall a b. a -> b -> Of a b
:> r
b
{-# INLINE mapOf #-}
_first :: Functor f => (a -> f a') -> Of a b -> f (Of a' b)
_first :: forall (f :: * -> *) a a' b.
Functor f =>
(a -> f a') -> Of a b -> f (Of a' b)
_first a -> f a'
afb (a
a :> b
b) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a'
c -> a'
c forall a b. a -> b -> Of a b
:> b
b) (a -> f a'
afb a
a)
{-# INLINE _first #-}
_second :: Functor f => (b -> f b') -> Of a b -> f (Of a b')
_second :: forall (f :: * -> *) b b' a.
Functor f =>
(b -> f b') -> Of a b -> f (Of a b')
_second b -> f b'
afb (a
a :> b
b) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\b'
c -> a
a forall a b. a -> b -> Of a b
:> b'
c) (b -> f b'
afb b
b)
{-# INLINABLE _second #-}
all :: Monad m => (a -> Bool) -> Stream (Of a) m r -> m (Of Bool r)
all :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> m (Of Bool r)
all a -> Bool
thus = Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True where
loop :: Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
b Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
b forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
b
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thus a
a
then Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True Stream (Of a) m r
rest
else do
r
r <- forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
rest
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False forall a b. a -> b -> Of a b
:> r
r)
{-# INLINABLE all #-}
all_ :: Monad m => (a -> Bool) -> Stream (Of a) m r -> m Bool
all_ :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> m Bool
all_ a -> Bool
thus = Bool -> Stream (Of a) m r -> m Bool
loop Bool
True where
loop :: Bool -> Stream (Of a) m r -> m Bool
loop Bool
b Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
b
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m Bool
loop Bool
b
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thus a
a
then Bool -> Stream (Of a) m r -> m Bool
loop Bool
True Stream (Of a) m r
rest
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
{-# INLINABLE all_ #-}
any :: Monad m => (a -> Bool) -> Stream (Of a) m r -> m (Of Bool r)
any :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> m (Of Bool r)
any a -> Bool
thus = Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False where
loop :: Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
b Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
b forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
b
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thus a
a
then do
r
r <- forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
rest
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True forall a b. a -> b -> Of a b
:> r
r)
else Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False Stream (Of a) m r
rest
{-# INLINABLE any #-}
any_ :: Monad m => (a -> Bool) -> Stream (Of a) m r -> m Bool
any_ :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> m Bool
any_ a -> Bool
thus = Bool -> Stream (Of a) m r -> m Bool
loop Bool
False where
loop :: Bool -> Stream (Of a) m r -> m Bool
loop Bool
b Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
b
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m Bool
loop Bool
b
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thus a
a
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else Bool -> Stream (Of a) m r -> m Bool
loop Bool
False Stream (Of a) m r
rest
{-# INLINABLE any_ #-}
break :: Monad m => (a -> Bool) -> Stream (Of a) m r
-> Stream (Of a) m (Stream (Of a) m r)
break :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
break a -> Bool
thePred = Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thePred a
a
then forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
rest))
else forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop Stream (Of a) m r
rest)
{-# INLINABLE break #-}
breakWhen :: Monad m => (x -> a -> x) -> x -> (x -> b) -> (b -> Bool) -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
breakWhen :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x
-> (x -> b)
-> (b -> Bool)
-> Stream (Of a) m r
-> Stream (Of a) m (Stream (Of a) m r)
breakWhen x -> a -> x
step x
begin x -> b
done b -> Bool
thePred = x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop0 x
begin
where
loop0 :: x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop0 x
x Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) a. Monad m => a -> m a
return r
r)
Effect m (Stream (Of a) m r)
mn -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop0 x
x) m (Stream (Of a) m r)
mn
Step (a
a :> Stream (Of a) m r
rest) -> a -> x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop a
a (x -> a -> x
step x
x a
a) Stream (Of a) m r
rest
loop :: a -> x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop a
a !x
x Stream (Of a) m r
stream =
if b -> Bool
thePred (x -> b
done x
x)
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream (Of a) m r
stream)
else case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) a. Monad m => a -> m a
return r
r)
Effect m (Stream (Of a) m r)
mn -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop a
a x
x) m (Stream (Of a) m r)
mn
Step (a
a' :> Stream (Of a) m r
rest) -> do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a
a -> x -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop a
a' (x -> a -> x
step x
x a
a') Stream (Of a) m r
rest
{-# INLINABLE breakWhen #-}
breaks
:: Monad m =>
(a -> Bool) -> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
breaks :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
breaks a -> Bool
thus = Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop where
loop :: Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop Stream (Of a) m r
stream = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
Either r (a, Stream (Of a) m r)
e <- forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
stream
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Either r (a, Stream (Of a) m r)
e of
Left r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Right (a
a, Stream (Of a) m r
p') ->
if Bool -> Bool
not (a -> Bool
thus a
a)
then forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop (forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
break a -> Bool
thus Stream (Of a) m r
p')
else Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop Stream (Of a) m r
p'
{-# INLINABLE breaks #-}
chain :: Monad m => (a -> m y) -> Stream (Of a) m r -> Stream (Of a) m r
chain :: forall (m :: * -> *) a y r.
Monad m =>
(a -> m y) -> Stream (Of a) m r -> Stream (Of a) m r
chain a -> m y
f = Stream (Of a) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of a) m r)
mn -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m r
loop m (Stream (Of a) m r)
mn)
Step (a
a :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
y
_ <- a -> m y
f a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
rest))
{-# INLINABLE chain #-}
concat :: (Monad m, Foldable.Foldable f) => Stream (Of (f a)) m r -> Stream (Of a) m r
concat :: forall (m :: * -> *) (f :: * -> *) a r.
(Monad m, Foldable f) =>
Stream (Of (f a)) m r -> Stream (Of a) m r
concat = forall {m :: * -> *} {t :: * -> *} {a} {r}.
(Functor m, Foldable t) =>
Stream (Of (t a)) m r -> Stream (Of a) m r
loop
where
loop :: Stream (Of (t a)) m r -> Stream (Of a) m r
loop Stream (Of (t a)) m r
str = case Stream (Of (t a)) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of (t a)) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of (t a)) m r -> Stream (Of a) m r
loop m (Stream (Of (t a)) m r)
m)
Step (t a
lst :> Stream (Of (t a)) m r
as) ->
let inner :: [a] -> Stream (Of a) m r
inner [] = Stream (Of (t a)) m r -> Stream (Of a) m r
loop Stream (Of (t a)) m r
as
inner (a
x:[a]
rest) = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
x forall a b. a -> b -> Of a b
:> [a] -> Stream (Of a) m r
inner [a]
rest)
in [a] -> Stream (Of a) m r
inner (forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList t a
lst)
{-# INLINABLE concat #-}
cons :: (Monad m) => a -> Stream (Of a) m r -> Stream (Of a) m r
cons :: forall (m :: * -> *) a r.
Monad m =>
a -> Stream (Of a) m r -> Stream (Of a) m r
cons a
a Stream (Of a) m r
str = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
str)
{-# INLINE cons #-}
cycle :: (Monad m, Functor f) => Stream f m r -> Stream f m s
cycle :: forall (m :: * -> *) (f :: * -> *) r s.
(Monad m, Functor f) =>
Stream f m r -> Stream f m s
cycle Stream f m r
str = Stream f m s
loop where loop :: Stream f m s
loop = Stream f m r
str forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream f m s
loop
{-# INLINABLE cycle #-}
delay :: MonadIO m => Double -> Stream (Of a) m r -> Stream (Of a) m r
delay :: forall (m :: * -> *) a r.
MonadIO m =>
Double -> Stream (Of a) m r -> Stream (Of a) m r
delay Double
seconds = Stream (Of a) m r -> Stream (Of a) m r
loop where
pico :: Int
pico = forall a b. (RealFrac a, Integral b) => a -> b
truncate (Double
seconds forall a. Num a => a -> a -> a
* Double
1000000)
loop :: Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
str = do
Either r (a, Stream (Of a) m r)
e <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
str
case Either r (a, Stream (Of a) m r)
e of
Left r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Right (a
a,Stream (Of a) m r
rest) -> do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay Int
pico
Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
rest
{-# INLINABLE delay #-}
drained :: (Monad m, Monad (t m), MonadTrans t) => t m (Stream (Of a) m r) -> t m r
drained :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a r.
(Monad m, Monad (t m), MonadTrans t) =>
t m (Stream (Of a) m r) -> t m r
drained t m (Stream (Of a) m r)
tms = t m (Stream (Of a) m r)
tms forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects
{-# INLINE drained #-}
drop :: (Monad m) => Int -> Stream (Of a) m r -> Stream (Of a) m r
drop :: forall (m :: * -> *) a r.
Monad m =>
Int -> Stream (Of a) m r -> Stream (Of a) m r
drop Int
n Stream (Of a) m r
str | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = Stream (Of a) m r
str
drop Int
n Stream (Of a) m r
str = forall {t} {m :: * -> *} {a} {r}.
(Eq t, Num t, Functor m) =>
t -> Stream (Of a) m r -> Stream (Of a) m r
loop Int
n Stream (Of a) m r
str where
loop :: t -> Stream (Of a) m r -> Stream (Of a) m r
loop t
0 Stream (Of a) m r
stream = Stream (Of a) m r
stream
loop t
m Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
ma -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> Stream (Of a) m r -> Stream (Of a) m r
loop t
m) m (Stream (Of a) m r)
ma)
Step (a
_ :> Stream (Of a) m r
as) -> t -> Stream (Of a) m r -> Stream (Of a) m r
loop (t
mforall a. Num a => a -> a -> a
-t
1) Stream (Of a) m r
as
{-# INLINABLE drop #-}
dropWhile :: Monad m => (a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m r
dropWhile :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m r
dropWhile a -> Bool
thePred = Stream (Of a) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
ma -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m r
loop m (Stream (Of a) m r)
ma)
Step (a
a :> Stream (Of a) m r
as) -> if a -> Bool
thePred a
a
then Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
as
else forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
as)
{-# INLINABLE dropWhile #-}
each :: (Monad m, Foldable.Foldable f) => f a -> Stream (Of a) m ()
each :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
f a -> Stream (Of a) m ()
each = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr (\a
a Stream (Of a) m ()
p -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m ()
p)) (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ())
{-# INLINABLE each #-}
effects :: Monad m => Stream (Of a) m r -> m r
effects :: forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects = forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
loop where
loop :: Stream (Of a) m a -> m a
loop Stream (Of a) m a
stream = case Stream (Of a) m a
stream of
Return a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return a
r
Effect m (Stream (Of a) m a)
m -> m (Stream (Of a) m a)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m a -> m a
loop
Step (a
_ :> Stream (Of a) m a
rest) -> Stream (Of a) m a -> m a
loop Stream (Of a) m a
rest
{-# INLINABLE effects #-}
wrapEffect :: (Monad m, Functor f) => m a -> (a -> m y) -> Stream f m r -> Stream f m r
wrapEffect :: forall (m :: * -> *) (f :: * -> *) a y r.
(Monad m, Functor f) =>
m a -> (a -> m y) -> Stream f m r -> Stream f m r
wrapEffect m a
m a -> m y
f = Stream f m r -> Stream f m r
loop where
loop :: Stream f m r -> Stream f m r
loop Stream f m r
stream = do
a
x <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m
Either r (f (Stream f m r))
step <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Stream f m r -> m (Either r (f (Stream f m r)))
inspect Stream f m r
stream
y
_ <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ a -> m y
f a
x
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (f :: * -> *) a. Applicative f => a -> f a
pure f (Stream f m r) -> Stream f m r
loop' Either r (f (Stream f m r))
step
loop' :: f (Stream f m r) -> Stream f m r
loop' f (Stream f m r)
stream = forall (m :: * -> *) (f :: * -> *) r.
(Monad m, Functor f) =>
f (Stream f m r) -> Stream f m r
wrap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream f m r -> Stream f m r
loop f (Stream f m r)
stream)
elem :: (Monad m, Eq a) => a -> Stream (Of a) m r -> m (Of Bool r)
elem :: forall (m :: * -> *) a r.
(Monad m, Eq a) =>
a -> Stream (Of a) m r -> m (Of Bool r)
elem a
a' = Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False where
loop :: Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True Stream (Of a) m r
str = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool
True forall a b. a -> b -> Of a b
:>) (forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
str)
loop Bool
False Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False
Step (a
a:> Stream (Of a) m r
rest) ->
if a
a forall a. Eq a => a -> a -> Bool
== a
a'
then forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool
True forall a b. a -> b -> Of a b
:>) (forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
rest)
else Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False Stream (Of a) m r
rest
{-# INLINABLE elem #-}
elem_ :: (Monad m, Eq a) => a -> Stream (Of a) m r -> m Bool
elem_ :: forall (m :: * -> *) a r.
(Monad m, Eq a) =>
a -> Stream (Of a) m r -> m Bool
elem_ a
a' = Bool -> Stream (Of a) m r -> m Bool
loop Bool
False where
loop :: Bool -> Stream (Of a) m r -> m Bool
loop Bool
True Stream (Of a) m r
_ = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
loop Bool
False Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m Bool
loop Bool
False
Step (a
a:> Stream (Of a) m r
rest) ->
if a
a forall a. Eq a => a -> a -> Bool
== a
a'
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else Bool -> Stream (Of a) m r -> m Bool
loop Bool
False Stream (Of a) m r
rest
{-# INLINABLE elem_ #-}
enumFrom :: (Monad m, Enum n) => n -> Stream (Of n) m r
enumFrom :: forall (m :: * -> *) n r.
(Monad m, Enum n) =>
n -> Stream (Of n) m r
enumFrom = forall (m :: * -> *) n r.
(Monad m, Enum n) =>
n -> Stream (Of n) m r
loop where
loop :: t -> Stream (Of t) m r
loop !t
n = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (t
n forall a b. a -> b -> Of a b
:> t -> Stream (Of t) m r
loop (forall a. Enum a => a -> a
succ t
n))))
{-# INLINABLE enumFrom #-}
enumFromThen:: (Monad m, Enum a) => a -> a -> Stream (Of a) m r
enumFromThen :: forall (m :: * -> *) a r.
(Monad m, Enum a) =>
a -> a -> Stream (Of a) m r
enumFromThen a
first a
second = forall (m :: * -> *) a b r.
Monad m =>
(a -> b) -> Stream (Of a) m r -> Stream (Of b) m r
Streaming.Prelude.map forall a. Enum a => Int -> a
toEnum (Int -> Stream (Of Int) m r
loop Int
_first)
where
_first :: Int
_first = forall a. Enum a => a -> Int
fromEnum a
first
_second :: Int
_second = forall a. Enum a => a -> Int
fromEnum a
second
diff :: Int
diff = Int
_second forall a. Num a => a -> a -> a
- Int
_first
loop :: Int -> Stream (Of Int) m r
loop !Int
s = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Int
s forall a b. a -> b -> Of a b
:> Int -> Stream (Of Int) m r
loop (Int
sforall a. Num a => a -> a -> a
+Int
diff))
{-# INLINABLE enumFromThen #-}
erase :: Monad m => Stream (Of a) m r -> Stream Identity m r
erase :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream Identity m r
erase = forall {m :: * -> *} {a} {r}.
Functor m =>
Stream (Of a) m r -> Stream Identity m r
loop where
loop :: Stream (Of a) m r -> Stream Identity m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream Identity m r
loop m (Stream (Of a) m r)
m)
Step (a
_:>Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (forall a. a -> Identity a
Identity (Stream (Of a) m r -> Stream Identity m r
loop Stream (Of a) m r
rest))
{-# INLINABLE erase #-}
filter :: (Monad m) => (a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m r
filter :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m r
filter a -> Bool
thePred = Stream (Of a) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
as) -> if a -> Bool
thePred a
a
then forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
as)
else Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
as
{-# INLINE filter #-}
filterM :: (Monad m) => (a -> m Bool) -> Stream (Of a) m r -> Stream (Of a) m r
filterM :: forall (m :: * -> *) a r.
Monad m =>
(a -> m Bool) -> Stream (Of a) m r -> Stream (Of a) m r
filterM a -> m Bool
thePred = Stream (Of a) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m r
loop m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
as) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
Bool
bool <- a -> m Bool
thePred a
a
if Bool
bool
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
as)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Stream (Of a) m r -> Stream (Of a) m r
loop Stream (Of a) m r
as
{-# INLINE filterM #-}
fold_ :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ x -> a -> x
step x
begin x -> b
done = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(b
a :> r
_) -> b
a) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold x -> a -> x
step x
begin x -> b
done
{-# INLINE fold_ #-}
fold :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold x -> a -> x
step x
begin x -> b
done Stream (Of a) m r
str = Stream (Of a) m r -> x -> m (Of b r)
fold_loop Stream (Of a) m r
str x
begin
where
fold_loop :: Stream (Of a) m r -> x -> m (Of b r)
fold_loop Stream (Of a) m r
stream !x
x = case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (x -> b
done x
x forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Stream (Of a) m r
str' -> Stream (Of a) m r -> x -> m (Of b r)
fold_loop Stream (Of a) m r
str' x
x
Step (a
a :> Stream (Of a) m r
rest) -> Stream (Of a) m r -> x -> m (Of b r)
fold_loop Stream (Of a) m r
rest forall a b. (a -> b) -> a -> b
$! x -> a -> x
step x
x a
a
{-# INLINE fold #-}
foldM_
:: Monad m
=> (x -> a -> m x) -> m x -> (x -> m b) -> Stream (Of a) m r -> m b
foldM_ :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream (Of a) m r -> m b
foldM_ x -> a -> m x
step m x
begin x -> m b
done = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(b
a :> r
_) -> b
a) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> m x)
-> m x -> (x -> m b) -> Stream (Of a) m r -> m (Of b r)
foldM x -> a -> m x
step m x
begin x -> m b
done
{-# INLINE foldM_ #-}
foldM
:: Monad m
=> (x -> a -> m x) -> m x -> (x -> m b) -> Stream (Of a) m r ->m (Of b r)
foldM :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> m x)
-> m x -> (x -> m b) -> Stream (Of a) m r -> m (Of b r)
foldM x -> a -> m x
step m x
begin x -> m b
done Stream (Of a) m r
str = do
x
x0 <- m x
begin
Stream (Of a) m r -> x -> m (Of b r)
loop Stream (Of a) m r
str x
x0
where
loop :: Stream (Of a) m r -> x -> m (Of b r)
loop Stream (Of a) m r
stream !x
x = case Stream (Of a) m r
stream of
Return r
r -> x -> m b
done x
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (b
b forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Stream (Of a) m r
s -> Stream (Of a) m r -> x -> m (Of b r)
loop Stream (Of a) m r
s x
x
Step (a
a :> Stream (Of a) m r
rest) -> do
x
x' <- x -> a -> m x
step x
x a
a
Stream (Of a) m r -> x -> m (Of b r)
loop Stream (Of a) m r
rest x
x'
{-# INLINABLE foldM #-}
foldrT :: (Monad m, MonadTrans t, Monad (t m))
=> (a -> t m r -> t m r) -> Stream (Of a) m r -> t m r
foldrT :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a r.
(Monad m, MonadTrans t, Monad (t m)) =>
(a -> t m r -> t m r) -> Stream (Of a) m r -> t m r
foldrT a -> t m r -> t m r
step = Stream (Of a) m r -> t m r
loop where
loop :: Stream (Of a) m r -> t m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of a) m r)
m -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m r -> t m r
loop
Step (a
a :> Stream (Of a) m r
as) -> a -> t m r -> t m r
step a
a (Stream (Of a) m r -> t m r
loop Stream (Of a) m r
as)
{-# INLINABLE foldrT #-}
foldrM :: Monad m
=> (a -> m r -> m r) -> Stream (Of a) m r -> m r
foldrM :: forall (m :: * -> *) a r.
Monad m =>
(a -> m r -> m r) -> Stream (Of a) m r -> m r
foldrM a -> m r -> m r
step = Stream (Of a) m r -> m r
loop where
loop :: Stream (Of a) m r -> m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m r -> m r
loop
Step (a
a :> Stream (Of a) m r
as) -> a -> m r -> m r
step a
a (Stream (Of a) m r -> m r
loop Stream (Of a) m r
as)
{-# INLINABLE foldrM #-}
for :: (Monad m, Functor f) => Stream (Of a) m r -> (a -> Stream f m x) -> Stream f m r
for :: forall (m :: * -> *) (f :: * -> *) a r x.
(Monad m, Functor f) =>
Stream (Of a) m r -> (a -> Stream f m x) -> Stream f m r
for Stream (Of a) m r
str0 a -> Stream f m x
f = Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
str0 where
loop :: Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream f m r
loop m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
as) -> a -> Stream f m x
f a
a forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
as
{-# INLINABLE for #-}
groupBy :: Monad m
=> (a -> a -> Bool)
-> Stream (Of a) m r
-> Stream (Stream (Of a) m) m r
groupBy :: forall (m :: * -> *) a r.
Monad m =>
(a -> a -> Bool)
-> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
groupBy a -> a -> Bool
equals = Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop where
loop :: Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop Stream (Of a) m r
stream = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
Either r (a, Stream (Of a) m r)
e <- forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
stream
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Either r (a, Stream (Of a) m r)
e of
Left r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Right (a
a, Stream (Of a) m r
p') -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop (forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
span (a -> a -> Bool
equals a
a) Stream (Of a) m r
p')
{-# INLINABLE groupBy #-}
group :: (Monad m, Eq a) => Stream (Of a) m r -> Stream (Stream (Of a) m) m r
group :: forall (m :: * -> *) a r.
(Monad m, Eq a) =>
Stream (Of a) m r -> Stream (Stream (Of a) m) m r
group = forall (m :: * -> *) a r.
Monad m =>
(a -> a -> Bool)
-> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
groupBy forall a. Eq a => a -> a -> Bool
(==)
{-# INLINE group #-}
head :: Monad m => Stream (Of a) m r -> m (Of (Maybe a) r)
head :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of (Maybe a) r)
head Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing forall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of (Maybe a) r)
head
Step (a
a :> Stream (Of a) m r
rest) -> forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
rest forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a forall a b. a -> b -> Of a b
:> r
r)
{-# INLINABLE head #-}
head_ :: Monad m => Stream (Of a) m r -> m (Maybe a)
head_ :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Maybe a)
head_ Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Maybe a)
head_
Step (a
a :> Stream (Of a) m r
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
{-# INLINABLE head_ #-}
intersperse :: Monad m => a -> Stream (Of a) m r -> Stream (Of a) m r
intersperse :: forall (m :: * -> *) a r.
Monad m =>
a -> Stream (Of a) m r -> Stream (Of a) m r
intersperse a
x Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a r.
Monad m =>
a -> Stream (Of a) m r -> Stream (Of a) m r
intersperse a
x) m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) -> a -> Stream (Of a) m r -> Stream (Of a) m r
loop a
a Stream (Of a) m r
rest
where
loop :: a -> Stream (Of a) m r -> Stream (Of a) m r
loop !a
a Stream (Of a) m r
theStr = case Stream (Of a) m r
theStr of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> Stream (Of a) m r -> Stream (Of a) m r
loop a
a) m (Stream (Of a) m r)
m)
Step (a
b :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
x forall a b. a -> b -> Of a b
:> a -> Stream (Of a) m r -> Stream (Of a) m r
loop a
b Stream (Of a) m r
rest))
{-# INLINABLE intersperse #-}
iterate :: Monad m => (a -> a) -> a -> Stream (Of a) m r
iterate :: forall (m :: * -> *) a r.
Monad m =>
(a -> a) -> a -> Stream (Of a) m r
iterate a -> a
f = a -> Stream (Of a) m r
loop where
loop :: a -> Stream (Of a) m r
loop a
a' = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a' forall a b. a -> b -> Of a b
:> a -> Stream (Of a) m r
loop (a -> a
f a
a'))))
{-# INLINABLE iterate #-}
iterateM :: Monad m => (a -> m a) -> m a -> Stream (Of a) m r
iterateM :: forall (m :: * -> *) a r.
Monad m =>
(a -> m a) -> m a -> Stream (Of a) m r
iterateM a -> m a
f = m a -> Stream (Of a) m r
loop where
loop :: m a -> Stream (Of a) m r
loop m a
ma = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
ma
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> m a -> Stream (Of a) m r
loop (a -> m a
f a
a)))
{-# INLINABLE iterateM #-}
last :: Monad m => Stream (Of a) m r -> m (Of (Maybe a) r)
last :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of (Maybe a) r)
last = forall {m :: * -> *} {a} {b}.
Monad m =>
Maybe_ a -> Stream (Of a) m b -> m (Of (Maybe a) b)
loop forall a. Maybe_ a
Nothing_ where
loop :: Maybe_ a -> Stream (Of a) m b -> m (Of (Maybe a) b)
loop Maybe_ a
mb Stream (Of a) m b
str = case Stream (Of a) m b
str of
Return b
r -> case Maybe_ a
mb of
Maybe_ a
Nothing_ -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing forall a b. a -> b -> Of a b
:> b
r)
Just_ a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a forall a b. a -> b -> Of a b
:> b
r)
Effect m (Stream (Of a) m b)
m -> m (Stream (Of a) m b)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe_ a -> Stream (Of a) m b -> m (Of (Maybe a) b)
loop Maybe_ a
mb
Step (a
a :> Stream (Of a) m b
rest) -> Maybe_ a -> Stream (Of a) m b -> m (Of (Maybe a) b)
loop (forall a. a -> Maybe_ a
Just_ a
a) Stream (Of a) m b
rest
{-# INLINABLE last #-}
last_ :: Monad m => Stream (Of a) m r -> m (Maybe a)
last_ :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Maybe a)
last_ = forall {m :: * -> *} {a} {r}.
Monad m =>
Maybe_ a -> Stream (Of a) m r -> m (Maybe a)
loop forall a. Maybe_ a
Nothing_ where
loop :: Maybe_ a -> Stream (Of a) m r -> m (Maybe a)
loop Maybe_ a
mb Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> case Maybe_ a
mb of
Maybe_ a
Nothing_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Just_ a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe_ a -> Stream (Of a) m r -> m (Maybe a)
loop Maybe_ a
mb
Step (a
a :> Stream (Of a) m r
rest) -> Maybe_ a -> Stream (Of a) m r -> m (Maybe a)
loop (forall a. a -> Maybe_ a
Just_ a
a) Stream (Of a) m r
rest
{-# INLINABLE last_ #-}
length_ :: Monad m => Stream (Of a) m r -> m Int
length_ :: forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m Int
length_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ (\Int
n a
_ -> Int
n forall a. Num a => a -> a -> a
+ Int
1) Int
0 forall a. a -> a
id
{-# INLINE length_ #-}
length :: Monad m => Stream (Of a) m r -> m (Of Int r)
length :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of Int r)
length = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold (\Int
n a
_ -> Int
n forall a. Num a => a -> a -> a
+ Int
1) Int
0 forall a. a -> a
id
{-# INLINE length #-}
map :: Monad m => (a -> b) -> Stream (Of a) m r -> Stream (Of b) m r
map :: forall (m :: * -> *) a b r.
Monad m =>
(a -> b) -> Stream (Of a) m r -> Stream (Of b) m r
map a -> b
f = forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
(Monad m, Functor f) =>
(forall x. f x -> g x) -> Stream f m r -> Stream g m r
maps (\(a
x :> x
rest) -> a -> b
f a
x forall a b. a -> b -> Of a b
:> x
rest)
{-# INLINABLE map #-}
mapM :: Monad m => (a -> m b) -> Stream (Of a) m r -> Stream (Of b) m r
mapM :: forall (m :: * -> *) a b r.
Monad m =>
(a -> m b) -> Stream (Of a) m r -> Stream (Of b) m r
mapM a -> m b
f = Stream (Of a) m r -> Stream (Of b) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of b) m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
as) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
b
a' <- a -> m b
f a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
a' forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
as) )
{-# INLINABLE mapM #-}
mapM_ :: Monad m => (a -> m x) -> Stream (Of a) m r -> m r
mapM_ :: forall (m :: * -> *) a x r.
Monad m =>
(a -> m x) -> Stream (Of a) m r -> m r
mapM_ a -> m x
f = Stream (Of a) m r -> m r
loop where
loop :: Stream (Of a) m r -> m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m r -> m r
loop
Step (a
a :> Stream (Of a) m r
as) -> a -> m x
f a
a forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Stream (Of a) m r -> m r
loop Stream (Of a) m r
as
{-# INLINABLE mapM_ #-}
mapped :: (Monad m, Functor f) => (forall x . f x -> m (g x)) -> Stream f m r -> Stream g m r
mapped :: forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
(Monad m, Functor f) =>
(forall x. f x -> m (g x)) -> Stream f m r -> Stream g m r
mapped = forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
(Monad m, Functor f) =>
(forall x. f x -> m (g x)) -> Stream f m r -> Stream g m r
mapsM
{-# INLINE mapped #-}
mappedPost :: (Monad m, Functor g) => (forall x . f x -> m (g x)) -> Stream f m r -> Stream g m r
mappedPost :: forall (m :: * -> *) (g :: * -> *) (f :: * -> *) r.
(Monad m, Functor g) =>
(forall x. f x -> m (g x)) -> Stream f m r -> Stream g m r
mappedPost = forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
(Monad m, Functor g) =>
(forall x. f x -> m (g x)) -> Stream f m r -> Stream g m r
mapsMPost
{-# INLINE mappedPost #-}
foldMap :: (Monad m, Monoid w) => (a -> w) -> Stream (Of a) m r -> m (Of w r)
foldMap :: forall (m :: * -> *) w a r.
(Monad m, Monoid w) =>
(a -> w) -> Stream (Of a) m r -> m (Of w r)
foldMap a -> w
f = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold (\ !w
acc a
a -> forall a. Monoid a => a -> a -> a
mappend w
acc (a -> w
f a
a)) forall a. Monoid a => a
mempty forall a. a -> a
id
{-# INLINE foldMap #-}
foldMap_ :: (Monad m, Monoid w) => (a -> w) -> Stream (Of a) m r -> m w
foldMap_ :: forall (m :: * -> *) w a r.
(Monad m, Monoid w) =>
(a -> w) -> Stream (Of a) m r -> m w
foldMap_ a -> w
f = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ (\ !w
acc a
a -> forall a. Monoid a => a -> a -> a
mappend w
acc (a -> w
f a
a)) forall a. Monoid a => a
mempty forall a. a -> a
id
{-# INLINE foldMap_ #-}
mconcat :: (Monad m, Monoid w) => Stream (Of w) m r -> m (Of w r)
mconcat :: forall (m :: * -> *) w r.
(Monad m, Monoid w) =>
Stream (Of w) m r -> m (Of w r)
mconcat = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a
mempty forall a. a -> a
id
{-# INLINE mconcat #-}
data Maybe_ a = Just_ !a | Nothing_
mconcat_ :: (Monad m, Monoid w) => Stream (Of w) m r -> m w
mconcat_ :: forall (m :: * -> *) w r.
(Monad m, Monoid w) =>
Stream (Of w) m r -> m w
mconcat_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a
mempty forall a. a -> a
id
minimum :: (Monad m, Ord a) => Stream (Of a) m r -> m (Of (Maybe a) r)
minimum :: forall (m :: * -> *) a r.
(Monad m, Ord a) =>
Stream (Of a) m r -> m (Of (Maybe a) r)
minimum = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold (\Maybe_ a
m a
a -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. a -> Maybe_ a
Just_ a
a ; Just_ a
a' -> forall a. a -> Maybe_ a
Just_ (forall a. Ord a => a -> a -> a
min a
a a
a'))
forall a. Maybe_ a
Nothing_
(\Maybe_ a
m -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. Maybe a
Nothing; Just_ a
r -> forall a. a -> Maybe a
Just a
r)
{-# INLINE minimum #-}
minimum_ :: (Monad m, Ord a) => Stream (Of a) m r -> m (Maybe a)
minimum_ :: forall (m :: * -> *) a r.
(Monad m, Ord a) =>
Stream (Of a) m r -> m (Maybe a)
minimum_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ (\Maybe_ a
m a
a -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. a -> Maybe_ a
Just_ a
a ; Just_ a
a' -> forall a. a -> Maybe_ a
Just_ (forall a. Ord a => a -> a -> a
min a
a a
a'))
forall a. Maybe_ a
Nothing_
(\Maybe_ a
m -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. Maybe a
Nothing; Just_ a
r -> forall a. a -> Maybe a
Just a
r)
{-# INLINE minimum_ #-}
maximum :: (Monad m, Ord a) => Stream (Of a) m r -> m (Of (Maybe a) r)
maximum :: forall (m :: * -> *) a r.
(Monad m, Ord a) =>
Stream (Of a) m r -> m (Of (Maybe a) r)
maximum = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold (\Maybe_ a
m a
a -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. a -> Maybe_ a
Just_ a
a ; Just_ a
a' -> forall a. a -> Maybe_ a
Just_ (forall a. Ord a => a -> a -> a
max a
a a
a'))
forall a. Maybe_ a
Nothing_
(\Maybe_ a
m -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. Maybe a
Nothing; Just_ a
r -> forall a. a -> Maybe a
Just a
r)
{-# INLINE maximum #-}
maximum_ :: (Monad m, Ord a) => Stream (Of a) m r -> m (Maybe a)
maximum_ :: forall (m :: * -> *) a r.
(Monad m, Ord a) =>
Stream (Of a) m r -> m (Maybe a)
maximum_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ (\Maybe_ a
m a
a -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. a -> Maybe_ a
Just_ a
a ; Just_ a
a' -> forall a. a -> Maybe_ a
Just_ (forall a. Ord a => a -> a -> a
max a
a a
a'))
forall a. Maybe_ a
Nothing_
(\Maybe_ a
m -> case Maybe_ a
m of Maybe_ a
Nothing_ -> forall a. Maybe a
Nothing; Just_ a
r -> forall a. a -> Maybe a
Just a
r)
{-# INLINE maximum_ #-}
next :: Monad m => Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next = forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
loop where
loop :: Stream (Of a) m a -> m (Either a (a, Stream (Of a) m a))
loop Stream (Of a) m a
stream = case Stream (Of a) m a
stream of
Return a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left a
r)
Effect m (Stream (Of a) m a)
m -> m (Stream (Of a) m a)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m a -> m (Either a (a, Stream (Of a) m a))
loop
Step (a
a :> Stream (Of a) m a
rest) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right (a
a,Stream (Of a) m a
rest))
{-# INLINABLE next #-}
notElem :: (Monad m, Eq a) => a -> Stream (Of a) m r -> m (Of Bool r)
notElem :: forall (m :: * -> *) a r.
(Monad m, Eq a) =>
a -> Stream (Of a) m r -> m (Of Bool r)
notElem a
a' = Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True where
loop :: Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
False Stream (Of a) m r
str = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool
False forall a b. a -> b -> Of a b
:>) (forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
str)
loop Bool
True Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
Trueforall a b. a -> b -> Of a b
:> r
r)
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True
Step (a
a:> Stream (Of a) m r
rest) ->
if a
a forall a. Eq a => a -> a -> Bool
== a
a'
then forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool
False forall a b. a -> b -> Of a b
:>) (forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
effects Stream (Of a) m r
rest)
else Bool -> Stream (Of a) m r -> m (Of Bool r)
loop Bool
True Stream (Of a) m r
rest
{-# INLINABLE notElem #-}
notElem_ :: (Monad m, Eq a) => a -> Stream (Of a) m r -> m Bool
notElem_ :: forall (m :: * -> *) a r.
(Monad m, Eq a) =>
a -> Stream (Of a) m r -> m Bool
notElem_ a
a' = Bool -> Stream (Of a) m r -> m Bool
loop Bool
True where
loop :: Bool -> Stream (Of a) m r -> m Bool
loop Bool
False Stream (Of a) m r
_ = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
loop Bool
True Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> Stream (Of a) m r -> m Bool
loop Bool
True
Step (a
a:> Stream (Of a) m r
rest) ->
if a
a forall a. Eq a => a -> a -> Bool
== a
a'
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else Bool -> Stream (Of a) m r -> m Bool
loop Bool
True Stream (Of a) m r
rest
{-# INLINABLE notElem_ #-}
nubOrd :: (Monad m, Ord a) => Stream (Of a) m r -> Stream (Of a) m r
nubOrd :: forall (m :: * -> *) a r.
(Monad m, Ord a) =>
Stream (Of a) m r -> Stream (Of a) m r
nubOrd = forall (m :: * -> *) b a r.
(Monad m, Ord b) =>
(a -> b) -> Stream (Of a) m r -> Stream (Of a) m r
nubOrdOn forall a. a -> a
id
{-# INLINE nubOrd #-}
nubOrdOn :: (Monad m, Ord b) => (a -> b) -> Stream (Of a) m r -> Stream (Of a) m r
nubOrdOn :: forall (m :: * -> *) b a r.
(Monad m, Ord b) =>
(a -> b) -> Stream (Of a) m r -> Stream (Of a) m r
nubOrdOn a -> b
f Stream (Of a) m r
xs = Set b -> Stream (Of a) m r -> Stream (Of a) m r
loop forall a. Monoid a => a
mempty Stream (Of a) m r
xs where
loop :: Set b -> Stream (Of a) m r -> Stream (Of a) m r
loop !Set b
set Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Set b -> Stream (Of a) m r -> Stream (Of a) m r
loop Set b
set) m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) -> let !fa :: b
fa = a -> b
f a
a in
if forall a. Ord a => a -> Set a -> Bool
Set.member b
fa Set b
set
then Set b -> Stream (Of a) m r -> Stream (Of a) m r
loop Set b
set Stream (Of a) m r
rest
else forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Set b -> Stream (Of a) m r -> Stream (Of a) m r
loop (forall a. Ord a => a -> Set a -> Set a
Set.insert b
fa Set b
set) Stream (Of a) m r
rest)
nubInt :: Monad m => Stream (Of Int) m r -> Stream (Of Int) m r
nubInt :: forall (m :: * -> *) r.
Monad m =>
Stream (Of Int) m r -> Stream (Of Int) m r
nubInt = forall (m :: * -> *) a r.
Monad m =>
(a -> Int) -> Stream (Of a) m r -> Stream (Of a) m r
nubIntOn forall a. a -> a
id
{-# INLINE nubInt #-}
nubIntOn :: Monad m => (a -> Int) -> Stream (Of a) m r -> Stream (Of a) m r
nubIntOn :: forall (m :: * -> *) a r.
Monad m =>
(a -> Int) -> Stream (Of a) m r -> Stream (Of a) m r
nubIntOn a -> Int
f Stream (Of a) m r
xs = IntSet -> Stream (Of a) m r -> Stream (Of a) m r
loop forall a. Monoid a => a
mempty Stream (Of a) m r
xs where
loop :: IntSet -> Stream (Of a) m r -> Stream (Of a) m r
loop !IntSet
set Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (IntSet -> Stream (Of a) m r -> Stream (Of a) m r
loop IntSet
set) m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) -> let !fa :: Int
fa = a -> Int
f a
a in
if Int -> IntSet -> Bool
IntSet.member Int
fa IntSet
set
then IntSet -> Stream (Of a) m r -> Stream (Of a) m r
loop IntSet
set Stream (Of a) m r
rest
else forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> IntSet -> Stream (Of a) m r -> Stream (Of a) m r
loop (Int -> IntSet -> IntSet
IntSet.insert Int
fa IntSet
set) Stream (Of a) m r
rest)
partition :: Monad m => (a -> Bool) -> Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
partition :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
partition a -> Bool
thus = Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Stream (Of a) m r)
m))
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thus a
a
then forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of a) m r
rest)
else forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of a) m r
rest)
partitionEithers :: Monad m => Stream (Of (Either a b)) m r -> Stream (Of a) (Stream (Of b) m) r
partitionEithers :: forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (Either a b)) m r -> Stream (Of a) (Stream (Of b) m) r
partitionEithers = forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (Either a b)) m r -> Stream (Of a) (Stream (Of b) m) r
loop where
loop :: Stream (Of (Either a a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of (Either a a)) m r
str = case Stream (Of (Either a a)) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of (Either a a)) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of (Either a a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Stream (Of (Either a a)) m r)
m))
Step (Left a
a :> Stream (Of (Either a a)) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of (Either a a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of (Either a a)) m r
rest)
Step (Right a
b :> Stream (Of (Either a a)) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
b
forall (m :: * -> *) a. Monad m => a -> m a
return (Stream (Of (Either a a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of (Either a a)) m r
rest)
product_ :: (Monad m, Num a) => Stream (Of a) m () -> m a
product_ :: forall (m :: * -> *) a.
(Monad m, Num a) =>
Stream (Of a) m () -> m a
product_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ forall a. Num a => a -> a -> a
(*) a
1 forall a. a -> a
id
{-# INLINE product_ #-}
product :: (Monad m, Num a) => Stream (Of a) m r -> m (Of a r)
product :: forall (m :: * -> *) a r.
(Monad m, Num a) =>
Stream (Of a) m r -> m (Of a r)
product = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold forall a. Num a => a -> a -> a
(*) a
1 forall a. a -> a
id
{-# INLINE product #-}
read :: (Monad m, Read a) => Stream (Of String) m r -> Stream (Of a) m r
read :: forall (m :: * -> *) a r.
(Monad m, Read a) =>
Stream (Of String) m r -> Stream (Of a) m r
read Stream (Of String) m r
stream = forall (m :: * -> *) (f :: * -> *) a r x.
(Monad m, Functor f) =>
Stream (Of a) m r -> (a -> Stream f m x) -> Stream f m r
for Stream (Of String) m r
stream forall a b. (a -> b) -> a -> b
$ \String
str -> case forall a. Read a => String -> Maybe a
readMaybe String
str of
Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just a
r -> forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
r
{-# INLINE read #-}
repeat :: Monad m => a -> Stream (Of a) m r
repeat :: forall (m :: * -> *) a r. Monad m => a -> Stream (Of a) m r
repeat a
a = Stream (Of a) m r
loop where loop :: Stream (Of a) m r
loop = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
loop)))
{-# INLINE repeat #-}
repeatM :: Monad m => m a -> Stream (Of a) m r
repeatM :: forall (m :: * -> *) a r. Monad m => m a -> Stream (Of a) m r
repeatM m a
ma = Stream (Of a) m r
loop where
loop :: Stream (Of a) m r
loop = do
a
a <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
ma
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a
Stream (Of a) m r
loop
{-# INLINABLE repeatM #-}
replicate :: Monad m => Int -> a -> Stream (Of a) m ()
replicate :: forall (m :: * -> *) a. Monad m => Int -> a -> Stream (Of a) m ()
replicate Int
n a
_ | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
replicate Int
n a
a = Int -> Stream (Of a) m ()
loop Int
n where
loop :: Int -> Stream (Of a) m ()
loop Int
0 = forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ()
loop Int
m = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Int -> Stream (Of a) m ()
loop (Int
mforall a. Num a => a -> a -> a
-Int
1))))
{-# INLINABLE replicate #-}
replicateM :: Monad m => Int -> m a -> Stream (Of a) m ()
replicateM :: forall (m :: * -> *) a. Monad m => Int -> m a -> Stream (Of a) m ()
replicateM Int
n m a
_ | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
replicateM Int
n m a
ma = Int -> Stream (Of a) m ()
loop Int
n where
loop :: Int -> Stream (Of a) m ()
loop Int
0 = forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ()
loop Int
m = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
ma
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Int -> Stream (Of a) m ()
loop (Int
mforall a. Num a => a -> a -> a
-Int
1)))
{-# INLINABLE replicateM #-}
reread :: Monad m => (s -> m (Maybe a)) -> s -> Stream (Of a) m ()
reread :: forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe a)) -> s -> Stream (Of a) m ()
reread s -> m (Maybe a)
step s
s = Stream (Of a) m ()
loop where
loop :: Stream (Of a) m ()
loop = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
Maybe a
m <- s -> m (Maybe a)
step s
s
case Maybe a
m of
Maybe a
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ())
Just a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m ()
loop))
{-# INLINABLE reread #-}
scan :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> Stream (Of b) m r
scan :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x -> (x -> b) -> Stream (Of a) m r -> Stream (Of b) m r
scan x -> a -> x
step x
begin x -> b
done Stream (Of a) m r
str = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (x -> b
done x
begin forall a b. a -> b -> Of a b
:> x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
begin Stream (Of a) m r
str)
where
loop :: x -> Stream (Of a) m r -> Stream (Of b) m r
loop !x
acc Stream (Of a) m r
stream =
case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
acc) m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) ->
let !acc' :: x
acc' = x -> a -> x
step x
acc a
a
in forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (x -> b
done x
acc' forall a b. a -> b -> Of a b
:> x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
acc' Stream (Of a) m r
rest)
{-# INLINABLE scan #-}
scanM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> Stream (Of a) m r -> Stream (Of b) m r
scanM :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> m x)
-> m x -> (x -> m b) -> Stream (Of a) m r -> Stream (Of b) m r
scanM x -> a -> m x
step m x
begin x -> m b
done Stream (Of a) m r
str = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
x
x <- m x
begin
b
b <- x -> m b
done x
x
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
b forall a b. a -> b -> Of a b
:> x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
x Stream (Of a) m r
str))
where
loop :: x -> Stream (Of a) m r -> Stream (Of b) m r
loop !x
x Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (do
Stream (Of a) m r
stream' <- m (Stream (Of a) m r)
m
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
x Stream (Of a) m r
stream')
)
Step (a
a :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (do
x
x' <- x -> a -> m x
step x
x a
a
b
b <- x -> m b
done x
x'
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
b forall a b. a -> b -> Of a b
:> x -> Stream (Of a) m r -> Stream (Of b) m r
loop x
x' Stream (Of a) m r
rest))
)
{-# INLINABLE scanM #-}
scanned :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> Stream (Of (a,b)) m r
scanned :: forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x -> (x -> b) -> Stream (Of a) m r -> Stream (Of (a, b)) m r
scanned x -> a -> x
step x
begin x -> b
done = Maybe' a -> x -> Stream (Of a) m r -> Stream (Of (a, b)) m r
loop forall a. Maybe' a
Nothing' x
begin
where
loop :: Maybe' a -> x -> Stream (Of a) m r -> Stream (Of (a, b)) m r
loop !Maybe' a
m !x
x Stream (Of a) m r
stream =
case Stream (Of a) m r
stream of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of a) m r)
mn -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe' a -> x -> Stream (Of a) m r -> Stream (Of (a, b)) m r
loop Maybe' a
m x
x) m (Stream (Of a) m r)
mn
Step (a
a :> Stream (Of a) m r
rest) ->
case Maybe' a
m of
Maybe' a
Nothing' -> do
let !acc :: x
acc = x -> a -> x
step x
x a
a
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield (a
a, x -> b
done x
acc)
Maybe' a -> x -> Stream (Of a) m r -> Stream (Of (a, b)) m r
loop (forall a. a -> Maybe' a
Just' a
a) x
acc Stream (Of a) m r
rest
Just' a
_ -> do
let !acc :: b
acc = x -> b
done (x -> a -> x
step x
x a
a)
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield (a
a, b
acc)
Maybe' a -> x -> Stream (Of a) m r -> Stream (Of (a, b)) m r
loop (forall a. a -> Maybe' a
Just' a
a) (x -> a -> x
step x
x a
a) Stream (Of a) m r
rest
{-# INLINABLE scanned #-}
data Maybe' a = Just' a | Nothing'
sequence :: Monad m => Stream (Of (m a)) m r -> Stream (Of a) m r
sequence :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of (m a)) m r -> Stream (Of a) m r
sequence = forall (m :: * -> *) a r.
Monad m =>
Stream (Of (m a)) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of (m a)) m r -> Stream (Of a) m r
loop Stream (Of (m a)) m r
stream = case Stream (Of (m a)) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of (m a)) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of (m a)) m r -> Stream (Of a) m r
loop m (Stream (Of (m a)) m r)
m
Step (m a
ma :> Stream (Of (m a)) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
ma
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of (m a)) m r -> Stream (Of a) m r
loop Stream (Of (m a)) m r
rest))
{-# INLINABLE sequence #-}
show :: (Monad m, Show a) => Stream (Of a) m r -> Stream (Of String) m r
show :: forall (m :: * -> *) a r.
(Monad m, Show a) =>
Stream (Of a) m r -> Stream (Of String) m r
show = forall (m :: * -> *) a b r.
Monad m =>
(a -> b) -> Stream (Of a) m r -> Stream (Of b) m r
map forall a. Show a => a -> String
Prelude.show
{-# INLINE show #-}
sum_ :: (Monad m, Num a) => Stream (Of a) m () -> m a
sum_ :: forall (m :: * -> *) a.
(Monad m, Num a) =>
Stream (Of a) m () -> m a
sum_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ forall a. Num a => a -> a -> a
(+) a
0 forall a. a -> a
id
{-# INLINE sum_ #-}
sum :: (Monad m, Num a) => Stream (Of a) m r -> m (Of a r)
sum :: forall (m :: * -> *) a r.
(Monad m, Num a) =>
Stream (Of a) m r -> m (Of a r)
sum = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold forall a. Num a => a -> a -> a
(+) a
0 forall a. a -> a
id
{-# INLINABLE sum #-}
span :: Monad m => (a -> Bool) -> Stream (Of a) m r
-> Stream (Of a) m (Stream (Of a) m r)
span :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
span a -> Bool
thePred = Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
rest) -> if a -> Bool
thePred a
a
then forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
loop Stream (Of a) m r
rest)
else forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
rest))
{-# INLINABLE span #-}
split :: (Eq a, Monad m) =>
a -> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
split :: forall a (m :: * -> *) r.
(Eq a, Monad m) =>
a -> Stream (Of a) m r -> Stream (Stream (Of a) m) m r
split a
t = Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop where
loop :: Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) ->
if a
a forall a. Eq a => a -> a -> Bool
/= a
t
then forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop (forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a r.
Monad m =>
(a -> Bool)
-> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r)
break (forall a. Eq a => a -> a -> Bool
== a
t) Stream (Of a) m r
rest))
else Stream (Of a) m r -> Stream (Stream (Of a) m) m r
loop Stream (Of a) m r
rest
{-# INLINABLE split #-}
splitAt :: (Monad m, Functor f) => Int -> Stream f m r -> Stream f m (Stream f m r)
splitAt :: forall (m :: * -> *) (f :: * -> *) r.
(Monad m, Functor f) =>
Int -> Stream f m r -> Stream f m (Stream f m r)
splitAt = forall (m :: * -> *) (f :: * -> *) r.
(Monad m, Functor f) =>
Int -> Stream f m r -> Stream f m (Stream f m r)
splitsAt
{-# INLINE splitAt #-}
subst :: (Monad m, Functor f) => (a -> f x) -> Stream (Of a) m r -> Stream f m r
subst :: forall (m :: * -> *) (f :: * -> *) a x r.
(Monad m, Functor f) =>
(a -> f x) -> Stream (Of a) m r -> Stream f m r
subst a -> f x
f Stream (Of a) m r
s = Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
s where
loop :: Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream f m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
rest forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ a -> f x
f a
a)
{-# INLINABLE subst #-}
take :: (Monad m, Functor f) => Int -> Stream f m r -> Stream f m ()
take :: forall (m :: * -> *) (f :: * -> *) r.
(Monad m, Functor f) =>
Int -> Stream f m r -> Stream f m ()
take Int
n0 Stream f m r
_ | Int
n0 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return ()
take Int
n0 Stream f m r
str = forall {t} {m :: * -> *} {f :: * -> *} {r}.
(Eq t, Num t, Monad m, Functor f) =>
t -> Stream f m r -> Stream f m ()
loop Int
n0 Stream f m r
str where
loop :: t -> Stream f m r -> Stream f m ()
loop t
0 Stream f m r
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
loop t
n Stream f m r
p =
case Stream f m r
p of
Step f (Stream f m r)
fas -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> Stream f m r -> Stream f m ()
loop (t
nforall a. Num a => a -> a -> a
-t
1)) f (Stream f m r)
fas)
Effect m (Stream f m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> Stream f m r -> Stream f m ()
loop t
n) m (Stream f m r)
m)
Return r
_ -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ()
{-# INLINABLE take #-}
takeWhile :: Monad m => (a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m ()
takeWhile :: forall (m :: * -> *) a r.
Monad m =>
(a -> Bool) -> Stream (Of a) m r -> Stream (Of a) m ()
takeWhile a -> Bool
thePred = Stream (Of a) m r -> Stream (Of a) m ()
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m ()
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Step (a
a :> Stream (Of a) m r
as) -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a -> Bool
thePred a
a) (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m ()
loop Stream (Of a) m r
as))
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m ()
loop m (Stream (Of a) m r)
m)
Return r
_ -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ()
{-# INLINE takeWhile #-}
takeWhileM :: Monad m => (a -> m Bool) -> Stream (Of a) m r -> Stream (Of a) m ()
takeWhileM :: forall (m :: * -> *) a r.
Monad m =>
(a -> m Bool) -> Stream (Of a) m r -> Stream (Of a) m ()
takeWhileM a -> m Bool
thePred = Stream (Of a) m r -> Stream (Of a) m ()
loop where
loop :: Stream (Of a) m r -> Stream (Of a) m ()
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Step (a
a :> Stream (Of a) m r
as) -> do
Bool
b <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (a -> m Bool
thePred a
a)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
b (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m ()
loop Stream (Of a) m r
as))
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) m ()
loop m (Stream (Of a) m r)
m)
Return r
_ -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ()
{-# INLINE takeWhileM #-}
toList_ :: Monad m => Stream (Of a) m r -> m [a]
toList_ :: forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m [a]
toList_ = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m b
fold_ (\[a] -> [a]
diff a
a [a]
ls -> [a] -> [a]
diff (a
aforall a. a -> [a] -> [a]
: [a]
ls)) forall a. a -> a
id (\[a] -> [a]
diff -> [a] -> [a]
diff [])
{-# INLINE toList_ #-}
toList :: Monad m => Stream (Of a) m r -> m (Of [a] r)
toList :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of [a] r)
toList = forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream (Of a) m r -> m (Of b r)
fold (\[a] -> [a]
diff a
a [a]
ls -> [a] -> [a]
diff (a
aforall a. a -> [a] -> [a]
: [a]
ls)) forall a. a -> a
id (\[a] -> [a]
diff -> [a] -> [a]
diff [])
{-# INLINE toList #-}
uncons :: Monad m => Stream (Of a) m r -> m (Maybe (a, Stream (Of a) m r))
uncons :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Maybe (a, Stream (Of a) m r))
uncons = forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Maybe (a, Stream (Of a) m r))
loop where
loop :: Stream (Of a) m r -> m (Maybe (a, Stream (Of a) m r))
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Effect m (Stream (Of a) m r)
m -> m (Stream (Of a) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m r -> m (Maybe (a, Stream (Of a) m r))
loop
Step (a
a :> Stream (Of a) m r
rest) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (a
a,Stream (Of a) m r
rest))
{-# INLINABLE uncons #-}
unfoldr :: Monad m
=> (s -> m (Either r (a, s))) -> s -> Stream (Of a) m r
unfoldr :: forall (m :: * -> *) s r a.
Monad m =>
(s -> m (Either r (a, s))) -> s -> Stream (Of a) m r
unfoldr s -> m (Either r (a, s))
step = s -> Stream (Of a) m r
loop where
loop :: s -> Stream (Of a) m r
loop s
s0 = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (do
Either r (a, s)
e <- s -> m (Either r (a, s))
step s
s0
case Either r (a, s)
e of
Left r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
Right (a
a,s
s) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> s -> Stream (Of a) m r
loop s
s)))
{-# INLINABLE unfoldr #-}
untilLeft :: Monad m => m (Either r a) -> Stream (Of a) m r
untilLeft :: forall (m :: * -> *) r a.
Monad m =>
m (Either r a) -> Stream (Of a) m r
untilLeft m (Either r a)
act = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect m (Stream (Of a) m r)
loop where
loop :: m (Stream (Of a) m r)
loop = do
Either r a
e <- m (Either r a)
act
case Either r a
e of
Right a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect m (Stream (Of a) m r)
loop))
Left r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
{-# INLINABLE untilLeft #-}
untilRight :: Monad m => m (Either a r) -> Stream (Of a) m r
untilRight :: forall (m :: * -> *) a r.
Monad m =>
m (Either a r) -> Stream (Of a) m r
untilRight m (Either a r)
act = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect m (Stream (Of a) m r)
loop where
loop :: m (Stream (Of a) m r)
loop = do
Either a r
e <- m (Either a r)
act
case Either a r
e of
Right r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
Left a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect m (Stream (Of a) m r)
loop))
{-# INLINABLE untilRight #-}
with :: (Monad m, Functor f) => Stream (Of a) m r -> (a -> f x) -> Stream f m r
with :: forall (m :: * -> *) (f :: * -> *) a r x.
(Monad m, Functor f) =>
Stream (Of a) m r -> (a -> f x) -> Stream f m r
with Stream (Of a) m r
s a -> f x
f = Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
s where
loop :: Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream f m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Stream (Of a) m r -> Stream f m r
loop Stream (Of a) m r
rest forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ a -> f x
f a
a)
{-# INLINABLE with #-}
yield :: Monad m => a -> Stream (Of a) m ()
yield :: forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
a = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ())
{-# INLINE yield #-}
zip :: Monad m
=> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of (a,b)) m r
zip :: forall (m :: * -> *) a r b.
Monad m =>
Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of (a, b)) m r
zip = forall (m :: * -> *) a b c r.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
zipWith (,)
{-# INLINE zip #-}
zipWith :: Monad m
=> (a -> b -> c)
-> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
zipWith :: forall (m :: * -> *) a b c r.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
zipWith a -> b -> c
f = Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
loop
where
loop :: Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
loop Stream (Of a) m r
str0 Stream (Of b) m r
str1 = case Stream (Of a) m r
str0 of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Stream (Of a) m r
str -> Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
loop Stream (Of a) m r
str Stream (Of b) m r
str1) m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
rest0) -> case Stream (Of b) m r
str1 of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of b) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
loop Stream (Of a) m r
str0) m (Stream (Of b) m r)
m
Step (b
b :> Stream (Of b) m r
rest1) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a -> b -> c
f a
a b
b forall a b. a -> b -> Of a b
:>Stream (Of a) m r -> Stream (Of b) m r -> Stream (Of c) m r
loop Stream (Of a) m r
rest0 Stream (Of b) m r
rest1)
{-# INLINABLE zipWith #-}
zipWith3 :: Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
-> Stream (Of d) m r
zipWith3 :: forall (m :: * -> *) a b c d r.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
-> Stream (Of d) m r
zipWith3 a -> b -> c -> d
op = Stream (Of a) m r
-> Stream (Of b) m r -> Stream (Of c) m r -> Stream (Of d) m r
loop where
loop :: Stream (Of a) m r
-> Stream (Of b) m r -> Stream (Of c) m r -> Stream (Of d) m r
loop Stream (Of a) m r
str0 Stream (Of b) m r
str1 Stream (Of c) m r
str2 = do
Either r (a, Stream (Of a) m r)
e0 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
str0)
case Either r (a, Stream (Of a) m r)
e0 of
Left r
r0 -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r0
Right (a
a0,Stream (Of a) m r
rest0) -> do
Either r (b, Stream (Of b) m r)
e1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of b) m r
str1)
case Either r (b, Stream (Of b) m r)
e1 of
Left r
r1 -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r1
Right (b
a1,Stream (Of b) m r
rest1) -> do
Either r (c, Stream (Of c) m r)
e2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of c) m r
str2)
case Either r (c, Stream (Of c) m r)
e2 of
Left r
r2 -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r2
Right (c
a2,Stream (Of c) m r
rest2) -> do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield (a -> b -> c -> d
op a
a0 b
a1 c
a2)
Stream (Of a) m r
-> Stream (Of b) m r -> Stream (Of c) m r -> Stream (Of d) m r
loop Stream (Of a) m r
rest0 Stream (Of b) m r
rest1 Stream (Of c) m r
rest2
{-# INLINABLE zipWith3 #-}
zip3 :: Monad m
=> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
-> Stream (Of (a,b,c)) m r
zip3 :: forall (m :: * -> *) a r b c.
Monad m =>
Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
-> Stream (Of (a, b, c)) m r
zip3 = forall (m :: * -> *) a b c d r.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r
-> Stream (Of b) m r
-> Stream (Of c) m r
-> Stream (Of d) m r
zipWith3 (,,)
{-# INLINABLE zip3 #-}
stdinLn :: MonadIO m => Stream (Of String) m ()
stdinLn :: forall (m :: * -> *). MonadIO m => Stream (Of String) m ()
stdinLn = forall (m :: * -> *).
MonadIO m =>
Handle -> Stream (Of String) m ()
fromHandle Handle
IO.stdin
{-# INLINABLE stdinLn #-}
readLn :: (MonadIO m, Read a) => Stream (Of a) m ()
readLn :: forall (m :: * -> *) a. (MonadIO m, Read a) => Stream (Of a) m ()
readLn = Stream (Of a) m ()
loop where
loop :: Stream (Of a) m ()
loop = do
Bool
eof <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Bool
IO.isEOF
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
eof forall a b. (a -> b) -> a -> b
$ do
String
str <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO String
getLine
case forall a. Read a => String -> Maybe a
readMaybe String
str of
Maybe a
Nothing -> forall (m :: * -> *) a. (MonadIO m, Read a) => Stream (Of a) m ()
readLn
Just a
n -> forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
n forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream (Of a) m ()
loop
{-# INLINABLE readLn #-}
fromHandle :: MonadIO m => IO.Handle -> Stream (Of String) m ()
fromHandle :: forall (m :: * -> *).
MonadIO m =>
Handle -> Stream (Of String) m ()
fromHandle Handle
h = Stream (Of String) m ()
go
where
go :: Stream (Of String) m ()
go = do
Bool
eof <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> IO Bool
IO.hIsEOF Handle
h
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
eof forall a b. (a -> b) -> a -> b
$ do
String
str <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> IO String
IO.hGetLine Handle
h
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield String
str
Stream (Of String) m ()
go
{-# INLINABLE fromHandle #-}
toHandle :: MonadIO m => IO.Handle -> Stream (Of String) m r -> m r
toHandle :: forall (m :: * -> *) r.
MonadIO m =>
Handle -> Stream (Of String) m r -> m r
toHandle Handle
handle = Stream (Of String) m r -> m r
loop where
loop :: Stream (Of String) m r -> m r
loop Stream (Of String) m r
str = case Stream (Of String) m r
str of
Return r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Effect m (Stream (Of String) m r)
m -> m (Stream (Of String) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of String) m r -> m r
loop
Step (String
s :> Stream (Of String) m r
rest) -> do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
IO.hPutStrLn Handle
handle String
s)
Stream (Of String) m r -> m r
loop Stream (Of String) m r
rest
{-# INLINABLE toHandle #-}
print :: (MonadIO m, Show a) => Stream (Of a) m r -> m r
print :: forall (m :: * -> *) a r.
(MonadIO m, Show a) =>
Stream (Of a) m r -> m r
print = forall (m :: * -> *) a r.
(MonadIO m, Show a) =>
Stream (Of a) m r -> m r
loop where
loop :: Stream (Of a) m a -> m a
loop Stream (Of a) m a
stream = case Stream (Of a) m a
stream of
Return a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return a
r
Effect m (Stream (Of a) m a)
m -> m (Stream (Of a) m a)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of a) m a -> m a
loop
Step (a
a :> Stream (Of a) m a
rest) -> do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. Show a => a -> IO ()
Prelude.print a
a)
Stream (Of a) m a -> m a
loop Stream (Of a) m a
rest
stdoutLn :: MonadIO m => Stream (Of String) m () -> m ()
stdoutLn :: forall (m :: * -> *). MonadIO m => Stream (Of String) m () -> m ()
stdoutLn = forall {m :: * -> *} {r}.
MonadIO m =>
Stream (Of String) m r -> m ()
loop
where
loop :: Stream (Of String) m r -> m ()
loop Stream (Of String) m r
stream = case Stream (Of String) m r
stream of
Return r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Effect m (Stream (Of String) m r)
m -> m (Stream (Of String) m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Stream (Of String) m r -> m ()
loop
Step (String
s :> Stream (Of String) m r
rest) -> do
Either IOException ()
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => IO a -> IO (Either e a)
try (String -> IO ()
putStrLn String
s)
case Either IOException ()
x of
Left (G.IOError { ioe_type :: IOException -> IOErrorType
G.ioe_type = IOErrorType
G.ResourceVanished
, ioe_errno :: IOException -> Maybe CInt
G.ioe_errno = Just CInt
ioe })
| CInt -> Errno
Errno CInt
ioe forall a. Eq a => a -> a -> Bool
== Errno
ePIPE
-> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Left IOException
e -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => e -> IO a
throwIO IOException
e)
Right () -> Stream (Of String) m r -> m ()
loop Stream (Of String) m r
rest
{-# INLINABLE stdoutLn #-}
readFile :: FilePath -> (Stream (Of String) IO () -> IO a) -> IO a
readFile :: forall a. String -> (Stream (Of String) IO () -> IO a) -> IO a
readFile String
f Stream (Of String) IO () -> IO a
s = forall r. String -> IOMode -> (Handle -> IO r) -> IO r
IO.withFile String
f IOMode
IO.ReadMode forall a b. (a -> b) -> a -> b
$ \Handle
h -> Stream (Of String) IO () -> IO a
s (forall (m :: * -> *).
MonadIO m =>
Handle -> Stream (Of String) m ()
fromHandle Handle
h)
writeFile :: FilePath -> Stream (Of String) IO r -> IO r
writeFile :: forall r. String -> Stream (Of String) IO r -> IO r
writeFile String
f = forall r. String -> IOMode -> (Handle -> IO r) -> IO r
IO.withFile String
f IOMode
IO.WriteMode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r.
MonadIO m =>
Handle -> Stream (Of String) m r -> m r
toHandle
stdoutLn' :: MonadIO m => Stream (Of String) m r -> m r
stdoutLn' :: forall (m :: * -> *) r. MonadIO m => Stream (Of String) m r -> m r
stdoutLn' = forall (m :: * -> *) r.
MonadIO m =>
Handle -> Stream (Of String) m r -> m r
toHandle Handle
IO.stdout
distinguish :: (a -> Bool) -> Of a r -> Sum (Of a) (Of a) r
distinguish :: forall a r. (a -> Bool) -> Of a r -> Sum (Of a) (Of a) r
distinguish a -> Bool
predicate (a
a :> r
b) = if a -> Bool
predicate a
a then forall {k} (f :: k -> *) (g :: k -> *) (a :: k). g a -> Sum f g a
InR (a
a forall a b. a -> b -> Of a b
:> r
b) else forall {k} (f :: k -> *) (g :: k -> *) (a :: k). f a -> Sum f g a
InL (a
a forall a b. a -> b -> Of a b
:> r
b)
{-# INLINE distinguish #-}
sumToEither ::Sum (Of a) (Of b) r -> Of (Either a b) r
sumToEither :: forall a b r. Sum (Of a) (Of b) r -> Of (Either a b) r
sumToEither Sum (Of a) (Of b) r
s = case Sum (Of a) (Of b) r
s of
InL (a
a :> r
r) -> forall a b. a -> Either a b
Left a
a forall a b. a -> b -> Of a b
:> r
r
InR (b
b :> r
r) -> forall a b. b -> Either a b
Right b
b forall a b. a -> b -> Of a b
:> r
r
{-# INLINE sumToEither #-}
eitherToSum :: Of (Either a b) r -> Sum (Of a) (Of b) r
eitherToSum :: forall a b r. Of (Either a b) r -> Sum (Of a) (Of b) r
eitherToSum Of (Either a b) r
s = case Of (Either a b) r
s of
Left a
a :> r
r -> forall {k} (f :: k -> *) (g :: k -> *) (a :: k). f a -> Sum f g a
InL (a
a forall a b. a -> b -> Of a b
:> r
r)
Right b
b :> r
r -> forall {k} (f :: k -> *) (g :: k -> *) (a :: k). g a -> Sum f g a
InR (b
b forall a b. a -> b -> Of a b
:> r
r)
{-# INLINE eitherToSum #-}
composeToSum :: Compose (Of Bool) f r -> Sum f f r
composeToSum :: forall (f :: * -> *) r. Compose (Of Bool) f r -> Sum f f r
composeToSum Compose (Of Bool) f r
x = case Compose (Of Bool) f r
x of
Compose (Bool
True :> f r
f) -> forall {k} (f :: k -> *) (g :: k -> *) (a :: k). g a -> Sum f g a
InR f r
f
Compose (Bool
False :> f r
f) -> forall {k} (f :: k -> *) (g :: k -> *) (a :: k). f a -> Sum f g a
InL f r
f
{-# INLINE composeToSum #-}
sumToCompose :: Sum f f r -> Compose (Of Bool) f r
sumToCompose :: forall (f :: * -> *) r. Sum f f r -> Compose (Of Bool) f r
sumToCompose Sum f f r
x = case Sum f f r
x of
InR f r
f -> forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Bool
True forall a b. a -> b -> Of a b
:> f r
f)
InL f r
f -> forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Bool
False forall a b. a -> b -> Of a b
:> f r
f)
{-# INLINE sumToCompose #-}
store
:: Monad m =>
(Stream (Of a) (Stream (Of a) m) r -> t) -> Stream (Of a) m r -> t
store :: forall (m :: * -> *) a r t.
Monad m =>
(Stream (Of a) (Stream (Of a) m) r -> t) -> Stream (Of a) m r -> t
store Stream (Of a) (Stream (Of a) m) r -> t
f Stream (Of a) m r
x = Stream (Of a) (Stream (Of a) m) r -> t
f (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
copy Stream (Of a) m r
x)
{-# INLINE store #-}
copy
:: Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
copy :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
copy = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop where
loop :: Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of a) m r
str = case Stream (Of a) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Stream (Of a) m r)
m))
Step (a
a :> Stream (Of a) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of a) m r
rest))))
{-# INLINABLE copy#-}
duplicate
:: Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
duplicate :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
duplicate = forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> Stream (Of a) (Stream (Of a) m) r
copy
{-# INLINE duplicate #-}
unzip :: Monad m => Stream (Of (a,b)) m r -> Stream (Of a) (Stream (Of b) m) r
unzip :: forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r -> Stream (Of a) (Stream (Of b) m) r
unzip = forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r -> Stream (Of a) (Stream (Of b) m) r
loop where
loop :: Stream (Of (a, a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of (a, a)) m r
str = case Stream (Of (a, a)) m r
str of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of (a, a)) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of (a, a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Stream (Of (a, a)) m r)
m))
Step ((a
a,a
b):> Stream (Of (a, a)) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
b forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (Stream (Of (a, a)) m r -> Stream (Of a) (Stream (Of a) m) r
loop Stream (Of (a, a)) m r
rest))))
{-# INLINABLE unzip #-}
merge :: (Monad m, Ord a)
=> Stream (Of a) m r
-> Stream (Of a) m s
-> Stream (Of a) m (r, s)
merge :: forall (m :: * -> *) a r s.
(Monad m, Ord a) =>
Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
merge = forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
mergeBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE merge #-}
mergeOn :: (Monad m, Ord b)
=> (a -> b)
-> Stream (Of a) m r
-> Stream (Of a) m s
-> Stream (Of a) m (r, s)
mergeOn :: forall (m :: * -> *) b a r s.
(Monad m, Ord b) =>
(a -> b)
-> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
mergeOn a -> b
f = forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
mergeBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing a -> b
f)
{-# INLINE mergeOn #-}
mergeBy :: Monad m
=> (a -> a -> Ordering)
-> Stream (Of a) m r
-> Stream (Of a) m s
-> Stream (Of a) m (r, s)
mergeBy :: forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
cmp = Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop
where
loop :: Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
str0 Stream (Of a) m s
str1 = case Stream (Of a) m r
str0 of
Return r
r0 -> (\ s
r1 -> (r
r0, s
r1)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream (Of a) m s
str1
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ Stream (Of a) m r
str -> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
str Stream (Of a) m s
str1) m (Stream (Of a) m r)
m
Step (a
a :> Stream (Of a) m r
rest0) -> case Stream (Of a) m s
str1 of
Return s
r1 -> (\ r
r0 -> (r
r0, s
r1)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream (Of a) m r
str0
Effect m (Stream (Of a) m s)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
str0) m (Stream (Of a) m s)
m
Step (a
b :> Stream (Of a) m s
rest1) -> case a -> a -> Ordering
cmp a
a a
b of
Ordering
LT -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
rest0 Stream (Of a) m s
str1)
Ordering
EQ -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
rest0 Stream (Of a) m s
str1)
Ordering
GT -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
b forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of a) m s -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
str0 Stream (Of a) m s
rest1)
{-# INLINABLE mergeBy #-}
catMaybes :: Monad m => Stream (Of (Maybe a)) m r -> Stream (Of a) m r
catMaybes :: forall (m :: * -> *) a r.
Monad m =>
Stream (Of (Maybe a)) m r -> Stream (Of a) m r
catMaybes = forall {m :: * -> *} {a} {r}.
Functor m =>
Stream (Of (Maybe a)) m r -> Stream (Of a) m r
loop where
loop :: Stream (Of (Maybe a)) m r -> Stream (Of a) m r
loop Stream (Of (Maybe a)) m r
stream = case Stream (Of (Maybe a)) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of (Maybe a)) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of (Maybe a)) m r -> Stream (Of a) m r
loop m (Stream (Of (Maybe a)) m r)
m)
Step (Maybe a
ma :> Stream (Of (Maybe a)) m r
snext) -> case Maybe a
ma of
Maybe a
Nothing -> Stream (Of (Maybe a)) m r -> Stream (Of a) m r
loop Stream (Of (Maybe a)) m r
snext
Just a
a -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of (Maybe a)) m r -> Stream (Of a) m r
loop Stream (Of (Maybe a)) m r
snext)
{-# INLINABLE catMaybes #-}
mapMaybe :: Monad m => (a -> Maybe b) -> Stream (Of a) m r -> Stream (Of b) m r
mapMaybe :: forall (m :: * -> *) a b r.
Monad m =>
(a -> Maybe b) -> Stream (Of a) m r -> Stream (Of b) m r
mapMaybe a -> Maybe b
phi = Stream (Of a) m r -> Stream (Of b) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of b) m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
snext) -> case a -> Maybe b
phi a
a of
Maybe b
Nothing -> Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
snext
Just b
b -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
b forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
snext)
{-# INLINABLE mapMaybe #-}
slidingWindow :: Monad m
=> Int
-> Stream (Of a) m b
-> Stream (Of (Seq.Seq a)) m b
slidingWindow :: forall (m :: * -> *) a b.
Monad m =>
Int -> Stream (Of a) m b -> Stream (Of (Seq a)) m b
slidingWindow Int
n = forall {t} {m :: * -> *} {a} {r}.
(Eq t, Num t, Monad m) =>
t -> Seq a -> Stream (Of a) m r -> Stream (Of (Seq a)) m r
setup (forall a. Ord a => a -> a -> a
max Int
1 Int
n :: Int) forall a. Monoid a => a
mempty
where
window :: Seq a -> Stream (Of a) m b -> Stream (Of (Seq a)) m b
window !Seq a
sequ Stream (Of a) m b
str = do
Either b (a, Stream (Of a) m b)
e <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m b
str)
case Either b (a, Stream (Of a) m b)
e of
Left b
r -> forall (m :: * -> *) a. Monad m => a -> m a
return b
r
Right (a
a,Stream (Of a) m b
rest) -> do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield (Seq a
sequ forall a. Seq a -> a -> Seq a
Seq.|> a
a)
Seq a -> Stream (Of a) m b -> Stream (Of (Seq a)) m b
window (forall a. Int -> Seq a -> Seq a
Seq.drop Int
1 forall a b. (a -> b) -> a -> b
$ Seq a
sequ forall a. Seq a -> a -> Seq a
Seq.|> a
a) Stream (Of a) m b
rest
setup :: t -> Seq a -> Stream (Of a) m r -> Stream (Of (Seq a)) m r
setup t
0 !Seq a
sequ Stream (Of a) m r
str = do
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield Seq a
sequ
forall {m :: * -> *} {a} {b}.
Monad m =>
Seq a -> Stream (Of a) m b -> Stream (Of (Seq a)) m b
window (forall a. Int -> Seq a -> Seq a
Seq.drop Int
1 Seq a
sequ) Stream (Of a) m r
str
setup t
m Seq a
sequ Stream (Of a) m r
str = do
Either r (a, Stream (Of a) m r)
e <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
str
case Either r (a, Stream (Of a) m r)
e of
Left r
r -> forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield Seq a
sequ forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Right (a
x,Stream (Of a) m r
rest) -> t -> Seq a -> Stream (Of a) m r -> Stream (Of (Seq a)) m r
setup (t
mforall a. Num a => a -> a -> a
-t
1) (Seq a
sequ forall a. Seq a -> a -> Seq a
Seq.|> a
x) Stream (Of a) m r
rest
{-# INLINABLE slidingWindow #-}
slidingWindowMin :: (Monad m, Ord a) => Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMin :: forall (m :: * -> *) a b.
(Monad m, Ord a) =>
Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMin = forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Ordering)
-> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMinBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE slidingWindowMin #-}
slidingWindowMax :: (Monad m, Ord a) => Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMax :: forall (m :: * -> *) a b.
(Monad m, Ord a) =>
Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMax = forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Ordering)
-> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMaxBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE slidingWindowMax #-}
slidingWindowMinBy :: Monad m => (a -> a -> Ordering) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMinBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Ordering)
-> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMinBy a -> a -> Ordering
cmp = forall (m :: * -> *) a p b.
Monad m =>
(a -> p)
-> (p -> p -> Bool)
-> Int
-> Stream (Of a) m b
-> Stream (Of a) m b
slidingWindowOrd forall a. a -> a
id (\a
a a
b -> a -> a -> Ordering
cmp a
a a
b forall a. Eq a => a -> a -> Bool
== Ordering
GT)
{-# INLINE slidingWindowMinBy #-}
slidingWindowMaxBy :: Monad m => (a -> a -> Ordering) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMaxBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> a -> Ordering)
-> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMaxBy a -> a -> Ordering
cmp = forall (m :: * -> *) a p b.
Monad m =>
(a -> p)
-> (p -> p -> Bool)
-> Int
-> Stream (Of a) m b
-> Stream (Of a) m b
slidingWindowOrd forall a. a -> a
id (\a
a a
b -> a -> a -> Ordering
cmp a
a a
b forall a. Eq a => a -> a -> Bool
/= Ordering
GT)
{-# INLINE slidingWindowMaxBy #-}
slidingWindowMinOn :: (Monad m, Ord p) => (a -> p) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMinOn :: forall (m :: * -> *) p a b.
(Monad m, Ord p) =>
(a -> p) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMinOn a -> p
proj = forall (m :: * -> *) a p b.
Monad m =>
(a -> p)
-> (p -> p -> Bool)
-> Int
-> Stream (Of a) m b
-> Stream (Of a) m b
slidingWindowOrd a -> p
proj (\p
a p
b -> forall a. Ord a => a -> a -> Ordering
compare p
a p
b forall a. Eq a => a -> a -> Bool
== Ordering
GT)
{-# INLINE slidingWindowMinOn #-}
slidingWindowMaxOn :: (Monad m, Ord p) => (a -> p) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMaxOn :: forall (m :: * -> *) p a b.
(Monad m, Ord p) =>
(a -> p) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowMaxOn a -> p
proj = forall (m :: * -> *) a p b.
Monad m =>
(a -> p)
-> (p -> p -> Bool)
-> Int
-> Stream (Of a) m b
-> Stream (Of a) m b
slidingWindowOrd a -> p
proj (\p
a p
b -> forall a. Ord a => a -> a -> Ordering
compare p
a p
b forall a. Eq a => a -> a -> Bool
/= Ordering
GT)
{-# INLINE slidingWindowMaxOn #-}
slidingWindowOrd :: Monad m => (a -> p) -> (p -> p -> Bool) -> Int -> Stream (Of a) m b -> Stream (Of a) m b
slidingWindowOrd :: forall (m :: * -> *) a p b.
Monad m =>
(a -> p)
-> (p -> p -> Bool)
-> Int
-> Stream (Of a) m b
-> Stream (Of a) m b
slidingWindowOrd a -> p
proj p -> p -> Bool
f Int
n =
forall (m :: * -> *) a r.
Monad m =>
Int -> Stream (Of a) m r -> Stream (Of a) m r
dropButRetainAtLeastOne (Int
kforall a. Num a => a -> a -> a
-Int
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r.
Monad m =>
Stream (Of (Maybe a)) m r -> Stream (Of a) m r
catMaybes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x -> (x -> b) -> Stream (Of a) m r -> Stream (Of b) m r
scan SlidingWindowOrdState a p -> a -> SlidingWindowOrdState a p
update forall {a} {p}. SlidingWindowOrdState a p
initial forall {a} {p}. SlidingWindowOrdState a p -> Maybe a
extract
where
k :: Int
k = forall a. Ord a => a -> a -> a
max Int
1 Int
n
initial :: SlidingWindowOrdState a p
initial = forall a p.
Word64
-> Seq (SlidingWindowOrdElement a p) -> SlidingWindowOrdState a p
SlidingWindowOrdState Word64
0 forall a. Monoid a => a
mempty
update :: SlidingWindowOrdState a p -> a -> SlidingWindowOrdState a p
update (SlidingWindowOrdState Word64
i Seq (SlidingWindowOrdElement a p)
w0) a
a =
let projected :: p
projected = a -> p
proj a
a
w1 :: Seq (SlidingWindowOrdElement a p)
w1 = forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileR (\(SlidingWindowOrdElement Word64
_ a
_ p
p) -> p -> p -> Bool
f p
p p
projected) Seq (SlidingWindowOrdElement a p)
w0
w2 :: Seq (SlidingWindowOrdElement a p)
w2 = Seq (SlidingWindowOrdElement a p)
w1 forall a. Seq a -> a -> Seq a
Seq.|> forall a p. Word64 -> a -> p -> SlidingWindowOrdElement a p
SlidingWindowOrdElement Word64
i a
a p
projected
w3 :: Seq (SlidingWindowOrdElement a p)
w3 = forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileL (\(SlidingWindowOrdElement Word64
j a
_ p
_) -> Word64
j forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k forall a. Ord a => a -> a -> Bool
<= Word64
i) Seq (SlidingWindowOrdElement a p)
w2
in forall a p.
Word64
-> Seq (SlidingWindowOrdElement a p) -> SlidingWindowOrdState a p
SlidingWindowOrdState (Word64
i forall a. Num a => a -> a -> a
+ Word64
1) Seq (SlidingWindowOrdElement a p)
w3
extract :: SlidingWindowOrdState a p -> Maybe a
extract (SlidingWindowOrdState Word64
_ Seq (SlidingWindowOrdElement a p)
w) =
case forall a. Seq a -> ViewL a
Seq.viewl Seq (SlidingWindowOrdElement a p)
w of
SlidingWindowOrdElement Word64
_ a
m p
_ Seq.:< Seq (SlidingWindowOrdElement a p)
_ -> forall a. a -> Maybe a
Just a
m
ViewL (SlidingWindowOrdElement a p)
_ -> forall a. Maybe a
Nothing
{-# INLINABLE slidingWindowOrd #-}
data SlidingWindowOrdState a p =
SlidingWindowOrdState !Word64
!(Seq.Seq (SlidingWindowOrdElement a p))
data SlidingWindowOrdElement a p = SlidingWindowOrdElement !Word64 a p
dropButRetainAtLeastOne :: Monad m => Int -> Stream (Of a) m r -> Stream (Of a) m r
dropButRetainAtLeastOne :: forall (m :: * -> *) a r.
Monad m =>
Int -> Stream (Of a) m r -> Stream (Of a) m r
dropButRetainAtLeastOne Int
0 = forall a. a -> a
id
dropButRetainAtLeastOne Int
n = forall {t} {m :: * -> *} {a} {r}.
(Eq t, Num t, Monad m) =>
Maybe a -> t -> Stream (Of a) m r -> Stream (Of a) m r
loop forall a. Maybe a
Nothing Int
n
where
loop :: Maybe a -> t -> Stream (Of a) m r -> Stream (Of a) m r
loop (Just a
final) (-1) Stream (Of a) m r
str = forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
final forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream (Of a) m r
str
loop Maybe a
final t
m Stream (Of a) m r
str = do
Either r (a, Stream (Of a) m r)
e <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r))
next Stream (Of a) m r
str)
case Either r (a, Stream (Of a) m r)
e of
Left r
r -> do
case Maybe a
final of
Maybe a
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just a
l -> forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
l
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Right (a
x, Stream (Of a) m r
rest) -> Maybe a -> t -> Stream (Of a) m r -> Stream (Of a) m r
loop (forall a. a -> Maybe a
Just a
x) (t
m forall a. Num a => a -> a -> a
- t
1) Stream (Of a) m r
rest
{-# INLINABLE dropButRetainAtLeastOne #-}
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream (Of a) m r -> Stream (Of b) m r
mapMaybeM :: forall (m :: * -> *) a b r.
Monad m =>
(a -> m (Maybe b)) -> Stream (Of a) m r -> Stream (Of b) m r
mapMaybeM a -> m (Maybe b)
phi = Stream (Of a) m r -> Stream (Of b) m r
loop where
loop :: Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
stream = case Stream (Of a) m r
stream of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Effect m (Stream (Of a) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (Of a) m r -> Stream (Of b) m r
loop m (Stream (Of a) m r)
m)
Step (a
a :> Stream (Of a) m r
snext) -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$
forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> m (Maybe b)
phi a
a) forall a b. (a -> b) -> a -> b
$ \Maybe b
x -> case Maybe b
x of
Maybe b
Nothing -> Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
snext
Just b
b -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
b forall a b. a -> b -> Of a b
:> Stream (Of a) m r -> Stream (Of b) m r
loop Stream (Of a) m r
snext)
{-# INLINABLE mapMaybeM #-}