{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide, not-home #-}
module Data.Massiv.Vector.Stream (
Steps (..),
Stream (..),
steps,
isteps,
consume,
fromStream,
fromStreamM,
fromStreamExactM,
unstreamExact,
unstreamMax,
unstreamMaxM,
unstreamUnknown,
unstreamUnknownM,
unstreamIntoM,
length,
null,
empty,
singleton,
generate,
headMaybe,
last,
cons,
uncons,
snoc,
drop,
take,
slice,
iterateN,
iterateNM,
replicate,
replicateM,
generateM,
traverse,
map,
mapM,
mapM_,
indexed,
concatMap,
append,
zipWith,
zipWith3,
zipWith4,
zipWith5,
zipWith6,
zipWithM,
zipWith3M,
zipWith4M,
zipWith5M,
zipWith6M,
zipWithM_,
zipWith3M_,
zipWith4M_,
zipWith5M_,
zipWith6M_,
foldl,
foldl1,
foldlM,
foldl1M,
foldlLazy,
foldl1Lazy,
foldlLazyM,
foldl1LazyM,
foldrLazy,
foldr1Lazy,
foldrLazyM,
foldr1LazyM,
or,
and,
unfoldr,
unfoldrN,
unsafeUnfoldrN,
unfoldrM,
unfoldrNM,
unsafeUnfoldrNM,
unfoldrExactN,
unfoldrExactNM,
prescanlM,
postscanlM,
postscanlAccM,
scanlM,
scanl1M,
enumFromStepN,
toList,
fromList,
fromListN,
unsafeFromListN,
mapMaybe,
mapMaybeA,
mapMaybeM,
filter,
filterA,
filterM,
transSteps,
transStepsId,
module Data.Vector.Fusion.Util,
Id (..),
) where
import qualified Control.Monad as M
import Control.Monad.ST
import Data.Coerce
import qualified Data.Foldable as F
import Data.Massiv.Core.Common hiding (empty, replicate, singleton)
import Data.Maybe (catMaybes)
import qualified Data.Stream.Monadic as S
import qualified Data.Traversable as Traversable (traverse)
import qualified Data.Vector.Fusion.Bundle.Size as B
import Data.Vector.Fusion.Util
import qualified GHC.Exts (IsList (..))
import Prelude hiding (
and,
concatMap,
drop,
filter,
foldl,
foldl1,
foldr,
foldr1,
length,
map,
mapM,
mapM_,
null,
or,
replicate,
take,
traverse,
zipWith,
zipWith3,
)
instance Monad m => Functor (Steps m) where
fmap :: forall a b. (a -> b) -> Steps m a -> Steps m b
fmap a -> b
f Steps m a
str = Steps m a
str{stepsStream = S.map f (stepsStream str)}
{-# INLINE fmap #-}
<$ :: forall a b. a -> Steps m b -> Steps m a
(<$) a
e Steps m b
str =
case Steps m b -> LengthHint
forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize Steps m b
str of
LengthExact Sz1
n -> Steps m b
str{stepsStream = S.replicate (coerce n) e}
LengthHint
_ -> (b -> a) -> Steps m b -> Steps m a
forall a b. (a -> b) -> Steps m a -> Steps m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> b -> a
forall a b. a -> b -> a
const a
e) Steps m b
str
{-# INLINE (<$) #-}
instance Monad m => Semigroup (Steps m e) where
<> :: Steps m e -> Steps m e -> Steps m e
(<>) = Steps m e -> Steps m e -> Steps m e
forall (m :: * -> *) e.
Monad m =>
Steps m e -> Steps m e -> Steps m e
append
{-# INLINE (<>) #-}
instance Monad m => Monoid (Steps m e) where
mempty :: Steps m e
mempty = Steps m e
forall (m :: * -> *) e. Monad m => Steps m e
empty
{-# INLINE mempty #-}
#if !MIN_VERSION_base(4,11,0)
mappend = append
{-# INLINE mappend #-}
#endif
instance GHC.Exts.IsList (Steps Id e) where
type Item (Steps Id e) = e
toList :: Steps Id e -> [Item (Steps Id e)]
toList = Steps Id e -> [e]
Steps Id e -> [Item (Steps Id e)]
forall e. Steps Id e -> [e]
toList
{-# INLINE toList #-}
fromList :: [Item (Steps Id e)] -> Steps Id e
fromList = [e] -> Steps Id e
[Item (Steps Id e)] -> Steps Id e
forall (m :: * -> *) e. Monad m => [e] -> Steps m e
fromList
{-# INLINE fromList #-}
fromListN :: Int -> [Item (Steps Id e)] -> Steps Id e
fromListN Int
n = (Stream Id e -> LengthHint -> Steps Id e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax (Int -> Sz1
forall ix. Index ix => ix -> Sz ix
Sz Int
n)) (Stream Id e -> Steps Id e)
-> ([e] -> Stream Id e) -> [e] -> Steps Id e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream Id e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n
{-# INLINE fromListN #-}
instance Foldable (Steps Id) where
foldr :: forall a b. (a -> b -> b) -> b -> Steps Id a -> b
foldr a -> b -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Steps m a -> m b
foldrLazy a -> b -> b
f b
acc
{-# INLINE foldr #-}
foldl :: forall b a. (b -> a -> b) -> b -> Steps Id a -> b
foldl b -> a -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldlLazy b -> a -> b
f b
acc
{-# INLINE foldl #-}
foldl' :: forall b a. (b -> a -> b) -> b -> Steps Id a -> b
foldl' b -> a -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl b -> a -> b
f b
acc
{-# INLINE foldl' #-}
foldr1 :: forall a. (a -> a -> a) -> Steps Id a -> a
foldr1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldr1Lazy a -> a -> a
f
{-# INLINE foldr1 #-}
foldl1 :: forall a. (a -> a -> a) -> Steps Id a -> a
foldl1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1Lazy a -> a -> a
f
{-# INLINE foldl1 #-}
toList :: forall e. Steps Id e -> [e]
toList = Steps Id a -> [a]
forall e. Steps Id e -> [e]
toList
{-# INLINE toList #-}
length :: forall a. Steps Id a -> Int
length = Id Int -> Int
forall a. Id a -> a
unId (Id Int -> Int) -> (Steps Id a -> Id Int) -> Steps Id a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps Id a -> Id Int
forall (m :: * -> *) a. Monad m => Steps m a -> m Int
length
{-# INLINE length #-}
null :: forall a. Steps Id a -> Bool
null = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool) -> (Steps Id a -> Id Bool) -> Steps Id a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps Id a -> Id Bool
forall (m :: * -> *) a. Monad m => Steps m a -> m Bool
null
{-# INLINE null #-}
sum :: forall a. Num a => Steps Id a -> a
sum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> a -> Steps Id a -> Id a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE sum #-}
product :: forall a. Num a => Steps Id a -> a
product = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> a -> Steps Id a -> Id a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
{-# INLINE product #-}
maximum :: forall a. Ord a => Steps Id a -> a
maximum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
forall a. Ord a => a -> a -> a
max
{-# INLINE maximum #-}
minimum :: forall a. Ord a => Steps Id a -> a
minimum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
forall a. Ord a => a -> a -> a
min
{-# INLINE minimum #-}
steps :: forall r ix e m. (Monad m, Index ix, Source r e) => Array r ix e -> Steps m e
steps :: forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
steps !Array r ix e
arr =
case Array r ix e -> PrefIndex ix e
forall ix. Index ix => Array r ix e -> PrefIndex ix e
forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r ix e
arr of
PrefIndex ix -> e
gix -> ix -> e
gix (ix -> e) -> Steps m ix -> Steps m e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sz ix -> Steps m ix
forall ix (m :: * -> *). (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps (Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size Array r ix e
arr)
PrefIndexLinear Int -> e
gi ->
Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((Int -> m (Step Int e)) -> Int -> Stream m e
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream Int -> m (Step Int e)
step Int
0) (Sz1 -> LengthHint
LengthExact (Int -> Sz1
forall a b. Coercible a b => a -> b
coerce Int
k))
where
!k :: Int
k = Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem (Sz ix -> Int) -> Sz ix -> Int
forall a b. (a -> b) -> a -> b
$ Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size Array r ix e
arr
step :: Int -> m (Step Int e)
step !Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k = Step Int e -> m (Step Int e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step Int e -> m (Step Int e)) -> Step Int e -> m (Step Int e)
forall a b. (a -> b) -> a -> b
$ e -> Int -> Step Int e
forall a s. a -> s -> Step s a
S.Yield (Int -> e
gi Int
i) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Step Int e -> m (Step Int e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step Int e
forall s a. Step s a
S.Done
{-# INLINE [0] step #-}
{-# INLINE [1] steps #-}
ixRangeSteps :: forall ix m. (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps :: forall ix (m :: * -> *). (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps Sz ix
sz = Stream m ix -> LengthHint -> Steps m ix
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((Maybe ix -> m (Step (Maybe ix) ix)) -> Maybe ix -> Stream m ix
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream Maybe ix -> m (Step (Maybe ix) ix)
step Maybe ix
initStep) (Sz1 -> LengthHint
LengthExact Sz1
k)
where
!k :: Sz1
k = Sz ix -> Sz1
forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
!initStep :: Maybe ix
initStep = if Sz1
k Sz1 -> Sz1 -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1
forall ix. Index ix => Sz ix
zeroSz then Maybe ix
forall a. Maybe a
Nothing else ix -> Maybe ix
forall a. a -> Maybe a
Just ix
forall ix. Index ix => ix
zeroIndex
step :: Maybe ix -> m (Step (Maybe ix) ix)
step (Just ix
ix) = ix
-> ix
-> ix
-> (Int -> Int -> Bool)
-> (Maybe ix -> m (Step (Maybe ix) ix))
-> m (Step (Maybe ix) ix)
forall ix (f :: * -> *) a.
Index ix =>
ix -> ix -> ix -> (Int -> Int -> Bool) -> (Maybe ix -> f a) -> f a
forall (f :: * -> *) a.
ix -> ix -> ix -> (Int -> Int -> Bool) -> (Maybe ix -> f a) -> f a
stepNextMF ix
ix (Sz ix -> ix
forall ix. Sz ix -> ix
unSz Sz ix
sz) ix
forall ix. Index ix => ix
oneIndex Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<) ((Maybe ix -> m (Step (Maybe ix) ix)) -> m (Step (Maybe ix) ix))
-> (Maybe ix -> m (Step (Maybe ix) ix)) -> m (Step (Maybe ix) ix)
forall a b. (a -> b) -> a -> b
$ \Maybe ix
mIx -> Step (Maybe ix) ix -> m (Step (Maybe ix) ix)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Maybe ix) ix -> m (Step (Maybe ix) ix))
-> Step (Maybe ix) ix -> m (Step (Maybe ix) ix)
forall a b. (a -> b) -> a -> b
$ ix -> Maybe ix -> Step (Maybe ix) ix
forall a s. a -> s -> Step s a
S.Yield ix
ix Maybe ix
mIx
step Maybe ix
Nothing = Step (Maybe ix) ix -> m (Step (Maybe ix) ix)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step (Maybe ix) ix
forall s a. Step s a
S.Done
{-# INLINE [0] step #-}
{-# INLINE [1] ixRangeSteps #-}
isteps :: forall r ix e m. (Monad m, Index ix, Source r e) => Array r ix e -> Steps m (ix, e)
isteps :: forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m (ix, e)
isteps !Array r ix e
arr =
case Array r ix e -> PrefIndex ix e
forall ix. Index ix => Array r ix e -> PrefIndex ix e
forall r e ix.
(Source r e, Index ix) =>
Array r ix e -> PrefIndex ix e
unsafePrefIndex Array r ix e
arr of
PrefIndex ix -> e
gix -> (\ !ix
ix -> let e :: e
e = ix -> e
gix ix
ix in e
e e -> (ix, e) -> (ix, e)
forall a b. a -> b -> b
`seq` (ix
ix, e
e)) (ix -> (ix, e)) -> Steps m ix -> Steps m (ix, e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sz ix -> Steps m ix
forall ix (m :: * -> *). (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps Sz ix
sz
PrefIndexLinear Int -> e
gi ->
let k :: Int
k = Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz
step :: Int -> m (Step Int (ix, e))
step Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k =
let e :: e
e = Int -> e
gi Int
i
in e
e e -> m (Step Int (ix, e)) -> m (Step Int (ix, e))
forall a b. a -> b -> b
`seq` Step Int (ix, e) -> m (Step Int (ix, e))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step Int (ix, e) -> m (Step Int (ix, e)))
-> Step Int (ix, e) -> m (Step Int (ix, e))
forall a b. (a -> b) -> a -> b
$ (ix, e) -> Int -> Step Int (ix, e)
forall a s. a -> s -> Step s a
S.Yield (Sz ix -> Int -> ix
forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex Sz ix
sz Int
i, e
e) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Step Int (ix, e) -> m (Step Int (ix, e))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step Int (ix, e)
forall s a. Step s a
S.Done
{-# INLINE [0] step #-}
in Stream m (ix, e) -> LengthHint -> Steps m (ix, e)
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((Int -> m (Step Int (ix, e))) -> Int -> Stream m (ix, e)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream Int -> m (Step Int (ix, e))
step Int
0) (Sz1 -> LengthHint
LengthExact (Int -> Sz1
forall a b. Coercible a b => a -> b
coerce Int
k))
where
!sz :: Sz ix
sz = Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
forall ix e. Array r ix e -> Sz ix
size Array r ix e
arr
{-# INLINE isteps #-}
fromStream :: forall r e. Manifest r e => B.Size -> S.Stream Id e -> Vector r e
fromStream :: forall r e. Manifest r e => Size -> Stream Id e -> Vector r e
fromStream Size
sz Stream Id e
str =
case Size -> Maybe Int
B.upperBound Size
sz of
Maybe Int
Nothing -> Stream Id e -> Vector r e
forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown Stream Id e
str
Just Int
k -> Int -> Stream Id e -> Vector r e
forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
k Stream Id e
str
{-# INLINE fromStream #-}
fromStreamM :: forall r e m. (Monad m, Manifest r e) => B.Size -> S.Stream m e -> m (Vector r e)
fromStreamM :: forall r e (m :: * -> *).
(Monad m, Manifest r e) =>
Size -> Stream m e -> m (Vector r e)
fromStreamM Size
sz Stream m e
str = do
[e]
xs <- Stream m e -> m [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m e
str
case Size -> Maybe Int
B.upperBound Size
sz of
Maybe Int
Nothing -> Vector r e -> m (Vector r e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector r e -> m (Vector r e)) -> Vector r e -> m (Vector r e)
forall a b. (a -> b) -> a -> b
$! Stream Id e -> Vector r e
forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
Just Int
k -> Vector r e -> m (Vector r e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector r e -> m (Vector r e)) -> Vector r e -> m (Vector r e)
forall a b. (a -> b) -> a -> b
$! Int -> Stream Id e -> Vector r e
forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
k ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
{-# INLINE fromStreamM #-}
fromStreamExactM
:: forall r ix e m
. (Monad m, Manifest r e, Index ix)
=> Sz ix
-> S.Stream m e
-> m (Array r ix e)
fromStreamExactM :: forall r ix e (m :: * -> *).
(Monad m, Manifest r e, Index ix) =>
Sz ix -> Stream m e -> m (Array r ix e)
fromStreamExactM Sz ix
sz Stream m e
str = do
[e]
xs <- Stream m e -> m [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m e
str
Array r ix e -> m (Array r ix e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array r ix e -> m (Array r ix e))
-> Array r ix e -> m (Array r ix e)
forall a b. (a -> b) -> a -> b
$! Sz ix -> Stream Id e -> Array r ix e
forall r ix e.
(Manifest r e, Index ix) =>
Sz ix -> Stream Id e -> Array r ix e
unstreamExact Sz ix
sz ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
{-# INLINE fromStreamExactM #-}
unstreamIntoM
:: (Manifest r a, PrimMonad m)
=> MVector (PrimState m) r a
-> LengthHint
-> S.Stream Id a
-> m (MVector (PrimState m) r a)
unstreamIntoM :: forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> LengthHint -> Stream Id a -> m (MVector (PrimState m) r a)
unstreamIntoM MVector (PrimState m) r a
marr LengthHint
sz Stream Id a
str =
case LengthHint
sz of
LengthExact Sz1
_ -> MVector (PrimState m) r a
marr MVector (PrimState m) r a -> m Int -> m (MVector (PrimState m) r a)
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MVector (PrimState m) r a -> Stream Id a -> m Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MVector (PrimState m) r a
marr Stream Id a
str
LengthMax Sz1
_ -> MVector (PrimState m) r a -> Sz1 -> m (MVector (PrimState m) r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a
-> Sz ix -> m (MArray (PrimState m) r ix a)
unsafeLinearShrink MVector (PrimState m) r a
marr (Sz1 -> m (MVector (PrimState m) r a))
-> (Int -> Sz1) -> Int -> m (MVector (PrimState m) r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Int -> m (MVector (PrimState m) r a))
-> m Int -> m (MVector (PrimState m) r a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MVector (PrimState m) r a -> Stream Id a -> m Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MVector (PrimState m) r a
marr Stream Id a
str
LengthHint
LengthUnknown -> MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MVector (PrimState m) r a
marr Stream Id a
str
{-# INLINE unstreamIntoM #-}
unstreamMax
:: forall r e
. Manifest r e
=> Int
-> S.Stream Id e
-> Vector r e
unstreamMax :: forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
kMax Stream Id e
str =
(forall s. ST s (Vector r e)) -> Vector r e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector r e)) -> Vector r e)
-> (forall s. ST s (Vector r e)) -> Vector r e
forall a b. (a -> b) -> a -> b
$ do
MArray s r Int e
marr <- Sz1 -> ST s (MArray (PrimState (ST s)) r Int e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
kMax)
Int
k <- MArray (PrimState (ST s)) r Int e -> Stream Id e -> ST s Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray s r Int e
MArray (PrimState (ST s)) r Int e
marr Stream Id e
str
MArray s r Int e
marrShrunk <-
if Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
kMax
then MArray s r Int e -> ST s (MArray s r Int e)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray s r Int e
marr
else MArray (PrimState (ST s)) r Int e
-> Sz1 -> ST s (MArray (PrimState (ST s)) r Int e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MArray s r Int e
MArray (PrimState (ST s)) r Int e
marr (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
k)
Comp -> MArray (PrimState (ST s)) r Int e -> ST s (Vector r e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray s r Int e
MArray (PrimState (ST s)) r Int e
marrShrunk
{-# INLINE unstreamMax #-}
unstreamMaxM
:: (Manifest r a, Index ix, PrimMonad m) => MArray (PrimState m) r ix a -> S.Stream Id a -> m Int
unstreamMaxM :: forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray (PrimState m) r ix a
marr = (Int -> a -> m Int) -> Int -> Stream m a -> m Int
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM' Int -> a -> m Int
fillAtIndex Int
0 (Stream m a -> m Int)
-> (Stream Id a -> Stream m a) -> Stream Id a -> m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall z. Id z -> m z) -> Stream Id a -> Stream m a
forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (z -> m z
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (z -> m z) -> (Id z -> z) -> Id z -> m z
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id z -> z
forall a. Id a -> a
unId)
where
fillAtIndex :: Int -> a -> m Int
fillAtIndex Int
i a
x = (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> m () -> m Int
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MArray (PrimState m) r ix a -> Int -> a -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Int -> a -> m ()
unsafeLinearWrite MArray (PrimState m) r ix a
marr Int
i a
x
{-# INLINE fillAtIndex #-}
{-# INLINE unstreamMaxM #-}
unstreamUnknown :: Manifest r a => S.Stream Id a -> Vector r a
unstreamUnknown :: forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown Stream Id a
str =
(forall s. ST s (Vector r a)) -> Vector r a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector r a)) -> Vector r a)
-> (forall s. ST s (Vector r a)) -> Vector r a
forall a b. (a -> b) -> a -> b
$ do
MArray s r Int a
marr <- Sz1 -> ST s (MArray (PrimState (ST s)) r Int a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix a)
unsafeNew Sz1
forall ix. Index ix => Sz ix
zeroSz
MArray (PrimState (ST s)) r Int a
-> Stream Id a -> ST s (MArray (PrimState (ST s)) r Int a)
forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MArray s r Int a
MArray (PrimState (ST s)) r Int a
marr Stream Id a
str ST s (MArray s r Int a)
-> (MArray s r Int a -> ST s (Vector r a)) -> ST s (Vector r a)
forall a b. ST s a -> (a -> ST s b) -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Comp -> MArray (PrimState (ST s)) r Int a -> ST s (Vector r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix a -> m (Array r ix a)
unsafeFreeze Comp
Seq
{-# INLINE unstreamUnknown #-}
unstreamUnknownM
:: (Manifest r a, PrimMonad m)
=> MVector (PrimState m) r a
-> S.Stream Id a
-> m (MVector (PrimState m) r a)
unstreamUnknownM :: forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MVector (PrimState m) r a
marr Stream Id a
str = do
(MVector (PrimState m) r a
marr', Int
k) <- ((MVector (PrimState m) r a, Int)
-> a -> m (MVector (PrimState m) r a, Int))
-> (MVector (PrimState m) r a, Int)
-> Stream m a
-> m (MVector (PrimState m) r a, Int)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM' (MVector (PrimState m) r a, Int)
-> a -> m (MVector (PrimState m) r a, Int)
forall {r} {e} {m :: * -> *}.
(Manifest r e, PrimMonad m) =>
(MArray (PrimState m) r Int e, Int)
-> e -> m (MArray (PrimState m) r Int e, Int)
fillAtIndex (MVector (PrimState m) r a
marr, Int
0) (Stream m a -> m (MVector (PrimState m) r a, Int))
-> Stream m a -> m (MVector (PrimState m) r a, Int)
forall a b. (a -> b) -> a -> b
$ (forall z. Id z -> m z) -> Stream Id a -> Stream m a
forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (z -> m z
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (z -> m z) -> (Id z -> z) -> Id z -> m z
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id z -> z
forall a. Id a -> a
unId) Stream Id a
str
if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Sz1 -> Int
forall ix. Sz ix -> ix
unSz (MVector (PrimState m) r a -> Sz1
forall ix s. Index ix => MArray s r ix a -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MVector (PrimState m) r a
marr')
then MVector (PrimState m) r a -> Sz1 -> m (MVector (PrimState m) r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a
-> Sz ix -> m (MArray (PrimState m) r ix a)
unsafeLinearShrink MVector (PrimState m) r a
marr' (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
k)
else MVector (PrimState m) r a -> m (MVector (PrimState m) r a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MVector (PrimState m) r a
marr'
where
fillAtIndex :: (MArray (PrimState m) r Int e, Int)
-> e -> m (MArray (PrimState m) r Int e, Int)
fillAtIndex (!MArray (PrimState m) r Int e
ma, !Int
i) e
x = do
let k :: Int
k = Sz1 -> Int
forall ix. Sz ix -> ix
unSz (MArray (PrimState m) r Int e -> Sz1
forall ix s. Index ix => MArray s r ix e -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) r Int e
ma)
MArray (PrimState m) r Int e
ma' <-
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k
then MArray (PrimState m) r Int e -> m (MArray (PrimState m) r Int e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray (PrimState m) r Int e
ma
else MArray (PrimState m) r Int e
-> Sz1 -> m (MArray (PrimState m) r Int e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearGrow MArray (PrimState m) r Int e
ma (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2))
(MArray (PrimState m) r Int e
ma', Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (MArray (PrimState m) r Int e, Int)
-> m () -> m (MArray (PrimState m) r Int e, Int)
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MArray (PrimState m) r Int e -> Int -> e -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r Int e
ma' Int
i e
x
{-# INLINE fillAtIndex #-}
{-# INLINE unstreamUnknownM #-}
unstreamExact
:: forall r ix e
. (Manifest r e, Index ix)
=> Sz ix
-> S.Stream Id e
-> Array r ix e
unstreamExact :: forall r ix e.
(Manifest r e, Index ix) =>
Sz ix -> Stream Id e -> Array r ix e
unstreamExact Sz ix
sz Stream Id e
str =
(forall s. ST s (Array r ix e)) -> Array r ix e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Array r ix e)) -> Array r ix e)
-> (forall s. ST s (Array r ix e)) -> Array r ix e
forall a b. (a -> b) -> a -> b
$ do
MArray s r ix e
marr <- Sz ix -> ST s (MArray (PrimState (ST s)) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
Int
_ <- MArray (PrimState (ST s)) r ix e -> Stream Id e -> ST s Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray s r ix e
MArray (PrimState (ST s)) r ix e
marr Stream Id e
str
Comp -> MArray (PrimState (ST s)) r ix e -> ST s (Array r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray s r ix e
MArray (PrimState (ST s)) r ix e
marr
{-# INLINE unstreamExact #-}
length :: Monad m => Steps m a -> m Int
length :: forall (m :: * -> *) a. Monad m => Steps m a -> m Int
length (Steps Stream m a
str LengthHint
sz) =
case LengthHint
sz of
LengthExact Sz1
k -> Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
k
LengthHint
_ -> Stream m a -> m Int
forall (m :: * -> *) a. Monad m => Stream m a -> m Int
S.length Stream m a
str
{-# INLINE length #-}
null :: Monad m => Steps m a -> m Bool
null :: forall (m :: * -> *) a. Monad m => Steps m a -> m Bool
null (Steps Stream m a
str LengthHint
sz) =
case LengthHint
sz of
LengthExact Sz1
k -> Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Sz1
k Sz1 -> Sz1 -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1
forall ix. Index ix => Sz ix
zeroSz)
LengthHint
_ -> Stream m a -> m Bool
forall (m :: * -> *) a. Monad m => Stream m a -> m Bool
S.null Stream m a
str
{-# INLINE null #-}
empty :: Monad m => Steps m e
empty :: forall (m :: * -> *) e. Monad m => Steps m e
empty = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps Stream m e
forall (m :: * -> *) a. Monad m => Stream m a
S.empty (Sz1 -> LengthHint
LengthExact Sz1
forall ix. Index ix => Sz ix
zeroSz)
{-# INLINE empty #-}
singleton :: Monad m => e -> Steps m e
singleton :: forall (m :: * -> *) e. Monad m => e -> Steps m e
singleton e
e = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (e -> Stream m e
forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton e
e) (Sz1 -> LengthHint
LengthExact Sz1
forall ix. Index ix => Sz ix
oneSz)
{-# INLINE singleton #-}
generate :: Monad m => Sz1 -> (Int -> e) -> Steps m e
generate :: forall (m :: * -> *) e. Monad m => Sz1 -> (Int -> e) -> Steps m e
generate Sz1
k Int -> e
f = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (Int -> e) -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
S.generate (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
k) Int -> e
f) (Sz1 -> LengthHint
LengthExact Sz1
k)
{-# INLINE generate #-}
headMaybe :: Monad m => Steps m a -> m (Maybe a)
headMaybe :: forall (m :: * -> *) a. Monad m => Steps m a -> m (Maybe a)
headMaybe (Steps (S.Stream s -> m (Step s a)
step s
t) LengthHint
_) = SPEC -> s -> m (Maybe a)
headMaybeLoop SPEC
S.SPEC s
t
where
headMaybeLoop :: SPEC -> s -> m (Maybe a)
headMaybeLoop !SPEC
_ s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
S.Yield a
x s
_ -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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
S.Skip s
s' -> SPEC -> s -> m (Maybe a)
headMaybeLoop SPEC
S.SPEC s
s'
Step s a
S.Done -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
{-# INLINE [0] headMaybeLoop #-}
{-# INLINE headMaybe #-}
cons :: Monad m => e -> Steps m e -> Steps m e
cons :: forall (m :: * -> *) e. Monad m => e -> Steps m e -> Steps m e
cons e
e (Steps Stream m e
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (e -> Stream m e -> Stream m e
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
S.cons e
e Stream m e
str) (LengthHint
k LengthHint -> Int -> LengthHint
`addInt` Int
1)
{-# INLINE cons #-}
uncons :: Monad m => Steps m e -> m (Maybe (e, Steps m e))
uncons :: forall (m :: * -> *) e.
Monad m =>
Steps m e -> m (Maybe (e, Steps m e))
uncons Steps m e
sts = (\Maybe e
mx -> (,Sz1 -> Steps m e -> Steps m e
forall (m :: * -> *) a. Monad m => Sz1 -> Steps m a -> Steps m a
drop Sz1
forall ix. Index ix => Sz ix
oneSz Steps m e
sts) (e -> (e, Steps m e)) -> Maybe e -> Maybe (e, Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe e
mx) (Maybe e -> Maybe (e, Steps m e))
-> m (Maybe e) -> m (Maybe (e, Steps m e))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Steps m e -> m (Maybe e)
forall (m :: * -> *) a. Monad m => Steps m a -> m (Maybe a)
headMaybe Steps m e
sts
{-# INLINE uncons #-}
snoc :: Monad m => Steps m e -> e -> Steps m e
snoc :: forall (m :: * -> *) e. Monad m => Steps m e -> e -> Steps m e
snoc (Steps Stream m e
str LengthHint
k) e
e = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e -> e -> Stream m e
forall (m :: * -> *) a. Monad m => Stream m a -> a -> Stream m a
S.snoc Stream m e
str e
e) (LengthHint
k LengthHint -> Int -> LengthHint
`addInt` Int
1)
{-# INLINE snoc #-}
traverse :: (Monad m, Applicative f) => (e -> f a) -> Steps Id e -> f (Steps m a)
traverse :: forall (m :: * -> *) (f :: * -> *) e a.
(Monad m, Applicative f) =>
(e -> f a) -> Steps Id e -> f (Steps m a)
traverse e -> f a
f (Steps Stream Id e
str LengthHint
k) = (Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
k) (Stream m a -> Steps m a) -> f (Stream m a) -> f (Steps m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([e] -> f [a]) -> Stream Id e -> f (Stream m a)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((e -> f a) -> [e] -> f [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
Traversable.traverse e -> f a
f) Stream Id e
str
{-# INLINE traverse #-}
append :: Monad m => Steps m e -> Steps m e -> Steps m e
append :: forall (m :: * -> *) e.
Monad m =>
Steps m e -> Steps m e -> Steps m e
append (Steps Stream m e
str1 LengthHint
k1) (Steps Stream m e
str2 LengthHint
k2) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e
str1 Stream m e -> Stream m e -> Stream m e
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
S.++ Stream m e
str2) (LengthHint
k1 LengthHint -> LengthHint -> LengthHint
`addLengthHint` LengthHint
k2)
{-# INLINE append #-}
map :: Monad m => (e -> a) -> Steps m e -> Steps m a
map :: forall (m :: * -> *) e a.
Monad m =>
(e -> a) -> Steps m e -> Steps m a
map e -> a
f (Steps Stream m e
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> a) -> Stream m e -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
S.map e -> a
f Stream m e
str) LengthHint
k
{-# INLINE map #-}
indexed :: Monad m => Steps m e -> Steps m (Int, e)
indexed :: forall (m :: * -> *) e. Monad m => Steps m e -> Steps m (Int, e)
indexed (Steps Stream m e
str LengthHint
k) = Stream m (Int, e) -> LengthHint -> Steps m (Int, e)
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e -> Stream m (Int, e)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
S.indexed Stream m e
str) LengthHint
k
{-# INLINE indexed #-}
mapM :: Monad m => (e -> m a) -> Steps m e -> Steps m a
mapM :: forall (m :: * -> *) e a.
Monad m =>
(e -> m a) -> Steps m e -> Steps m a
mapM e -> m a
f (Steps Stream m e
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> m a) -> Stream m e -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
S.mapM e -> m a
f Stream m e
str) LengthHint
k
{-# INLINE mapM #-}
mapM_ :: Monad m => (e -> m a) -> Steps m e -> m ()
mapM_ :: forall (m :: * -> *) e a.
Monad m =>
(e -> m a) -> Steps m e -> m ()
mapM_ e -> m a
f (Steps Stream m e
str LengthHint
_) = (e -> m a) -> Stream m e -> m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> m ()
S.mapM_ e -> m a
f Stream m e
str
{-# INLINE mapM_ #-}
zipWith :: Monad m => (a -> b -> e) -> Steps m a -> Steps m b -> Steps m e
zipWith :: forall (m :: * -> *) a b e.
Monad m =>
(a -> b -> e) -> Steps m a -> Steps m b -> Steps m e
zipWith a -> b -> e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> e) -> Stream m a -> Stream m b -> Stream m e
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
S.zipWith a -> b -> e
f Stream m a
sa Stream m b
sb) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka LengthHint
kb)
{-# INLINE zipWith #-}
zipWith3 :: Monad m => (a -> b -> c -> d) -> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3 :: forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3 a -> b -> c -> d
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) =
Stream m d -> LengthHint -> Steps m d
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
S.zipWith3 a -> b -> c -> d
f Stream m a
sa Stream m b
sb Stream m c
sc) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb LengthHint
kc))
{-# INLINE zipWith3 #-}
zipWith4
:: Monad m => (a -> b -> c -> d -> e) -> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4 :: forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4 a -> b -> c -> d -> e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) =
Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> 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 -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
S.zipWith4 a -> b -> c -> d -> e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc LengthHint
kd)))
{-# INLINE zipWith4 #-}
zipWith5
:: Monad m
=> (a -> b -> c -> d -> e -> f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5 :: forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5 a -> b -> c -> d -> e -> f
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) =
Stream m f -> LengthHint -> Steps m f
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> e -> 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 -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
S.zipWith5 a -> b -> c -> d -> e -> f
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd LengthHint
ke))))
{-# INLINE zipWith5 #-}
zipWith6
:: Monad m
=> (a -> b -> c -> d -> e -> f -> g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6 :: forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6 a -> b -> c -> d -> e -> f -> g
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) (Steps Stream m f
sf LengthHint
kf) =
Stream m g -> LengthHint -> Steps m g
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
((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
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
S.zipWith6 a -> b -> c -> d -> e -> f -> g
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf)
(LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ke LengthHint
kf)))))
{-# INLINE zipWith6 #-}
zipWithM :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM a -> b -> m c
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) = Stream m c -> LengthHint -> Steps m c
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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
S.zipWithM a -> b -> m c
f Stream m a
sa Stream m b
sb) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka LengthHint
kb)
{-# INLINE zipWithM #-}
zipWith3M :: Monad m => (a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M :: forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M a -> b -> c -> m d
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) =
Stream m d -> LengthHint -> Steps m d
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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
S.zipWith3M a -> b -> c -> m d
f Stream m a
sa Stream m b
sb Stream m c
sc) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb LengthHint
kc))
{-# INLINE zipWith3M #-}
zipWith4M
:: Monad m
=> (a -> b -> c -> d -> m e)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
zipWith4M :: forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4M a -> b -> c -> d -> m e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) =
Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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
S.zipWith4M a -> b -> c -> d -> m e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc LengthHint
kd)))
{-# INLINE zipWith4M #-}
zipWith5M
:: Monad m
=> (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5M :: forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5M a -> b -> c -> d -> e -> m f
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) =
Stream m f -> LengthHint -> Steps m f
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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
S.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) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd LengthHint
ke))))
{-# INLINE zipWith5M #-}
zipWith6M
:: Monad m
=> (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6M :: forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) (Steps Stream m f
sf LengthHint
kf) =
Stream m g -> LengthHint -> Steps m g
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
((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
S.zipWith6M a -> b -> c -> d -> e -> f -> m g
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf)
(LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ke LengthHint
kf)))))
{-# INLINE zipWith6M #-}
zipWithM_ :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ a -> b -> m c
f (Steps Stream m a
str1 LengthHint
_) (Steps Stream m b
str2 LengthHint
_) = (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> m ()
S.zipWithM_ a -> b -> m c
f Stream m a
str1 Stream m b
str2
{-# INLINE zipWithM_ #-}
zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> m ()
zipWith3M_ :: forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> m ()
zipWith3M_ a -> b -> c -> m d
f Steps m a
sa Steps m b
sb Steps m c
sc = Steps m d -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m d -> m ()) -> Steps m d -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M a -> b -> c -> m d
f Steps m a
sa Steps m b
sb Steps m c
sc
{-# INLINE zipWith3M_ #-}
zipWith4M_
:: Monad m
=> (a -> b -> c -> d -> m e)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> m ()
zipWith4M_ :: forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> m ()
zipWith4M_ a -> b -> c -> d -> m e
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd = Steps m e -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m e -> m ()) -> Steps m e -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4M a -> b -> c -> d -> m e
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd
{-# INLINE zipWith4M_ #-}
zipWith5M_
:: Monad m
=> (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> m ()
zipWith5M_ :: forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> m ()
zipWith5M_ a -> b -> c -> d -> e -> m f
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se = Steps m f -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m f -> m ()) -> Steps m f -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5M a -> b -> c -> d -> e -> m f
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se
{-# INLINE zipWith5M_ #-}
zipWith6M_
:: Monad m
=> (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> m ()
zipWith6M_ :: forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> m ()
zipWith6M_ a -> b -> c -> d -> e -> f -> m g
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se Steps m f
sf = Steps m g -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m g -> m ()) -> Steps m g -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se Steps m f
sf
{-# INLINE zipWith6M_ #-}
consume :: Monad m => Steps m a -> m ()
consume :: forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps (S.Stream s -> m (Step s a)
step s
t) LengthHint
_) = SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
t
where
consumeLoop :: SPEC -> s -> m ()
consumeLoop !SPEC
_ s
s = do
Step s a
r <- s -> m (Step s a)
step s
s
case Step s a
r of
S.Yield a
_ s
s' -> SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
s'
S.Skip s
s' -> SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
s'
Step s a
S.Done -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE consume #-}
transStepsId :: Monad m => Steps Id e -> Steps m e
transStepsId :: forall (m :: * -> *) e. Monad m => Steps Id e -> Steps m e
transStepsId (Steps Stream Id e
sts LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((forall z. Id z -> m z) -> Stream Id e -> Stream m e
forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (z -> m z
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (z -> m z) -> (Id z -> z) -> Id z -> m z
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id z -> z
forall a. Id a -> a
unId) Stream Id e
sts) LengthHint
k
{-# INLINE transStepsId #-}
transSteps :: (Monad m, Monad n) => Steps m e -> m (Steps n e)
transSteps :: forall (m :: * -> *) (n :: * -> *) e.
(Monad m, Monad n) =>
Steps m e -> m (Steps n e)
transSteps (Steps Stream m e
strM sz :: LengthHint
sz@(LengthExact Sz1
_)) = (Stream n e -> LengthHint -> Steps n e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
sz) (Stream n e -> Steps n e) -> m (Stream n e) -> m (Steps n e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream m e -> m (Stream n e)
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Stream n a)
transListM Stream m e
strM
transSteps (Steps Stream m e
strM LengthHint
_) = do
(Sz1
n, Stream n e
strN) <- Stream m e -> m (Sz1, Stream n e)
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Sz1, Stream n a)
transListNM Stream m e
strM
Steps n e -> m (Steps n e)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Stream n e -> LengthHint -> Steps n e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps Stream n e
strN (Sz1 -> LengthHint
LengthExact Sz1
n))
{-# INLINE transSteps #-}
foldl :: Monad m => (b -> a -> b) -> b -> Steps m a -> m b
foldl :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl b -> a -> b
f b
acc = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl' b -> a -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl #-}
foldl1 :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldl1 :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1' a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl1 #-}
foldlM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> m a
foldlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Steps m b -> m a
foldlM a -> b -> m a
f a
acc = (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
S.foldlM' a -> b -> m a
f a
acc (Stream m b -> m a)
-> (Steps m b -> Stream m b) -> Steps m b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m b -> Stream m b
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlM #-}
foldl1M :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldl1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Steps m a -> m a
foldl1M a -> a -> m a
f (Steps Stream m a
sts LengthHint
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M' a -> a -> m a
f Stream m a
sts
{-# INLINE foldl1M #-}
foldrLazy :: Monad m => (a -> b -> b) -> b -> Steps m a -> m b
foldrLazy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Steps m a -> m b
foldrLazy a -> b -> b
f b
acc = (a -> b -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr a -> b -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldrLazy #-}
foldr1Lazy :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldr1Lazy :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldr1Lazy a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldr1 a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldr1Lazy #-}
foldlLazy :: Monad m => (b -> a -> b) -> b -> Steps m a -> m b
foldlLazy :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldlLazy b -> a -> b
f b
acc = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl b -> a -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlLazy #-}
foldl1Lazy :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldl1Lazy :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1Lazy a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1 a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl1Lazy #-}
foldlLazyM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> m a
foldlLazyM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Steps m b -> m a
foldlLazyM a -> b -> m a
f a
acc = (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
S.foldlM a -> b -> m a
f a
acc (Stream m b -> m a)
-> (Steps m b -> Stream m b) -> Steps m b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m b -> Stream m b
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlLazyM #-}
foldl1LazyM :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldl1LazyM :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Steps m a -> m a
foldl1LazyM a -> a -> m a
f (Steps Stream m a
sts LengthHint
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M a -> a -> m a
f Stream m a
sts
{-# INLINE foldl1LazyM #-}
foldrLazyM :: Monad m => (b -> a -> m a) -> a -> Steps m b -> m a
foldrLazyM :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m a) -> a -> Steps m b -> m a
foldrLazyM b -> a -> m a
f a
acc (Steps Stream m b
sts LengthHint
_) = (b -> a -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
S.foldrM b -> a -> m a
f a
acc Stream m b
sts
{-# INLINE foldrLazyM #-}
foldr1LazyM :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldr1LazyM :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Steps m a -> m a
foldr1LazyM a -> a -> m 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
S.foldr1M a -> a -> m a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldr1LazyM #-}
or :: Monad m => Steps m Bool -> m Bool
or :: forall (m :: * -> *). Monad m => Steps m Bool -> m Bool
or = Stream m Bool -> m Bool
forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.or (Stream m Bool -> m Bool)
-> (Steps m Bool -> Stream m Bool) -> Steps m Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m Bool -> Stream m Bool
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE or #-}
and :: Monad m => Steps m Bool -> m Bool
and :: forall (m :: * -> *). Monad m => Steps m Bool -> m Bool
and = Stream m Bool -> m Bool
forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.and (Stream m Bool -> m Bool)
-> (Steps m Bool -> Stream m Bool) -> Steps m Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m Bool -> Stream m Bool
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE and #-}
mapMaybe :: Monad m => (a -> Maybe e) -> Steps m a -> Steps m e
mapMaybe :: forall (m :: * -> *) a e.
Monad m =>
(a -> Maybe e) -> Steps m a -> Steps m e
mapMaybe a -> Maybe e
f (Steps Stream m a
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Maybe e) -> Stream m a -> Stream m e
forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
S.mapMaybe a -> Maybe e
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE mapMaybe #-}
concatMap :: Monad m => (a -> Steps m e) -> Steps m a -> Steps m e
concatMap :: forall (m :: * -> *) a e.
Monad m =>
(a -> Steps m e) -> Steps m a -> Steps m e
concatMap a -> Steps m e
f (Steps Stream m a
str LengthHint
_) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Stream m e) -> Stream m a -> Stream m e
forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
S.concatMap (Steps m e -> Stream m e
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream (Steps m e -> Stream m e) -> (a -> Steps m e) -> a -> Stream m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Steps m e
f) Stream m a
str) LengthHint
LengthUnknown
{-# INLINE concatMap #-}
mapMaybeA :: (Monad m, Applicative f) => (a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA :: forall (m :: * -> *) (f :: * -> *) a e.
(Monad m, Applicative f) =>
(a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA a -> f (Maybe e)
f (Steps Stream Id a
str LengthHint
k) = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) (Stream m e -> Steps m e) -> f (Stream m e) -> f (Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([a] -> f [e]) -> Stream Id a -> f (Stream m e)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((a -> f (Maybe e)) -> [a] -> f [e]
forall (f :: * -> *) a b.
Applicative f =>
(a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA a -> f (Maybe e)
f) Stream Id a
str
{-# INLINE mapMaybeA #-}
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Steps m a -> Steps m b
mapMaybeM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Steps m a -> Steps m b
mapMaybeM a -> m (Maybe b)
f (Steps Stream m a
str LengthHint
k) = Stream m b -> LengthHint -> Steps m b
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> m (Maybe b)) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeStreamM a -> m (Maybe b)
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE mapMaybeM #-}
mapMaybeListA :: Applicative f => (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA a -> f (Maybe b)
f = ([Maybe b] -> [b]) -> f [Maybe b] -> f [b]
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe b] -> [b]
forall a. [Maybe a] -> [a]
catMaybes (f [Maybe b] -> f [b]) -> ([a] -> f [Maybe b]) -> [a] -> f [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f (Maybe b)) -> [a] -> f [Maybe b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
Traversable.traverse a -> f (Maybe b)
f
{-# INLINE mapMaybeListA #-}
mapMaybeStreamM :: Monad m => (a -> m (Maybe b)) -> S.Stream m a -> S.Stream m b
mapMaybeStreamM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeStreamM a -> m (Maybe b)
f (S.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
S.Stream s -> m (Step s b)
step' s
t
where
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
S.Yield a
x s
s' -> do
Maybe b
b <- a -> m (Maybe b)
f a
x
Step s b -> m (Step s b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$
case Maybe b
b of
Maybe b
Nothing -> s -> Step s b
forall s a. s -> Step s a
S.Skip s
s'
Just b
b' -> b -> s -> Step s b
forall a s. a -> s -> Step s a
S.Yield b
b' s
s'
S.Skip s
s' -> Step s b -> m (Step s b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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
S.Skip s
s'
Step s a
S.Done -> Step s b -> m (Step s b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step s b
forall s a. Step s a
S.Done
{-# INLINE [0] step' #-}
{-# INLINE mapMaybeStreamM #-}
filter :: Monad m => (a -> Bool) -> Steps m a -> Steps m a
filter :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Steps m a -> Steps m a
filter a -> Bool
f (Steps Stream m a
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
S.filter a -> Bool
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE filter #-}
filterA :: (Monad m, Applicative f) => (e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA :: forall (m :: * -> *) (f :: * -> *) e.
(Monad m, Applicative f) =>
(e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA e -> f Bool
f (Steps Stream Id e
str LengthHint
k) = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) (Stream m e -> Steps m e) -> f (Stream m e) -> f (Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([e] -> f [e]) -> Stream Id e -> f (Stream m e)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((e -> f Bool) -> [e] -> f [e]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
M.filterM e -> f Bool
f) Stream Id e
str
{-# INLINE filterA #-}
filterM :: Monad m => (e -> m Bool) -> Steps m e -> Steps m e
filterM :: forall (m :: * -> *) e.
Monad m =>
(e -> m Bool) -> Steps m e -> Steps m e
filterM e -> m Bool
f (Steps Stream m e
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> m Bool) -> Stream m e -> Stream m e
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
S.filterM e -> m Bool
f Stream m e
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE filterM #-}
take :: Monad m => Sz1 -> Steps m a -> Steps m a
take :: forall (m :: * -> *) a. Monad m => Sz1 -> Steps m a -> Steps m a
take Sz1
n (Steps Stream m a
str LengthHint
sz) =
Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.take (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) Stream m a
str) (LengthHint -> Steps m a) -> LengthHint -> Steps m a
forall a b. (a -> b) -> a -> b
$!
case LengthHint
sz of
LengthExact Sz1
k -> Sz1 -> LengthHint
LengthExact ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
n Sz1
k)
LengthMax Sz1
k -> Sz1 -> LengthHint
LengthMax ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
n Sz1
k)
LengthHint
LengthUnknown -> LengthHint
LengthUnknown
{-# INLINE take #-}
drop :: Monad m => Sz1 -> Steps m a -> Steps m a
drop :: forall (m :: * -> *) a. Monad m => Sz1 -> Steps m a -> Steps m a
drop Sz1
n (Steps Stream m a
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.drop (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) Stream m a
str) (LengthHint
k LengthHint -> LengthHint -> LengthHint
`subtractLengthHint` Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE drop #-}
slice :: Monad m => Int -> Sz1 -> Steps m a -> Steps m a
slice :: forall (m :: * -> *) a.
Monad m =>
Int -> Sz1 -> Steps m a -> Steps m a
slice Int
i Sz1
k (Steps Stream m a
str LengthHint
_) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Int -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> Int -> Stream m a -> Stream m a
S.slice Int
i (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
k) Stream m a
str) (Sz1 -> LengthHint
LengthMax Sz1
k)
{-# INLINE slice #-}
iterateN :: Monad m => Sz1 -> (a -> a) -> a -> Steps m a
iterateN :: forall (m :: * -> *) a.
Monad m =>
Sz1 -> (a -> a) -> a -> Steps m a
iterateN Sz1
n a -> a
f a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (a -> a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> a) -> a -> Stream m a
S.iterateN (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) a -> a
f a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE iterateN #-}
iterateNM :: Monad m => Sz1 -> (a -> m a) -> a -> Steps m a
iterateNM :: forall (m :: * -> *) a.
Monad m =>
Sz1 -> (a -> m a) -> a -> Steps m a
iterateNM Sz1
n a -> m a
f a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (a -> m a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
S.iterateNM (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) a -> m a
f a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE iterateNM #-}
replicate :: Monad m => Sz1 -> a -> Steps m a
replicate :: forall (m :: * -> *) a. Monad m => Sz1 -> a -> Steps m a
replicate Sz1
n a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE replicate #-}
replicateM :: Monad m => Sz1 -> m a -> Steps m a
replicateM :: forall (m :: * -> *) a. Monad m => Sz1 -> m a -> Steps m a
replicateM Sz1
n m a
f = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) m a
f) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE replicateM #-}
generateM :: Monad m => Sz1 -> (Int -> m a) -> Steps m a
generateM :: forall (m :: * -> *) a. Monad m => Sz1 -> (Int -> m a) -> Steps m a
generateM Sz1
n Int -> m a
f = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
S.generateM (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) Int -> m a
f) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE generateM #-}
unfoldr :: Monad m => (s -> Maybe (e, s)) -> s -> Steps m e
unfoldr :: forall (m :: * -> *) s e.
Monad m =>
(s -> Maybe (e, s)) -> s -> Steps m e
unfoldr s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
(s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldr s -> Maybe (e, s)
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldr #-}
unfoldrN :: Monad m => Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN :: forall (m :: * -> *) s e.
Monad m =>
Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN Sz1
n s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) s -> Maybe (e, s)
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrN #-}
unsafeUnfoldrN :: Monad m => Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN :: forall (m :: * -> *) s e.
Monad m =>
Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN Sz1
n s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) s -> Maybe (e, s)
f s
e0) (Sz1 -> LengthHint
LengthMax Sz1
n)
{-# INLINE unsafeUnfoldrN #-}
unfoldrM :: Monad m => (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM :: forall (m :: * -> *) s e.
Monad m =>
(s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrM s -> m (Maybe (e, s))
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrM #-}
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM :: forall (m :: * -> *) s e.
Monad m =>
Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrNM #-}
unsafeUnfoldrNM :: Monad m => Sz1 -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM :: forall (m :: * -> *) s e.
Monad m =>
Sz1 -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM Sz1
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n) s -> m (Maybe (e, s))
f s
e0) (Sz1 -> LengthHint
LengthMax Sz1
n)
{-# INLINE unsafeUnfoldrNM #-}
unfoldrExactN :: Monad m => Sz1 -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN :: forall (m :: * -> *) s a.
Monad m =>
Sz1 -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN Sz1
n s -> (a, s)
f = Sz1 -> (s -> m (a, s)) -> s -> Steps m a
forall (m :: * -> *) s a.
Monad m =>
Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Sz1
n ((a, s) -> m (a, s)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((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)
{-# INLINE unfoldrExactN #-}
unfoldrExactNM :: Monad m => Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM :: forall (m :: * -> *) s a.
Monad m =>
Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Sz1
n s -> m (a, s)
f s
t = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (((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
S.Stream (s, Int) -> m (Step (s, Int) a)
step (s
t, Sz1 -> Int
forall ix. Sz ix -> ix
unSz Sz1
n)) (Sz1 -> LengthHint
LengthExact Sz1
n)
where
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
0 = Step (s, Int) a -> m (Step (s, Int) a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step (s, Int) a
forall s a. Step s a
S.Done
| Bool
otherwise = ((a, s) -> Step (s, Int) a) -> m (a, s) -> m (Step (s, Int) a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x, s
s') -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
S.Yield a
x (s
s', Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (s -> m (a, s)
f s
s)
{-# INLINE [0] step #-}
{-# INLINE unfoldrExactNM #-}
enumFromStepN :: (Num a, Monad m) => a -> a -> Sz1 -> Steps m a
enumFromStepN :: forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Sz1 -> Steps m a
enumFromStepN a
x a
step Sz1
k = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (a -> a -> Int -> Stream m a
forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
S.enumFromStepN a
x a
step (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
k)) (Sz1 -> LengthHint
LengthExact Sz1
k)
{-# INLINE enumFromStepN #-}
toList :: Steps Id e -> [e]
toList :: forall e. Steps Id e -> [e]
toList (Steps Stream Id e
str LengthHint
_) = Id [e] -> [e]
forall a. Id a -> a
unId (Stream Id e -> Id [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream Id e
str)
{-# INLINE toList #-}
fromList :: Monad m => [e] -> Steps m e
fromList :: forall (m :: * -> *) e. Monad m => [e] -> Steps m e
fromList = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [e] -> Stream m e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList
{-# INLINE fromList #-}
fromListN :: Monad m => Int -> [e] -> Steps m e
fromListN :: forall (m :: * -> *) e. Monad m => Int -> [e] -> Steps m e
fromListN Int
n = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n
{-# INLINE fromListN #-}
unsafeFromListN :: Monad m => Sz1 -> [e] -> Steps m e
unsafeFromListN :: forall (m :: * -> *) e. Monad m => Sz1 -> [e] -> Steps m e
unsafeFromListN Sz1
n = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax Sz1
n) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n)
{-# INLINE unsafeFromListN #-}
liftListA :: (Monad m, Functor f) => ([a] -> f [b]) -> S.Stream Id a -> f (S.Stream m b)
liftListA :: forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA [a] -> f [b]
f Stream Id a
str = [b] -> Stream m b
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList ([b] -> Stream m b) -> f [b] -> f (Stream m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> f [b]
f (Id [a] -> [a]
forall a. Id a -> a
unId (Stream Id a -> Id [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream Id a
str))
{-# INLINE liftListA #-}
transListM :: (Monad m, Monad n) => S.Stream m a -> m (S.Stream n a)
transListM :: forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Stream n a)
transListM Stream m a
str = do
[a]
xs <- Stream m a -> m [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m a
str
Stream n a -> m (Stream n a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Stream n a -> m (Stream n a)) -> Stream n a -> m (Stream n a)
forall a b. (a -> b) -> a -> b
$ [a] -> Stream n a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [a]
xs
{-# INLINE transListM #-}
transListNM :: (Monad m, Monad n) => S.Stream m a -> m (Sz1, S.Stream n a)
transListNM :: forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Sz1, Stream n a)
transListNM Stream m a
str = do
(Int
n, [a]
xs) <- Stream m a -> m (Int, [a])
forall (m :: * -> *) a. Monad m => Stream m a -> m (Int, [a])
toListN Stream m a
str
(Sz1, Stream n a) -> m (Sz1, Stream n a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Sz1
forall a b. Coercible a b => a -> b
coerce Int
n, [a] -> Stream n a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [a]
xs)
{-# INLINE transListNM #-}
toListN :: Monad m => S.Stream m a -> m (Int, [a])
toListN :: forall (m :: * -> *) a. Monad m => Stream m a -> m (Int, [a])
toListN = (a -> (Int, [a]) -> (Int, [a]))
-> (Int, [a]) -> Stream m a -> m (Int, [a])
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr (\a
x (Int
i, [a]
xs) -> (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs)) (Int
0, [])
{-# INLINE toListN #-}
addHint :: (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint :: (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
hint Int
m Int
n
| Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
sz = Sz1 -> LengthHint
hint Sz1
sz
| Bool
otherwise = LengthHint
LengthUnknown
where
k :: Int
k = Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n
sz :: Sz1
sz = Int -> Sz1
forall ix. Index ix => ix -> Sz ix
Sz Int
k
{-# INLINE addHint #-}
addInt :: LengthHint -> Int -> LengthHint
addInt :: LengthHint -> Int -> LengthHint
addInt (LengthExact Sz1
m) Int
n = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) (Int -> Int
forall a b. Coercible a b => a -> b
coerce Int
n)
addInt (LengthMax Sz1
m) Int
n = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) Int
n
addInt LengthHint
_ Int
_ = LengthHint
LengthUnknown
{-# INLINE addInt #-}
addLengthHint :: LengthHint -> LengthHint -> LengthHint
addLengthHint :: LengthHint -> LengthHint -> LengthHint
addLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n)
addLengthHint (LengthMax Sz1
m) (LengthExact Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n)
addLengthHint (LengthExact Sz1
m) (LengthMax Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n)
addLengthHint (LengthMax Sz1
m) (LengthMax Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
m) (Sz1 -> Int
forall a b. Coercible a b => a -> b
coerce Sz1
n)
addLengthHint LengthHint
_ LengthHint
_ = LengthHint
LengthUnknown
{-# INLINE addLengthHint #-}
subtractLengthHint :: LengthHint -> LengthHint -> LengthHint
subtractLengthHint :: LengthHint -> LengthHint -> LengthHint
subtractLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthExact (Sz1
m Sz1 -> Sz1 -> Sz1
forall a. Num a => a -> a -> a
- Sz1
n)
subtractLengthHint (LengthMax Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax (Sz1
m Sz1 -> Sz1 -> Sz1
forall a. Num a => a -> a -> a
- Sz1
n)
subtractLengthHint (LengthExact Sz1
m) (LengthMax Sz1
_) = Sz1 -> LengthHint
LengthMax Sz1
m
subtractLengthHint (LengthMax Sz1
m) (LengthMax Sz1
_) = Sz1 -> LengthHint
LengthMax Sz1
m
subtractLengthHint LengthHint
_ LengthHint
_ = LengthHint
LengthUnknown
{-# INLINE subtractLengthHint #-}
minLengthHint :: LengthHint -> LengthHint -> LengthHint
minLengthHint :: LengthHint -> LengthHint -> LengthHint
minLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthExact ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthExact Sz1
m) (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthExact Sz1
m) LengthHint
LengthUnknown = Sz1 -> LengthHint
LengthMax Sz1
m
minLengthHint (LengthMax Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthMax Sz1
m) (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthMax Sz1
m) LengthHint
LengthUnknown = Sz1 -> LengthHint
LengthMax Sz1
m
minLengthHint LengthHint
LengthUnknown (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
minLengthHint LengthHint
LengthUnknown (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
minLengthHint LengthHint
LengthUnknown LengthHint
LengthUnknown = LengthHint
LengthUnknown
{-# INLINE minLengthHint #-}
toLengthMax :: LengthHint -> LengthHint
toLengthMax :: LengthHint -> LengthHint
toLengthMax (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
toLengthMax (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
toLengthMax LengthHint
LengthUnknown = LengthHint
LengthUnknown
{-# INLINE toLengthMax #-}
prescanlM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> Steps m a
prescanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Steps m b -> Steps m a
prescanlM a -> b -> m a
f a
acc Steps m b
ss = Steps m b
ss{stepsStream = S.prescanlM' f acc (stepsStream ss)}
{-# INLINE prescanlM #-}
postscanlM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> Steps m a
postscanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Steps m b -> Steps m a
postscanlM a -> b -> m a
f a
acc Steps m b
ss = Steps m b
ss{stepsStream = S.postscanlM' f acc (stepsStream ss)}
{-# INLINE postscanlM #-}
postscanlAccM :: Monad m => (c -> b -> m (a, c)) -> c -> Steps m b -> Steps m a
postscanlAccM :: forall (m :: * -> *) c b a.
Monad m =>
(c -> b -> m (a, c)) -> c -> Steps m b -> Steps m a
postscanlAccM c -> b -> m (a, c)
f c
acc Steps m b
ss = Steps m b
ss{stepsStream = postscanlAccStreamM f acc (stepsStream ss)}
{-# INLINE postscanlAccM #-}
postscanlAccStreamM :: Monad m => (c -> b -> m (a, c)) -> c -> S.Stream m b -> S.Stream m a
postscanlAccStreamM :: forall (m :: * -> *) c b a.
Monad m =>
(c -> b -> m (a, c)) -> c -> Stream m b -> Stream m a
postscanlAccStreamM c -> b -> m (a, c)
f c
w (S.Stream s -> m (Step s b)
step s
t) = c
w c -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((s, c) -> m (Step (s, c) a)) -> (s, c) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream (s, c) -> m (Step (s, c) a)
step' (s
t, c
w)
where
step' :: (s, c) -> m (Step (s, c) a)
step' (s
s, c
x) =
c
x c -> m (Step (s, c) a) -> m (Step (s, c) a)
forall a b. a -> b -> b
`seq`
do
Step s b
r <- s -> m (Step s b)
step s
s
case Step s b
r of
S.Yield b
y s
s' -> do
(a
a, c
z) <- c -> b -> m (a, c)
f c
x b
y
c
z c -> m (Step (s, c) a) -> m (Step (s, c) a)
forall a b. a -> b -> b
`seq` Step (s, c) a -> m (Step (s, c) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, c) -> Step (s, c) a
forall a s. a -> s -> Step s a
S.Yield a
a (s
s', c
z))
S.Skip s
s' -> Step (s, c) a -> m (Step (s, c) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, c) a -> m (Step (s, c) a))
-> Step (s, c) a -> m (Step (s, c) a)
forall a b. (a -> b) -> a -> b
$ (s, c) -> Step (s, c) a
forall s a. s -> Step s a
S.Skip (s
s', c
x)
Step s b
S.Done -> Step (s, c) a -> m (Step (s, c) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, c) a
forall s a. Step s a
S.Done
{-# INLINE [0] step' #-}
{-# INLINE postscanlAccStreamM #-}
scanlM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> Steps m a
scanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Steps m b -> Steps m a
scanlM a -> b -> m a
f a
acc Steps{Stream m b
stepsStream :: forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream :: Stream m b
stepsStream, LengthHint
stepsSize :: forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize :: LengthHint
stepsSize} =
Steps
{ stepsStream :: Stream m a
stepsStream = (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
S.scanlM' a -> b -> m a
f a
acc Stream m b
stepsStream
, stepsSize :: LengthHint
stepsSize = LengthHint -> LengthHint -> LengthHint
addLengthHint (Sz1 -> LengthHint
LengthExact Sz1
1) LengthHint
stepsSize
}
{-# INLINE scanlM #-}
scanl1M :: Monad m => (a -> a -> m a) -> Steps m a -> Steps m a
scanl1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Steps m a -> Steps m a
scanl1M a -> a -> m a
f Steps m a
ss = Steps m a
ss{stepsStream = S.scanl1M' f (stepsStream ss)}
{-# INLINE scanl1M #-}