{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Stream.Monadic (
Box(..), liftBox,
Stream(..), Step(..), SPEC(..),
length, null,
empty, singleton, cons, snoc, replicate, replicateM, generate, generateM, (++),
head, last, (!!), (!?),
slice, init, tail, take, drop,
map, mapM, mapM_, trans, unbox, concatMap, flatten,
indexed, indexedR, zipWithM_,
zipWithM, zipWith3M, zipWith4M, zipWith5M, zipWith6M,
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
zip, zip3, zip4, zip5, zip6,
eqBy, cmpBy,
filter, filterM, uniq, mapMaybe, mapMaybeM, catMaybes, takeWhile, takeWhileM, dropWhile, dropWhileM,
elem, notElem, find, findM, findIndex, findIndexM,
foldl, foldlM, foldl1, foldl1M, foldM, fold1M,
foldl', foldlM', foldl1', foldl1M', foldM', fold1M',
foldr, foldrM, foldr1, foldr1M,
and, or, concatMapM,
unfoldr, unfoldrM,
unfoldrN, unfoldrNM,
unfoldrExactN, unfoldrExactNM,
iterateN, iterateNM,
prescanl, prescanlM, prescanl', prescanlM',
postscanl, postscanlM, postscanl', postscanlM',
scanl, scanlM, scanl', scanlM',
scanl1, scanl1M, scanl1', scanl1M',
enumFromStepN, enumFromTo, enumFromThenTo,
toList, fromList, fromListN
) where
import Data.Char ( ord )
import GHC.Base ( unsafeChr )
import Control.Monad ( liftM )
import qualified Prelude
import Prelude hiding ( length, null,
replicate, (++),
head, last, (!!),
init, tail, take, drop,
map, mapM, mapM_, concatMap,
zipWith, zipWith3, zip, zip3,
filter, takeWhile, dropWhile,
elem, notElem,
foldl, foldl1, foldr, foldr1,
and, or,
scanl, scanl1,
enumFromTo, enumFromThenTo )
import Data.Int ( Int8, Int16, Int32 )
import Data.Word ( Word8, Word16, Word32, Word64 )
import GHC.Stack (HasCallStack)
import GHC.Types ( SPEC(..) )
#include "MachDeps.h"
#define INLINE_FUSED INLINE [1]
#define INLINE_INNER INLINE [0]
#if WORD_SIZE_IN_BITS > 32
import Data.Int ( Int64 )
#endif
data Box a = Box { Box a -> a
unBox :: a }
instance Functor Box where
fmap :: (a -> b) -> Box a -> Box b
fmap a -> b
f (Box a
x) = b -> Box b
forall a. a -> Box a
Box (a -> b
f a
x)
instance Applicative Box where
pure :: a -> Box a
pure = a -> Box a
forall a. a -> Box a
Box
Box a -> b
f <*> :: Box (a -> b) -> Box a -> Box b
<*> Box a
x = b -> Box b
forall a. a -> Box a
Box (a -> b
f a
x)
instance Monad Box where
return :: a -> Box a
return = a -> Box a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
Box a
x >>= :: Box a -> (a -> Box b) -> Box b
>>= a -> Box b
f = a -> Box b
f a
x
liftBox :: Monad m => Box a -> m a
liftBox :: Box a -> m a
liftBox (Box a
a) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE liftBox #-}
emptyStream :: String
{-# NOINLINE emptyStream #-}
emptyStream :: String
emptyStream = String
"empty stream"
data Step s a where
Yield :: a -> s -> Step s a
Skip :: s -> Step s a
Done :: Step s a
instance Functor (Step s) where
{-# INLINE fmap #-}
fmap :: (a -> b) -> Step s a -> Step s b
fmap a -> b
f (Yield a
x s
s) = b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield (a -> b
f a
x) s
s
fmap a -> b
_ (Skip s
s) = s -> Step s b
forall s a. s -> Step s a
Skip s
s
fmap a -> b
_ Step s a
Done = Step s b
forall s a. Step s a
Done
{-# INLINE (<$) #-}
<$ :: a -> Step s b -> Step s a
(<$) = (b -> a) -> Step s b -> Step s a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> a) -> Step s b -> Step s a)
-> (a -> b -> a) -> a -> Step s b -> Step s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> a
forall a b. a -> b -> a
const
data Stream m a = forall s. Stream (s -> m (Step s a)) s
length :: Monad m => Stream m a -> m Int
{-# INLINE_FUSED length #-}
length :: Stream m a -> m Int
length = (Int -> a -> Int) -> Int -> Stream m a -> m Int
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
foldl' (\Int
n a
_ -> Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
0
null :: Monad m => Stream m a -> m Bool
{-# INLINE_FUSED null #-}
null :: Stream m a -> m Bool
null (Stream s -> m (Step s a)
step s
t) = s -> m Bool
null_loop s
t
where
null_loop :: s -> m Bool
null_loop s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
_ s
_ -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Skip s
s' -> s -> m Bool
null_loop s
s'
Step s a
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
empty :: Monad m => Stream m a
{-# INLINE_FUSED empty #-}
empty :: Stream m a
empty = (() -> m (Step () a)) -> () -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (m (Step () a) -> () -> m (Step () a)
forall a b. a -> b -> a
const (Step () a -> m (Step () a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step () a
forall s a. Step s a
Done)) ()
singleton :: Monad m => a -> Stream m a
{-# INLINE_FUSED singleton #-}
singleton :: a -> Stream m a
singleton a
x = (Bool -> m (Step Bool a)) -> Bool -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Step Bool a -> m (Step Bool a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Bool a -> m (Step Bool a))
-> (Bool -> Step Bool a) -> Bool -> m (Step Bool a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Step Bool a
step) Bool
True
where
{-# INLINE_INNER step #-}
step :: Bool -> Step Bool a
step Bool
True = a -> Bool -> Step Bool a
forall a s. a -> s -> Step s a
Yield a
x Bool
False
step Bool
False = Step Bool a
forall s a. Step s a
Done
replicate :: Monad m => Int -> a -> Stream m a
{-# INLINE_FUSED replicate #-}
replicate :: Int -> a -> Stream m a
replicate Int
n a
x = Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
replicateM :: Monad m => Int -> m a -> Stream m a
{-# INLINE_FUSED replicateM #-}
replicateM :: Int -> m a -> Stream m a
replicateM Int
n m a
p = (Int -> m (Step Int a)) -> Int -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
n
where
{-# INLINE_INNER step #-}
step :: Int -> m (Step Int a)
step Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
Done
| Bool
otherwise = do { a
x <- m a
p; Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$ a -> Int -> Step Int a
forall a s. a -> s -> Step s a
Yield a
x (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) }
generate :: Monad m => Int -> (Int -> a) -> Stream m a
{-# INLINE generate #-}
generate :: Int -> (Int -> a) -> Stream m a
generate Int
n Int -> a
f = Int -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (Int -> a) -> Int -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f)
generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
{-# INLINE_FUSED generateM #-}
generateM :: Int -> (Int -> m a) -> Stream m a
generateM Int
n Int -> m a
f = Int
n Int -> Stream m a -> Stream m a
`seq` (Int -> m (Step Int a)) -> Int -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
0
where
{-# INLINE_INNER step #-}
step :: Int -> m (Step Int a)
step Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = do
a
x <- Int -> m a
f Int
i
Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$ a -> Int -> Step Int a
forall a s. a -> s -> Step s a
Yield a
x (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
Done
cons :: Monad m => a -> Stream m a -> Stream m a
{-# INLINE cons #-}
cons :: a -> Stream m a -> Stream m a
cons a
x Stream m a
s = a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ Stream m a
s
snoc :: Monad m => Stream m a -> a -> Stream m a
{-# INLINE snoc #-}
snoc :: Stream m a -> a -> Stream m a
snoc Stream m a
s a
x = Stream m a
s Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x
infixr 5 ++
(++) :: Monad m => Stream m a -> Stream m a -> Stream m a
{-# INLINE_FUSED (++) #-}
Stream s -> m (Step s a)
stepa s
ta ++ :: Stream m a -> Stream m a -> Stream m a
++ Stream s -> m (Step s a)
stepb s
tb = (Either s s -> m (Step (Either s s) a)) -> Either s s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step (s -> Either s s
forall a b. a -> Either a b
Left s
ta)
where
{-# INLINE_INNER step #-}
step :: Either s s -> m (Step (Either s s) a)
step (Left s
sa) = do
Step s a
r <- s -> m (Step s a)
stepa s
sa
case Step s a
r of
Yield a
x s
sa' -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. a -> Either a b
Left s
sa')
Skip s
sa' -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. a -> Either a b
Left s
sa')
Step s a
Done -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
tb)
step (Right s
sb) = do
Step s a
r <- s -> m (Step s a)
stepb s
sb
case Step s a
r of
Yield a
x s
sb' -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. b -> Either a b
Right s
sb')
Skip s
sb' -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
sb')
Step s a
Done -> Step (Either s s) a -> m (Step (Either s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Step (Either s s) a
forall s a. Step s a
Done
head :: (HasCallStack, Monad m) => Stream m a -> m a
{-# INLINE_FUSED head #-}
head :: Stream m a -> m a
head (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
head_loop SPEC
SPEC s
t
where
head_loop :: SPEC -> s -> m a
head_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
_ -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
Skip s
s' -> SPEC -> s -> m a
head_loop SPEC
SPEC s
s'
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
last :: (HasCallStack, Monad m) => Stream m a -> m a
{-# INLINE_FUSED last #-}
last :: Stream m a -> m a
last (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
last_loop0 SPEC
SPEC s
t
where
last_loop0 :: SPEC -> s -> m a
last_loop0 !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
x s
s'
Skip s
s' -> SPEC -> s -> m a
last_loop0 SPEC
SPEC s
s'
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
last_loop1 :: SPEC -> a -> s -> m a
last_loop1 !SPEC
_ a
x s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
y s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
y s
s'
Skip s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
x s
s'
Step s a
Done -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
infixl 9 !!
(!!) :: (HasCallStack, Monad m) => Stream m a -> Int -> m a
{-# INLINE (!!) #-}
Stream s -> m (Step s a)
step s
t !! :: Stream m a -> Int -> m a
!! Int
j | Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = String -> m a
forall a. HasCallStack => String -> a
error (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ String
"negative index (" String -> String -> String
forall a. [a] -> [a] -> [a]
Prelude.++ Int -> String
forall a. Show a => a -> String
show Int
j String -> String -> String
forall a. [a] -> [a] -> [a]
Prelude.++ String
")"
| Bool
otherwise = SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
t Int
j
where
index_loop :: SPEC -> s -> Int -> m a
index_loop !SPEC
_ s
s Int
i
= Int
i Int -> m a -> m a
`seq`
do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
| Bool
otherwise -> SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
s' (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
Skip s
s' -> SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
s' Int
i
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
infixl 9 !?
(!?) :: Monad m => Stream m a -> Int -> m (Maybe a)
{-# INLINE (!?) #-}
Stream s -> m (Step s a)
step s
t !? :: Stream m a -> Int -> m (Maybe a)
!? Int
j = SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
t Int
j
where
index_loop :: SPEC -> s -> Int -> m (Maybe a)
index_loop !SPEC
_ s
s Int
i
= Int
i Int -> m (Maybe a) -> m (Maybe a)
`seq`
do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
| Bool
otherwise -> SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
s' (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
Skip s
s' -> SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
s' Int
i
Step s a
Done -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
slice :: Monad m => Int
-> Int
-> Stream m a
-> Stream m a
{-# INLINE slice #-}
slice :: Int -> Int -> Stream m a -> Stream m a
slice Int
i Int
n Stream m a
s = Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
take Int
n (Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
drop Int
i Stream m a
s)
init :: (HasCallStack, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED init #-}
init :: Stream m a -> Stream m a
init (Stream s -> m (Step s a)
step s
t) = ((Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
forall a. Maybe a
Nothing, s
t)
where
{-# INLINE_INNER step' #-}
step' :: (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
Nothing, s
s) = (Step s a -> Step (Maybe a, s) a)
-> m (Step s a) -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
s' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s')
Skip s
s' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (Maybe a
forall a. Maybe a
Nothing, s
s')
Step s a
Done -> String -> Step (Maybe a, s) a
forall a. HasCallStack => String -> a
error String
emptyStream
) (s -> m (Step s a)
step s
s)
step' (Just a
x, s
s) = (Step s a -> Step (Maybe a, s) a)
-> m (Step s a) -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
y s
s' -> a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
y, s
s')
Skip s
s' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s')
Step s a
Done -> Step (Maybe a, s) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
tail :: (HasCallStack, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED tail #-}
tail :: Stream m a -> Stream m a
tail (Stream s -> m (Step s a)
step s
t) = (Either s s -> m (Step (Either s s) a)) -> Either s s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step' (s -> Either s s
forall a b. a -> Either a b
Left s
t)
where
{-# INLINE_INNER step' #-}
step' :: Either s s -> m (Step (Either s s) a)
step' (Left s
s) = (Step s a -> Step (Either s s) a)
-> m (Step s a) -> m (Step (Either s s) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
_ s
s' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
s')
Skip s
s' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. a -> Either a b
Left s
s')
Step s a
Done -> String -> Step (Either s s) a
forall a. HasCallStack => String -> a
error String
emptyStream
) (s -> m (Step s a)
step s
s)
step' (Right s
s) = (Step s a -> Step (Either s s) a)
-> m (Step s a) -> m (Step (Either s s) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
s' -> a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. b -> Either a b
Right s
s')
Skip s
s' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
s')
Step s a
Done -> Step (Either s s) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
take :: Monad m => Int -> Stream m a -> Stream m a
{-# INLINE_FUSED take #-}
take :: Int -> Stream m a -> Stream m a
take Int
n (Stream s -> m (Step s a)
step s
t) = Int
n Int -> Stream m a -> Stream m a
`seq` ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step' (s
t, Int
0)
where
{-# INLINE_INNER step' #-}
step' :: (s, Int) -> m (Step (s, Int) a)
step' (s
s, Int
i) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = (Step s a -> Step (s, Int) a)
-> m (Step s a) -> m (Step (s, Int) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
s' -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Skip s
s' -> (s, Int) -> Step (s, Int) a
forall s a. s -> Step s a
Skip (s
s', Int
i)
Step s a
Done -> Step (s, Int) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
step' (s
_, Int
_) = Step (s, Int) a -> m (Step (s, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
forall s a. Step s a
Done
drop :: Monad m => Int -> Stream m a -> Stream m a
{-# INLINE_FUSED drop #-}
drop :: Int -> Stream m a -> Stream m a
drop Int
n (Stream s -> m (Step s a)
step s
t) = ((s, Maybe Int) -> m (Step (s, Maybe Int) a))
-> (s, Maybe Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe Int) -> m (Step (s, Maybe Int) a)
step' (s
t, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n)
where
{-# INLINE_INNER step' #-}
step' :: (s, Maybe Int) -> m (Step (s, Maybe Int) a)
step' (s
s, Just Int
i) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = (Step s a -> Step (s, Maybe Int) a)
-> m (Step s a) -> m (Step (s, Maybe Int) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
_ s
s' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s', Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
Skip s
s' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s', Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i)
Step s a
Done -> Step (s, Maybe Int) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
| Bool
otherwise = Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a))
-> Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s, Maybe Int
forall a. Maybe a
Nothing)
step' (s
s, Maybe Int
Nothing) = (Step s a -> Step (s, Maybe Int) a)
-> m (Step s a) -> m (Step (s, Maybe Int) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
s' -> a -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', Maybe Int
forall a. Maybe a
Nothing)
Skip s
s' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s', Maybe Int
forall a. Maybe a
Nothing)
Step s a
Done -> Step (s, Maybe Int) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
instance Monad m => Functor (Stream m) where
{-# INLINE fmap #-}
fmap :: (a -> b) -> Stream m a -> Stream m b
fmap = (a -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map
map :: Monad m => (a -> b) -> Stream m a -> Stream m b
{-# INLINE map #-}
map :: (a -> b) -> Stream m a -> Stream m b
map a -> b
f = (a -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (a -> b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapM #-}
mapM :: (a -> m b) -> Stream m a -> Stream m b
mapM a -> m b
f (Stream s -> m (Step s a)
step s
t) = (s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s b)
step' s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> (b -> Step s b) -> m b -> m (Step s b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (b -> s -> Step s b
forall a s. a -> s -> Step s a
`Yield` s
s') (a -> m b
f a
x)
Skip s
s' -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> Step s b
forall s a. s -> Step s a
Skip s
s')
Step s a
Done -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
forall s a. Step s a
Done
consume :: Monad m => Stream m a -> m ()
{-# INLINE_FUSED consume #-}
consume :: Stream m a -> m ()
consume (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m ()
consume_loop SPEC
SPEC s
t
where
consume_loop :: SPEC -> s -> m ()
consume_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
_ s
s' -> SPEC -> s -> m ()
consume_loop SPEC
SPEC s
s'
Skip s
s' -> SPEC -> s -> m ()
consume_loop SPEC
SPEC s
s'
Step s a
Done -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
{-# INLINE_FUSED mapM_ #-}
mapM_ :: (a -> m b) -> Stream m a -> m ()
mapM_ a -> m b
m = Stream m b -> m ()
forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume (Stream m b -> m ())
-> (Stream m a -> Stream m b) -> Stream m a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM a -> m b
m
trans :: (Monad m, Monad m')
=> (forall z. m z -> m' z) -> Stream m a -> Stream m' a
{-# INLINE_FUSED trans #-}
trans :: (forall z. m z -> m' z) -> Stream m a -> Stream m' a
trans forall z. m z -> m' z
f (Stream s -> m (Step s a)
step s
s) = (s -> m' (Step s a)) -> s -> Stream m' a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (m (Step s a) -> m' (Step s a)
forall z. m z -> m' z
f (m (Step s a) -> m' (Step s a))
-> (s -> m (Step s a)) -> s -> m' (Step s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m (Step s a)
step) s
s
unbox :: Monad m => Stream m (Box a) -> Stream m a
{-# INLINE_FUSED unbox #-}
unbox :: Stream m (Box a) -> Stream m a
unbox (Stream s -> m (Step s (Box a))
step s
t) = (s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s a)
step' s
s = do
Step s (Box a)
r <- s -> m (Step s (Box a))
step s
s
case Step s (Box a)
r of
Yield (Box a
x) s
s' -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
Skip s
s' -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s'
Step s (Box a)
Done -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step s a
forall s a. Step s a
Done
indexed :: Monad m => Stream m a -> Stream m (Int,a)
{-# INLINE_FUSED indexed #-}
indexed :: Stream m a -> Stream m (Int, a)
indexed (Stream s -> m (Step s a)
step s
t) = ((s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
t,Int
0)
where
{-# INLINE_INNER step' #-}
step' :: (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
s,Int
i) = Int
i Int -> m (Step (s, Int) (Int, a)) -> m (Step (s, Int) (Int, a))
`seq`
do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (Int, a) -> (s, Int) -> Step (s, Int) (Int, a)
forall a s. a -> s -> Step s a
Yield (Int
i,a
x) (s
s', Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Skip s
s' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (s, Int) -> Step (s, Int) (Int, a)
forall s a. s -> Step s a
Skip (s
s', Int
i)
Step s a
Done -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) (Int, a)
forall s a. Step s a
Done
indexedR :: Monad m => Int -> Stream m a -> Stream m (Int,a)
{-# INLINE_FUSED indexedR #-}
indexedR :: Int -> Stream m a -> Stream m (Int, a)
indexedR Int
m (Stream s -> m (Step s a)
step s
t) = ((s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
t,Int
m)
where
{-# INLINE_INNER step' #-}
step' :: (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
s,Int
i) = Int
i Int -> m (Step (s, Int) (Int, a)) -> m (Step (s, Int) (Int, a))
`seq`
do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> let i' :: Int
i' = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1
in
Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (Int, a) -> (s, Int) -> Step (s, Int) (Int, a)
forall a s. a -> s -> Step s a
Yield (Int
i',a
x) (s
s', Int
i')
Skip s
s' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (s, Int) -> Step (s, Int) (Int, a)
forall s a. s -> Step s a
Skip (s
s', Int
i)
Step s a
Done -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) (Int, a)
forall s a. Step s a
Done
zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
{-# INLINE_FUSED zipWithM #-}
zipWithM :: (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f (Stream s -> m (Step s a)
stepa s
ta) (Stream s -> m (Step s b)
stepb s
tb) = ((s, s, Maybe a) -> m (Step (s, s, Maybe a) c))
-> (s, s, Maybe a) -> Stream m c
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step (s
ta, s
tb, Maybe a
forall a. Maybe a
Nothing)
where
{-# INLINE_INNER step #-}
step :: (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step (s
sa, s
sb, Maybe a
Nothing) = (Step s a -> Step (s, s, Maybe a) c)
-> m (Step s a) -> m (Step (s, s, Maybe a) c)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
sa' -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa', s
sb, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Skip s
sa' -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa', s
sb, Maybe a
forall a. Maybe a
Nothing)
Step s a
Done -> Step (s, s, Maybe a) c
forall s a. Step s a
Done
) (s -> m (Step s a)
stepa s
sa)
step (s
sa, s
sb, Just a
x) = do
Step s b
r <- s -> m (Step s b)
stepb s
sb
case Step s b
r of
Yield b
y s
sb' ->
do
c
z <- a -> b -> m c
f a
x b
y
Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c))
-> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a b. (a -> b) -> a -> b
$ c -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall a s. a -> s -> Step s a
Yield c
z (s
sa, s
sb', Maybe a
forall a. Maybe a
Nothing)
Skip s
sb' -> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c))
-> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a b. (a -> b) -> a -> b
$ (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa, s
sb', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Step s b
Done -> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, s, Maybe a) c
forall s a. Step s a
Done
zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
zipWithM_ a -> b -> m c
f Stream m a
sa Stream m b
sb = Stream m c -> m ()
forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume ((a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f Stream m a
sa Stream m b
sb)
zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
{-# INLINE_FUSED zipWith3M #-}
zipWith3M :: (a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3M a -> b -> c -> m d
f (Stream s -> m (Step s a)
stepa s
ta)
(Stream s -> m (Step s b)
stepb s
tb)
(Stream s -> m (Step s c)
stepc s
tc) = ((s, s, s, Maybe (a, Maybe b))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> (s, s, s, Maybe (a, Maybe b)) -> Stream m d
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, s, s, Maybe (a, Maybe b))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
step (s
ta, s
tb, s
tc, Maybe (a, Maybe b)
forall a. Maybe a
Nothing)
where
{-# INLINE_INNER step #-}
step :: (s, s, s, Maybe (a, Maybe b))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
step (s
sa, s
sb, s
sc, Maybe (a, Maybe b)
Nothing) = do
Step s a
r <- s -> m (Step s a)
stepa s
sa
Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
Yield a
x s
sa' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, Maybe b
forall a. Maybe a
Nothing))
Skip s
sa' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, Maybe (a, Maybe b)
forall a. Maybe a
Nothing)
Step s a
Done -> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. Step s a
Done
step (s
sa, s
sb, s
sc, Just (a
x, Maybe b
Nothing)) = do
Step s b
r <- s -> m (Step s b)
stepb s
sb
Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
y s
sb' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, b -> Maybe b
forall a. a -> Maybe a
Just b
y))
Skip s
sb' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, Maybe b
forall a. Maybe a
Nothing))
Step s b
Done -> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. Step s a
Done
step (s
sa, s
sb, s
sc, Just (a
x, Just b
y)) = do
Step s c
r <- s -> m (Step s c)
stepc s
sc
case Step s c
r of
Yield c
z s
sc' -> a -> b -> c -> m d
f a
x b
y c
z m d
-> (d -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\d
res -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ d
-> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall a s. a -> s -> Step s a
Yield d
res (s
sa, s
sb, s
sc', Maybe (a, Maybe b)
forall a. Maybe a
Nothing))
Skip s
sc' -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb, s
sc', (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, b -> Maybe b
forall a. a -> Maybe a
Just b
y))
Step s c
Done -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. Step s a
Done
zipWith4M :: Monad m => (a -> b -> c -> d -> m e)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e
{-# INLINE zipWith4M #-}
zipWith4M :: (a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4M a -> b -> c -> d -> m e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd
= ((a, b) -> (c, d) -> m e)
-> Stream m (a, b) -> Stream m (c, d) -> Stream m e
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b) (c
c,d
d) -> a -> b -> c -> d -> m e
f a
a b
b c
c d
d) (Stream m a -> Stream m b -> Stream m (a, b)
forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m a
sa Stream m b
sb) (Stream m c -> Stream m d -> Stream m (c, d)
forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m c
sc Stream m d
sd)
zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m f
{-# INLINE zipWith5M #-}
zipWith5M :: (a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5M a -> b -> c -> d -> e -> m f
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se
= ((a, b, c) -> (d, e) -> m f)
-> Stream m (a, b, c) -> Stream m (d, e) -> Stream m f
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b,c
c) (d
d,e
e) -> a -> b -> c -> d -> e -> m f
f a
a b
b c
c d
d e
e) (Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m a
sa Stream m b
sb Stream m c
sc) (Stream m d -> Stream m e -> Stream m (d, e)
forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m d
sd Stream m e
se)
zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m f -> Stream m g
{-# INLINE zipWith6M #-}
zipWith6M :: (a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
fn Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf
= ((a, b, c) -> (d, e, f) -> m g)
-> Stream m (a, b, c) -> Stream m (d, e, f) -> Stream m g
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b,c
c) (d
d,e
e,f
f) -> a -> b -> c -> d -> e -> f -> m g
fn a
a b
b c
c d
d e
e f
f) (Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m a
sa Stream m b
sb Stream m c
sc)
(Stream m d -> Stream m e -> Stream m f -> Stream m (d, e, f)
forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m d
sd Stream m e
se Stream m f
sf)
zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
{-# INLINE zipWith #-}
zipWith :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith a -> b -> c
f = (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\a
a b
b -> c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c
f a
a b
b))
zipWith3 :: Monad m => (a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
{-# INLINE zipWith3 #-}
zipWith3 :: (a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3 a -> b -> c -> d
f = (a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3M (\a
a b
b c
c -> d -> m d
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d
f a
a b
b c
c))
zipWith4 :: Monad m => (a -> b -> c -> d -> e)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e
{-# INLINE zipWith4 #-}
zipWith4 :: (a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4 a -> b -> c -> d -> e
f = (a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4M (\a
a b
b c
c d
d -> e -> m e
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e
f a
a b
b c
c d
d))
zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m f
{-# INLINE zipWith5 #-}
zipWith5 :: (a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5 a -> b -> c -> d -> e -> f
f = (a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5M (\a
a b
b c
c d
d e
e -> f -> m f
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e -> f
f a
a b
b c
c d
d e
e))
zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m f -> Stream m g
{-# INLINE zipWith6 #-}
zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6 a -> b -> c -> d -> e -> f -> g
fn = (a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6M (\a
a b
b c
c d
d e
e f
f -> g -> m g
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e -> f -> g
fn a
a b
b c
c d
d e
e f
f))
zip :: Monad m => Stream m a -> Stream m b -> Stream m (a,b)
{-# INLINE zip #-}
zip :: Stream m a -> Stream m b -> Stream m (a, b)
zip = (a -> b -> (a, b)) -> Stream m a -> Stream m b -> Stream m (a, b)
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith (,)
zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a,b,c)
{-# INLINE zip3 #-}
zip3 :: Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 = (a -> b -> c -> (a, b, c))
-> Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3 (,,)
zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m (a,b,c,d)
{-# INLINE zip4 #-}
zip4 :: Stream m a
-> Stream m b -> Stream m c -> Stream m d -> Stream m (a, b, c, d)
zip4 = (a -> b -> c -> d -> (a, b, c, d))
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m (a, b, c, d)
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4 (,,,)
zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m (a,b,c,d,e)
{-# INLINE zip5 #-}
zip5 :: Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m (a, b, c, d, e)
zip5 = (a -> b -> c -> d -> e -> (a, b, c, d, e))
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m (a, b, c, d, e)
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5 (,,,,)
zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
-> Stream m e -> Stream m f -> Stream m (a,b,c,d,e,f)
{-# INLINE zip6 #-}
zip6 :: Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m (a, b, c, d, e, f)
zip6 = (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m (a, b, c, d, e, f)
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6 (,,,,,)
eqBy :: (Monad m) => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
{-# INLINE_FUSED eqBy #-}
eqBy :: (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
eqBy a -> b -> Bool
eq (Stream s -> m (Step s a)
step1 s
t1) (Stream s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
t1 s
t2
where
eq_loop0 :: SPEC -> s -> s -> m Bool
eq_loop0 !SPEC
_ s
s1 s
s2 = do
Step s a
r <- s -> m (Step s a)
step1 s
s1
case Step s a
r of
Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1' s
s2
Skip s
s1' -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
s1' s
s2
Step s a
Done -> s -> m Bool
eq_null s
s2
eq_loop1 :: SPEC -> a -> s -> s -> m Bool
eq_loop1 !SPEC
_ a
x s
s1 s
s2 = do
Step s b
r <- s -> m (Step s b)
step2 s
s2
case Step s b
r of
Yield b
y s
s2'
| a -> b -> Bool
eq a
x b
y -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
s1 s
s2'
| Bool
otherwise -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Skip s
s2' -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1 s
s2'
Step s b
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
eq_null :: s -> m Bool
eq_null s
s2 = do
Step s b
r <- s -> m (Step s b)
step2 s
s2
case Step s b
r of
Yield b
_ s
_ -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Skip s
s2' -> s -> m Bool
eq_null s
s2'
Step s b
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
{-# INLINE_FUSED cmpBy #-}
cmpBy :: (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
cmpBy a -> b -> Ordering
cmp (Stream s -> m (Step s a)
step1 s
t1) (Stream s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
t1 s
t2
where
cmp_loop0 :: SPEC -> s -> s -> m Ordering
cmp_loop0 !SPEC
_ s
s1 s
s2 = do
Step s a
r <- s -> m (Step s a)
step1 s
s1
case Step s a
r of
Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1' s
s2
Skip s
s1' -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
s1' s
s2
Step s a
Done -> s -> m Ordering
cmp_null s
s2
cmp_loop1 :: SPEC -> a -> s -> s -> m Ordering
cmp_loop1 !SPEC
_ a
x s
s1 s
s2 = do
Step s b
r <- s -> m (Step s b)
step2 s
s2
case Step s b
r of
Yield b
y s
s2' -> case a
x a -> b -> Ordering
`cmp` b
y of
Ordering
EQ -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
s1 s
s2'
Ordering
c -> Ordering -> m Ordering
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
c
Skip s
s2' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1 s
s2'
Step s b
Done -> Ordering -> m Ordering
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
GT
cmp_null :: s -> m Ordering
cmp_null s
s2 = do
Step s b
r <- s -> m (Step s b)
step2 s
s2
case Step s b
r of
Yield b
_ s
_ -> Ordering -> m Ordering
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
LT
Skip s
s2' -> s -> m Ordering
cmp_null s
s2'
Step s b
Done -> Ordering -> m Ordering
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ
filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE filter #-}
filter :: (a -> Bool) -> Stream m a -> Stream m a
filter a -> Bool
f = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
mapMaybe :: Monad m => (a -> Maybe b) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapMaybe #-}
mapMaybe :: (a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe a -> Maybe b
f (Stream s -> m (Step s a)
step s
t) = (s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s b)
step' s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ case a -> Maybe b
f a
x of
Maybe b
Nothing -> s -> Step s b
forall s a. s -> Step s a
Skip s
s'
Just b
b' -> b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield b
b' s
s'
Skip s
s' -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
Skip s
s'
Step s a
Done -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ Step s b
forall s a. Step s a
Done
catMaybes :: Monad m => Stream m (Maybe a) -> Stream m a
catMaybes :: Stream m (Maybe a) -> Stream m a
catMaybes = (Maybe a -> Maybe a) -> Stream m (Maybe a) -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe Maybe a -> Maybe a
forall a. a -> a
id
filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED filterM #-}
filterM :: (a -> m Bool) -> Stream m a -> Stream m a
filterM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = (s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s a)
step' s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Bool
b <- a -> m Bool
f a
x
Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
else s -> Step s a
forall s a. s -> Step s a
Skip s
s'
Skip s
s' -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s'
Step s a
Done -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ Step s a
forall s a. Step s a
Done
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapMaybeM #-}
mapMaybeM :: (a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeM a -> m (Maybe b)
f (Stream s -> m (Step s a)
step s
t) = (s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s b)
step' s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Maybe b
fx <- a -> m (Maybe b)
f a
x
Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ case Maybe b
fx of
Maybe b
Nothing -> s -> Step s b
forall s a. s -> Step s a
Skip s
s'
Just b
b -> b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield b
b s
s'
Skip s
s' -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
Skip s
s'
Step s a
Done -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ Step s b
forall s a. Step s a
Done
uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED uniq #-}
uniq :: Stream m a -> Stream m a
uniq (Stream s -> m (Step s a)
step s
st) = ((Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
forall a. Maybe a
Nothing,s
st)
where
{-# INLINE_INNER step' #-}
step' :: (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
Nothing, s
s) = do Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x , s
s')
Skip s
s' -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (Maybe a
forall a. Maybe a
Nothing, s
s')
Step s a
Done -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe a, s) a
forall s a. Step s a
Done
step' (Just a
x0, s
s) = do Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x0 -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x0, s
s')
| Bool
otherwise -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x , s
s')
Skip s
s' -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x0, s
s')
Step s a
Done -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe a, s) a
forall s a. Step s a
Done
takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE takeWhile #-}
takeWhile :: (a -> Bool) -> Stream m a -> Stream m a
takeWhile a -> Bool
f = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED takeWhileM #-}
takeWhileM :: (a -> m Bool) -> Stream m a -> Stream m a
takeWhileM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = (s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
where
{-# INLINE_INNER step' #-}
step' :: s -> m (Step s a)
step' s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Bool
b <- a -> m Bool
f a
x
Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s' else Step s a
forall s a. Step s a
Done
Skip s
s' -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s'
Step s a
Done -> Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ Step s a
forall s a. Step s a
Done
dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE dropWhile #-}
dropWhile :: (a -> Bool) -> Stream m a -> Stream m a
dropWhile a -> Bool
f = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
data DropWhile s a = DropWhile_Drop s | DropWhile_Yield a s | DropWhile_Next s
dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED dropWhileM #-}
dropWhileM :: (a -> m Bool) -> Stream m a -> Stream m a
dropWhileM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = (DropWhile s a -> m (Step (DropWhile s a) a))
-> DropWhile s a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream DropWhile s a -> m (Step (DropWhile s a) a)
step' (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Drop s
t)
where
{-# INLINE_INNER step' #-}
step' :: DropWhile s a -> m (Step (DropWhile s a) a)
step' (DropWhile_Drop s
s)
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Bool
b <- a -> m Bool
f a
x
Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Drop s
s')
else DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (a -> s -> DropWhile s a
forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
Skip s
s' -> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Drop s
s')
Step s a
Done -> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ Step (DropWhile s a) a
forall s a. Step s a
Done
step' (DropWhile_Yield a
x s
s) = Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ a -> DropWhile s a -> Step (DropWhile s a) a
forall a s. a -> s -> Step s a
Yield a
x (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Next s
s)
step' (DropWhile_Next s
s)
= (Step s a -> Step (DropWhile s a) a)
-> m (Step s a) -> m (Step (DropWhile s a) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
case Step s a
r of
Yield a
x s
s' -> DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (a -> s -> DropWhile s a
forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
Skip s
s' -> DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Next s
s')
Step s a
Done -> Step (DropWhile s a) a
forall s a. Step s a
Done
) (s -> m (Step s a)
step s
s)
infix 4 `elem`
elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
{-# INLINE_FUSED elem #-}
elem :: a -> Stream m a -> m Bool
elem a
x (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
t
where
elem_loop :: SPEC -> s -> m Bool
elem_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
y s
s' | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
| Bool
otherwise -> SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
s'
Skip s
s' -> SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
s'
Step s a
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
infix 4 `notElem`
notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
{-# INLINE notElem #-}
notElem :: a -> Stream m a -> m Bool
notElem a
x Stream m a
s = (Bool -> Bool) -> m Bool -> m Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> Bool
not (a -> Stream m a -> m Bool
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
elem a
x Stream m a
s)
find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
{-# INLINE find #-}
find :: (a -> Bool) -> Stream m a -> m (Maybe a)
find a -> Bool
f = (a -> m Bool) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
findM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
{-# INLINE_FUSED findM #-}
findM :: (a -> m Bool) -> Stream m a -> m (Maybe a)
findM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
t
where
find_loop :: SPEC -> s -> m (Maybe a)
find_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Bool
b <- a -> m Bool
f a
x
if Bool
b then Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
x
else SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
s'
Skip s
s' -> SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
s'
Step s a
Done -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)
{-# INLINE_FUSED findIndex #-}
findIndex :: (a -> Bool) -> Stream m a -> m (Maybe Int)
findIndex a -> Bool
f = (a -> m Bool) -> Stream m a -> m (Maybe Int)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)
{-# INLINE_FUSED findIndexM #-}
findIndexM :: (a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
t Int
0
where
findIndex_loop :: SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop !SPEC
_ s
s Int
i
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> do
Bool
b <- a -> m Bool
f a
x
if Bool
b then Maybe Int -> m (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Int -> m (Maybe Int)) -> Maybe Int -> m (Maybe Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i
else SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
s' (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Skip s
s' -> SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
s' Int
i
Step s a
Done -> Maybe Int -> m (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
{-# INLINE foldl #-}
foldl :: (a -> b -> a) -> a -> Stream m b -> m a
foldl a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE_FUSED foldlM #-}
foldlM :: (a -> b -> m a) -> a -> Stream m b -> m a
foldlM a -> b -> m a
m a
w (Stream s -> m (Step s b)
step s
t) = SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
w s
t
where
foldlM_loop :: SPEC -> a -> s -> m a
foldlM_loop !SPEC
_ a
z s
s
= do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
x s
s' -> do { a
z' <- a -> b -> m a
m a
z b
x; SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
z' s
s' }
Skip s
s' -> SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
z s
s'
Step s b
Done -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
z
foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE foldM #-}
foldM :: (a -> b -> m a) -> a -> Stream m b -> m a
foldM = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM
foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldl1 #-}
foldl1 :: (a -> a -> a) -> Stream m a -> m a
foldl1 a -> a -> a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))
foldl1M :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M #-}
foldl1M :: (a -> a -> m a) -> Stream m a -> m a
foldl1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldl1M_loop SPEC
SPEC s
t
where
foldl1M_loop :: SPEC -> s -> m a
foldl1M_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> (a -> a -> m a) -> a -> Stream m a -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM a -> a -> m a
f a
x ((s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
s')
Skip s
s' -> SPEC -> s -> m a
foldl1M_loop SPEC
SPEC s
s'
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
fold1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE fold1M #-}
fold1M :: (a -> a -> m a) -> Stream m a -> m a
fold1M = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M
foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
{-# INLINE foldl' #-}
foldl' :: (a -> b -> a) -> a -> Stream m b -> m a
foldl' a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE_FUSED foldlM' #-}
foldlM' :: (a -> b -> m a) -> a -> Stream m b -> m a
foldlM' a -> b -> m a
m a
w (Stream s -> m (Step s b)
step s
t) = SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
w s
t
where
foldlM'_loop :: SPEC -> a -> s -> m a
foldlM'_loop !SPEC
_ a
z s
s
= a
z a -> m a -> m a
`seq`
do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
x s
s' -> do { a
z' <- a -> b -> m a
m a
z b
x; SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
z' s
s' }
Skip s
s' -> SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
z s
s'
Step s b
Done -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
z
foldM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE foldM' #-}
foldM' :: (a -> b -> m a) -> a -> Stream m b -> m a
foldM' = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM'
foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldl1' #-}
foldl1' :: (a -> a -> a) -> Stream m a -> m a
foldl1' a -> a -> a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M' (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))
foldl1M' :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M' #-}
foldl1M' :: (a -> a -> m a) -> Stream m a -> m a
foldl1M' a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldl1M'_loop SPEC
SPEC s
t
where
foldl1M'_loop :: SPEC -> s -> m a
foldl1M'_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> (a -> a -> m a) -> a -> Stream m a -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' a -> a -> m a
f a
x ((s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
s')
Skip s
s' -> SPEC -> s -> m a
foldl1M'_loop SPEC
SPEC s
s'
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
fold1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE fold1M' #-}
fold1M' :: (a -> a -> m a) -> Stream m a -> m a
fold1M' = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M'
foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
{-# INLINE foldr #-}
foldr :: (a -> b -> b) -> b -> Stream m a -> m b
foldr a -> b -> b
f = (a -> b -> m b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
foldrM (\a
a b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> b
f a
a b
b))
foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
{-# INLINE_FUSED foldrM #-}
foldrM :: (a -> b -> m b) -> b -> Stream m a -> m b
foldrM a -> b -> m b
f b
z (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
t
where
foldrM_loop :: SPEC -> s -> m b
foldrM_loop !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> a -> b -> m b
f a
x (b -> m b) -> m b -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
s'
Skip s
s' -> SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
s'
Step s a
Done -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
z
foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldr1 #-}
foldr1 :: (a -> a -> a) -> Stream m a -> m a
foldr1 a -> a -> a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldr1M (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))
foldr1M :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldr1M #-}
foldr1M :: (a -> a -> m a) -> Stream m a -> m a
foldr1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldr1M_loop0 SPEC
SPEC s
t
where
foldr1M_loop0 :: SPEC -> s -> m a
foldr1M_loop0 !SPEC
_ s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
x s
s'
Skip s
s' -> SPEC -> s -> m a
foldr1M_loop0 SPEC
SPEC s
s'
Step s a
Done -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream
foldr1M_loop1 :: SPEC -> a -> s -> m a
foldr1M_loop1 !SPEC
_ a
x s
s
= do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
y s
s' -> a -> a -> m a
f a
x (a -> m a) -> m a -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
y s
s'
Skip s
s' -> SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
x s
s'
Step s a
Done -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
and :: Monad m => Stream m Bool -> m Bool
{-# INLINE_FUSED and #-}
and :: Stream m Bool -> m Bool
and (Stream s -> m (Step s Bool)
step s
t) = SPEC -> s -> m Bool
and_loop SPEC
SPEC s
t
where
and_loop :: SPEC -> s -> m Bool
and_loop !SPEC
_ s
s
= do
Step s Bool
r <- s -> m (Step s Bool)
step s
s
case Step s Bool
r of
Yield Bool
False s
_ -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
Yield Bool
True s
s' -> SPEC -> s -> m Bool
and_loop SPEC
SPEC s
s'
Skip s
s' -> SPEC -> s -> m Bool
and_loop SPEC
SPEC s
s'
Step s Bool
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
or :: Monad m => Stream m Bool -> m Bool
{-# INLINE_FUSED or #-}
or :: Stream m Bool -> m Bool
or (Stream s -> m (Step s Bool)
step s
t) = SPEC -> s -> m Bool
or_loop SPEC
SPEC s
t
where
or_loop :: SPEC -> s -> m Bool
or_loop !SPEC
_ s
s
= do
Step s Bool
r <- s -> m (Step s Bool)
step s
s
case Step s Bool
r of
Yield Bool
False s
s' -> SPEC -> s -> m Bool
or_loop SPEC
SPEC s
s'
Yield Bool
True s
_ -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Skip s
s' -> SPEC -> s -> m Bool
or_loop SPEC
SPEC s
s'
Step s Bool
Done -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
{-# INLINE concatMap #-}
concatMap :: (a -> Stream m b) -> Stream m a -> Stream m b
concatMap a -> Stream m b
f = (a -> m (Stream m b)) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM (Stream m b -> m (Stream m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Stream m b -> m (Stream m b))
-> (a -> Stream m b) -> a -> m (Stream m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stream m b
f)
concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED concatMapM #-}
concatMapM :: (a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM a -> m (Stream m b)
f (Stream s -> m (Step s a)
step s
t) = (Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b))
-> Either s (Stream m b, s) -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b)
concatMap_go (s -> Either s (Stream m b, s)
forall a b. a -> Either a b
Left s
t)
where
concatMap_go :: Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b)
concatMap_go (Left s
s) = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
a s
s' -> do
Stream m b
b_stream <- a -> m (Stream m b)
f a
a
Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right (Stream m b
b_stream, s
s'))
Skip s
s' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (Stream m b, s)
forall a b. a -> Either a b
Left s
s')
Step s a
Done -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either s (Stream m b, s)) b
forall s a. Step s a
Done
concatMap_go (Right (Stream s -> m (Step s b)
inner_step s
inner_s, s
s)) = do
Step s b
r <- s -> m (Step s b)
inner_step s
inner_s
case Step s b
r of
Yield b
b s
inner_s' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ b -> Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall a s. a -> s -> Step s a
Yield b
b ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right ((s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
inner_step s
inner_s', s
s))
Skip s
inner_s' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right ((s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
inner_step s
inner_s', s
s))
Step s b
Done -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (Stream m b, s)
forall a b. a -> Either a b
Left s
s)
flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED flatten #-}
flatten :: (a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
flatten a -> m s
mk s -> m (Step s b)
istep (Stream s -> m (Step s a)
ostep s
u) = (Either s (s, s) -> m (Step (Either s (s, s)) b))
-> Either s (s, s) -> Stream m b
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s (s, s) -> m (Step (Either s (s, s)) b)
step (s -> Either s (s, s)
forall a b. a -> Either a b
Left s
u)
where
{-# INLINE_INNER step #-}
step :: Either s (s, s) -> m (Step (Either s (s, s)) b)
step (Left s
t) = do
Step s a
r <- s -> m (Step s a)
ostep s
t
case Step s a
r of
Yield a
a s
t' -> do
s
s <- a -> m s
mk a
a
s
s s -> m (Step (Either s (s, s)) b) -> m (Step (Either s (s, s)) b)
`seq` Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s,s
t')))
Skip s
t' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (s, s)
forall a b. a -> Either a b
Left s
t')
Step s a
Done -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Step (Either s (s, s)) b
forall s a. Step s a
Done
step (Right (s
s,s
t)) = do
Step s b
r <- s -> m (Step s b)
istep s
s
case Step s b
r of
Yield b
x s
s' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ b -> Either s (s, s) -> Step (Either s (s, s)) b
forall a s. a -> s -> Step s a
Yield b
x ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s',s
t))
Skip s
s' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s',s
t))
Step s b
Done -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (s, s)
forall a b. a -> Either a b
Left s
t)
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldr #-}
unfoldr :: (s -> Maybe (a, s)) -> s -> Stream m a
unfoldr s -> Maybe (a, s)
f = (s -> m (Maybe (a, s))) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM (Maybe (a, s) -> m (Maybe (a, s))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
{-# INLINE_FUSED unfoldrM #-}
unfoldrM :: (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM s -> m (Maybe (a, s))
f s
t = (s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
t
where
{-# INLINE_INNER step #-}
step :: s -> m (Step s a)
step s
s = (Maybe (a, s) -> Step s a) -> m (Maybe (a, s)) -> m (Step s a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Maybe (a, s)
r ->
case Maybe (a, s)
r of
Just (a
x, s
s') -> a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
Maybe (a, s)
Nothing -> Step s a
forall s a. Step s a
Done
) (s -> m (Maybe (a, s))
f s
s)
unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrN #-}
unfoldrN :: Int -> (s -> Maybe (a, s)) -> s -> Stream m a
unfoldrN Int
n s -> Maybe (a, s)
f = Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrNM Int
n (Maybe (a, s) -> m (Maybe (a, s))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
{-# INLINE_FUSED unfoldrNM #-}
unfoldrNM :: Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrNM Int
m s -> m (Maybe (a, s))
f s
t = ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step (s
t,Int
m)
where
{-# INLINE_INNER step #-}
step :: (s, Int) -> m (Step (s, Int) a)
step (s
s,Int
n) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Step (s, Int) a -> m (Step (s, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
forall s a. Step s a
Done
| Bool
otherwise = (Maybe (a, s) -> Step (s, Int) a)
-> m (Maybe (a, s)) -> m (Step (s, Int) a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Maybe (a, s)
r ->
case Maybe (a, s)
r of
Just (a
x,s
s') -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
Maybe (a, s)
Nothing -> Step (s, Int) a
forall s a. Step s a
Done
) (s -> m (Maybe (a, s))
f s
s)
unfoldrExactN :: Monad m => Int -> (s -> (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrExactN #-}
unfoldrExactN :: Int -> (s -> (a, s)) -> s -> Stream m a
unfoldrExactN Int
n s -> (a, s)
f = Int -> (s -> m (a, s)) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Stream m a
unfoldrExactNM Int
n ((a, s) -> m (a, s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, s) -> m (a, s)) -> (s -> (a, s)) -> s -> m (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> (a, s)
f)
unfoldrExactNM :: Monad m => Int -> (s -> m (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrExactNM #-}
unfoldrExactNM :: Int -> (s -> m (a, s)) -> s -> Stream m a
unfoldrExactNM Int
m s -> m (a, s)
f s
t = ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step (s
t,Int
m)
where
{-# INLINE_INNER step #-}
step :: (s, Int) -> m (Step (s, Int) a)
step (s
s,Int
n) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Step (s, Int) a -> m (Step (s, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
forall s a. Step s a
Done
| Bool
otherwise = do (a
x,s
s') <- s -> m (a, s)
f s
s
Step (s, Int) a -> m (Step (s, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) a -> m (Step (s, Int) a))
-> Step (s, Int) a -> m (Step (s, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
iterateNM :: Monad m => Int -> (a -> m a) -> a -> Stream m a
{-# INLINE_FUSED iterateNM #-}
iterateNM :: Int -> (a -> m a) -> a -> Stream m a
iterateNM Int
n a -> m a
f a
x0 = ((a, Int) -> m (Step (a, Int) a)) -> (a, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (a, Int) -> m (Step (a, Int) a)
step (a
x0,Int
n)
where
{-# INLINE_INNER step #-}
step :: (a, Int) -> m (Step (a, Int) a)
step (a
x,Int
i) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Step (a, Int) a -> m (Step (a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (a, Int) a
forall s a. Step s a
Done
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = Step (a, Int) a -> m (Step (a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
x (a
x,Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = do a
a <- a -> m a
f a
x
Step (a, Int) a -> m (Step (a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
a (a
a,Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
iterateN :: Monad m => Int -> (a -> a) -> a -> Stream m a
{-# INLINE_FUSED iterateN #-}
iterateN :: Int -> (a -> a) -> a -> Stream m a
iterateN Int
n a -> a
f a
x0 = Int -> (a -> m a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
iterateNM Int
n (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f) a
x0
prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE prescanl #-}
prescanl :: (a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED prescanlM #-}
prescanlM :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
where
{-# INLINE_INNER step' #-}
step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
y s
s' -> do
a
z <- a -> b -> m a
f a
x b
y
Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
Skip s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s', a
x)
Step s b
Done -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
forall s a. Step s a
Done
prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE prescanl' #-}
prescanl' :: (a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl' a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED prescanlM' #-}
prescanlM' :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
where
{-# INLINE_INNER step' #-}
step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = a
x a -> m (Step (s, a) a) -> m (Step (s, a) a)
`seq`
do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
y s
s' -> do
a
z <- a -> b -> m a
f a
x b
y
Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
Skip s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s', a
x)
Step s b
Done -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
forall s a. Step s a
Done
postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE postscanl #-}
postscanl :: (a -> b -> a) -> a -> Stream m b -> Stream m a
postscanl a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
postscanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED postscanlM #-}
postscanlM :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
where
{-# INLINE_INNER step' #-}
step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
y s
s' -> do
a
z <- a -> b -> m a
f a
x b
y
Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z)
Skip s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s',a
x)
Step s b
Done -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
forall s a. Step s a
Done
postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE postscanl' #-}
postscanl' :: (a -> b -> a) -> a -> Stream m b -> Stream m a
postscanl' a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM' (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
postscanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED postscanlM' #-}
postscanlM' :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM' a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = a
w a -> Stream m a -> Stream m a
`seq` ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
where
{-# INLINE_INNER step' #-}
step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = a
x a -> m (Step (s, a) a) -> m (Step (s, a) a)
`seq`
do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
Yield b
y s
s' -> do
a
z <- a -> b -> m a
f a
x b
y
a
z a -> m (Step (s, a) a) -> m (Step (s, a) a)
`seq` Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z))
Skip s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s',a
x)
Step s b
Done -> Step (s, a) a -> m (Step (s, a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
forall s a. Step s a
Done
scanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE scanl #-}
scanl :: (a -> b -> a) -> a -> Stream m b -> Stream m a
scanl a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
scanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE scanlM #-}
scanlM :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM a -> b -> m a
f a
z Stream m b
s = a
z a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
z Stream m b
s
scanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE scanl' #-}
scanl' :: (a -> b -> a) -> a -> Stream m b -> Stream m a
scanl' a -> b -> a
f = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM' (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))
scanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE scanlM' #-}
scanlM' :: (a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM' a -> b -> m a
f a
z Stream m b
s = a
z a -> Stream m a -> Stream m a
`seq` (a
z a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
z Stream m b
s)
scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
{-# INLINE scanl1 #-}
scanl1 :: (a -> a -> a) -> Stream m a -> Stream m a
scanl1 a -> a -> a
f = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M (\a
x a
y -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))
scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
{-# INLINE_FUSED scanl1M #-}
scanl1M :: (a -> a -> m a) -> Stream m a -> Stream m a
scanl1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = ((s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
t, Maybe a
forall a. Maybe a
Nothing)
where
{-# INLINE_INNER step' #-}
step' :: (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
s, Maybe a
Nothing) = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Skip s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', Maybe a
forall a. Maybe a
Nothing)
Step s a
Done -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done
step' (s
s, Just a
x) = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
y s
s' -> do
a
z <- a -> a -> m a
f a
x a
y
Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
z)
Skip s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Step s a
Done -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done
scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
{-# INLINE scanl1' #-}
scanl1' :: (a -> a -> a) -> Stream m a -> Stream m a
scanl1' a -> a -> a
f = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' (\a
x a
y -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))
scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
{-# INLINE_FUSED scanl1M' #-}
scanl1M' :: (a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = ((s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
t, Maybe a
forall a. Maybe a
Nothing)
where
{-# INLINE_INNER step' #-}
step' :: (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
s, Maybe a
Nothing) = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
x s
s' -> a
x a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x))
Skip s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', Maybe a
forall a. Maybe a
Nothing)
Step s a
Done -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done
step' (s
s, Just a
x) = a
x a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
`seq`
do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
Yield a
y s
s' -> do
a
z <- a -> a -> m a
f a
x a
y
a
z a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
z))
Skip s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Step s a
Done -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done
enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Stream m a
{-# INLINE_FUSED enumFromStepN #-}
enumFromStepN :: a -> a -> Int -> Stream m a
enumFromStepN a
x a
y Int
n = a
x a -> Stream m a -> Stream m a
`seq` a
y a -> Stream m a -> Stream m a
`seq` Int
n Int -> Stream m a -> Stream m a
`seq` ((a, Int) -> m (Step (a, Int) a)) -> (a, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (a, Int) -> m (Step (a, Int) a)
step (a
x,Int
n)
where
{-# INLINE_INNER step #-}
step :: (a, Int) -> m (Step (a, Int) a)
step (a
w,Int
m) | Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Step (a, Int) a -> m (Step (a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
w (a
wa -> a -> a
forall a. Num a => a -> a -> a
+a
y,Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
| Bool
otherwise = Step (a, Int) a -> m (Step (a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ Step (a, Int) a
forall s a. Step s a
Done
enumFromTo :: (Enum a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo #-}
enumFromTo :: a -> a -> Stream m a
enumFromTo a
x a
y = [a] -> Stream m a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
fromList [a
x .. a
y]
enumFromTo_small :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_small #-}
enumFromTo_small :: a -> a -> Stream m a
enumFromTo_small a
x a
y = a
x a -> Stream m a -> Stream m a
`seq` a
y a -> Stream m a -> Stream m a
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
where
{-# INLINE_INNER step #-}
step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
| a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
| Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
{-# RULES
"enumFromTo<Int8> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Int8 -> Int8 -> Stream m Int8
"enumFromTo<Int16> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Int16 -> Int16 -> Stream m Int16
"enumFromTo<Word8> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Word8 -> Word8 -> Stream m Word8
"enumFromTo<Word16> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Word16 -> Word16 -> Stream m Word16 #-}
#if WORD_SIZE_IN_BITS > 32
{-# RULES
"enumFromTo<Int32> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Int32 -> Int32 -> Stream m Int32
"enumFromTo<Word32> [Stream]"
enumFromTo = enumFromTo_small :: Monad m => Word32 -> Word32 -> Stream m Word32 #-}
#endif
enumFromTo_int :: forall m. Monad m => Int -> Int -> Stream m Int
{-# INLINE_FUSED enumFromTo_int #-}
enumFromTo_int :: Int -> Int -> Stream m Int
enumFromTo_int Int
x Int
y = Int
x Int -> Stream m Int -> Stream m Int
`seq` Int
y Int -> Stream m Int -> Stream m Int
`seq` (Maybe Int -> m (Step (Maybe Int) Int))
-> Maybe Int -> Stream m Int
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe Int -> m (Step (Maybe Int) Int)
step (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
x)
where
{-# INLINE_INNER step #-}
step :: Maybe Int -> m (Step (Maybe Int) Int)
step Maybe Int
Nothing = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Step (Maybe Int) Int
forall s a. Step s a
Done
step (Just Int
z) | Int
z Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Step (Maybe Int) Int
forall a s. a -> s -> Step s a
Yield Int
z Maybe Int
forall a. Maybe a
Nothing
| Int
z Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
y = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Step (Maybe Int) Int
forall a s. a -> s -> Step s a
Yield Int
z (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
zInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1))
| Bool
otherwise = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Step (Maybe Int) Int
forall s a. Step s a
Done
enumFromTo_intlike :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_intlike #-}
enumFromTo_intlike :: a -> a -> Stream m a
enumFromTo_intlike a
x a
y = a
x a -> Stream m a -> Stream m a
`seq` a
y a -> Stream m a -> Stream m a
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
where
{-# INLINE_INNER step #-}
step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
| a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
| Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
{-# RULES
"enumFromTo<Int> [Stream]"
enumFromTo = enumFromTo_int :: Monad m => Int -> Int -> Stream m Int
#if WORD_SIZE_IN_BITS > 32
"enumFromTo<Int64> [Stream]"
enumFromTo = enumFromTo_intlike :: Monad m => Int64 -> Int64 -> Stream m Int64 #-}
#else
"enumFromTo<Int32> [Stream]"
enumFromTo = enumFromTo_intlike :: Monad m => Int32 -> Int32 -> Stream m Int32 #-}
#endif
enumFromTo_big_word :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_big_word #-}
enumFromTo_big_word :: a -> a -> Stream m a
enumFromTo_big_word a
x a
y = a
x a -> Stream m a -> Stream m a
`seq` a
y a -> Stream m a -> Stream m a
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
where
{-# INLINE_INNER step #-}
step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
| a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
| Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
{-# RULES
"enumFromTo<Word> [Stream]"
enumFromTo = enumFromTo_big_word :: Monad m => Word -> Word -> Stream m Word
"enumFromTo<Word64> [Stream]"
enumFromTo = enumFromTo_big_word
:: Monad m => Word64 -> Word64 -> Stream m Word64
#if WORD_SIZE_IN_BITS == 32
"enumFromTo<Word32> [Stream]"
enumFromTo = enumFromTo_big_word
:: Monad m => Word32 -> Word32 -> Stream m Word32
#endif
"enumFromTo<Integer> [Stream]"
enumFromTo = enumFromTo_big_word
:: Monad m => Integer -> Integer -> Stream m Integer #-}
#if WORD_SIZE_IN_BITS > 32
enumFromTo_big_int :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_big_int #-}
enumFromTo_big_int :: a -> a -> Stream m a
enumFromTo_big_int a
x a
y = a
x a -> Stream m a -> Stream m a
`seq` a
y a -> Stream m a -> Stream m a
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
where
{-# INLINE_INNER step #-}
step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
| a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
| Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
{-# RULES
"enumFromTo<Int64> [Stream]"
enumFromTo = enumFromTo_big_int :: Monad m => Int64 -> Int64 -> Stream m Int64 #-}
#endif
enumFromTo_char :: Monad m => Char -> Char -> Stream m Char
{-# INLINE_FUSED enumFromTo_char #-}
enumFromTo_char :: Char -> Char -> Stream m Char
enumFromTo_char Char
x Char
y = Char
x Char -> Stream m Char -> Stream m Char
`seq` Char
y Char -> Stream m Char -> Stream m Char
`seq` (Int -> m (Step Int Char)) -> Int -> Stream m Char
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int Char)
step Int
xn
where
xn :: Int
xn = Char -> Int
ord Char
x
yn :: Int
yn = Char -> Int
ord Char
y
{-# INLINE_INNER step #-}
step :: Int -> m (Step Int Char)
step Int
zn | Int
zn Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
yn = Step Int Char -> m (Step Int Char)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int Char -> m (Step Int Char))
-> Step Int Char -> m (Step Int Char)
forall a b. (a -> b) -> a -> b
$ Char -> Int -> Step Int Char
forall a s. a -> s -> Step s a
Yield (Int -> Char
unsafeChr Int
zn) (Int
znInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = Step Int Char -> m (Step Int Char)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int Char -> m (Step Int Char))
-> Step Int Char -> m (Step Int Char)
forall a b. (a -> b) -> a -> b
$ Step Int Char
forall s a. Step s a
Done
{-# RULES
"enumFromTo<Char> [Stream]"
enumFromTo = enumFromTo_char #-}
enumFromTo_double :: (Monad m, Ord a, RealFrac a) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_double #-}
enumFromTo_double :: a -> a -> Stream m a
enumFromTo_double a
n a
m = a
n a -> Stream m a -> Stream m a
`seq` a
m a -> Stream m a -> Stream m a
`seq` (a -> m (Step a a)) -> a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream a -> m (Step a a)
step a
ini
where
lim :: a
lim = a
m a -> a -> a
forall a. Num a => a -> a -> a
+ a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2
#if MIN_VERSION_base(4,12,0)
ini :: a
ini = a
0
step :: a -> m (Step a a)
step a
x | a
x' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
lim = Step a a -> m (Step a a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ a -> a -> Step a a
forall a s. a -> s -> Step s a
Yield a
x' (a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1)
| Bool
otherwise = Step a a -> m (Step a a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ Step a a
forall s a. Step s a
Done
where
x' :: a
x' = a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
n
#else
ini = n
step x | x <= lim = return $ Yield x (x+1)
| otherwise = return $ Done
#endif
{-# RULES
"enumFromTo<Double> [Stream]"
enumFromTo = enumFromTo_double :: Monad m => Double -> Double -> Stream m Double
"enumFromTo<Float> [Stream]"
enumFromTo = enumFromTo_double :: Monad m => Float -> Float -> Stream m Float #-}
enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Stream m a
{-# INLINE_FUSED enumFromThenTo #-}
enumFromThenTo :: a -> a -> a -> Stream m a
enumFromThenTo a
x a
y a
z = [a] -> Stream m a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
fromList [a
x, a
y .. a
z]
toList :: Monad m => Stream m a -> m [a]
{-# INLINE toList #-}
toList :: Stream m a -> m [a]
toList = (a -> [a] -> [a]) -> [a] -> Stream m a -> m [a]
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
foldr (:) []
fromList :: Monad m => [a] -> Stream m a
{-# INLINE fromList #-}
fromList :: [a] -> Stream m a
fromList [a]
zs = ([a] -> m (Step [a] a)) -> [a] -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream [a] -> m (Step [a] a)
forall (m :: * -> *) a. Monad m => [a] -> m (Step [a] a)
step [a]
zs
where
step :: [a] -> m (Step [a] a)
step (a
x:[a]
xs) = Step [a] a -> m (Step [a] a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> [a] -> Step [a] a
forall a s. a -> s -> Step s a
Yield a
x [a]
xs)
step [] = Step [a] a -> m (Step [a] a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step [a] a
forall s a. Step s a
Done
fromListN :: Monad m => Int -> [a] -> Stream m a
{-# INLINE_FUSED fromListN #-}
fromListN :: Int -> [a] -> Stream m a
fromListN Int
m [a]
zs = (([a], Int) -> m (Step ([a], Int) a)) -> ([a], Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream ([a], Int) -> m (Step ([a], Int) a)
forall b (m :: * -> *) a.
(Ord b, Num b, Monad m) =>
([a], b) -> m (Step ([a], b) a)
step ([a]
zs,Int
m)
where
{-# INLINE_INNER step #-}
step :: ([a], b) -> m (Step ([a], b) a)
step ([a]
_, b
n) | b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Step ([a], b) a -> m (Step ([a], b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step ([a], b) a
forall s a. Step s a
Done
step (a
x:[a]
xs,b
n) = Step ([a], b) a -> m (Step ([a], b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ([a], b) -> Step ([a], b) a
forall a s. a -> s -> Step s a
Yield a
x ([a]
xs,b
nb -> b -> b
forall a. Num a => a -> a -> a
-b
1))
step ([],b
_) = Step ([a], b) a -> m (Step ([a], b) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step ([a], b) a
forall s a. Step s a
Done