{-# 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
-- Copyright   : (c) Alexey Kuleshevich 2019-2022
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
module Data.Massiv.Vector.Stream (
  -- | This module has a similar purpose as the 'Data.Vector.Fusion.Bundle.Monadic', but
  -- quite a bit simpler.
  --
  -- __Important__ - This module is still experimental, as such it is considered
  -- internal and exported for the curious users only.
  Steps (..),
  Stream (..),

  -- * Conversion
  steps,
  isteps,
  consume,
  fromStream,
  fromStreamM,
  fromStreamExactM,
  unstreamExact,
  unstreamMax,
  unstreamMaxM,
  unstreamUnknown,
  unstreamUnknownM,
  unstreamIntoM,

  -- * Operations on Steps
  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_,

  -- ** Folding
  foldl,
  foldl1,
  foldlM,
  foldl1M,
  foldlLazy,
  foldl1Lazy,
  foldlLazyM,
  foldl1LazyM,
  foldrLazy,
  foldr1Lazy,
  foldrLazyM,
  foldr1LazyM,
  or,
  and,

  -- ** Unfolding
  unfoldr,
  unfoldrN,
  unsafeUnfoldrN,
  unfoldrM,
  unfoldrNM,
  unsafeUnfoldrNM,
  unfoldrExactN,
  unfoldrExactNM,

  -- ** Scanning
  prescanlM,
  postscanlM,
  postscanlAccM,
  scanlM,
  scanl1M,

  -- ** Enumeration
  enumFromStepN,

  -- * Lists
  toList,
  fromList,
  fromListN,
  unsafeFromListN,

  -- ** Filter
  mapMaybe,
  mapMaybeA,
  mapMaybeM,
  filter,
  filterA,
  filterM,

  -- * Transformations
  transSteps,
  transStepsId,

  -- * Useful re-exports
  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 :: Stream m b
stepsStream = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
S.map a -> b
f (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m a
str)}
  {-# INLINE fmap #-}
  <$ :: forall a b. a -> Steps m b -> Steps m a
(<$) a
e Steps m b
str =
    case forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize Steps m b
str of
      LengthExact Sz1
n -> Steps m b
str{stepsStream :: Stream m a
stepsStream = forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
n) a
e}
      LengthHint
_ -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (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
(<>) = 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 = 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 = forall e. Steps Id e -> [e]
toList
  {-# INLINE toList #-}
  fromList :: [Item (Steps Id e)] -> Steps Id e
fromList = forall (m :: * -> *) e. Monad m => [e] -> Steps m e
fromList
  {-# INLINE fromList #-}
  fromListN :: Int -> [Item (Steps Id e)] -> Steps Id e
fromListN Int
n = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax (forall ix. Index ix => ix -> Sz ix
Sz Int
n)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall e. Steps Id e -> [e]
toList
  {-# INLINE toList #-}
  length :: forall a. Steps Id a -> Int
length = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Steps m a -> m Int
length
  {-# INLINE length #-}
  null :: forall a. Steps Id a -> Bool
null = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Steps m a -> m Bool
null
  {-# INLINE null #-}
  sum :: forall a. Num a => Steps Id a -> a
sum = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl forall a. Num a => a -> a -> a
(+) a
0
  {-# INLINE sum #-}
  product :: forall a. Num a => Steps Id a -> a
product = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl forall a. Num a => a -> a -> a
(*) a
1
  {-# INLINE product #-}
  maximum :: forall a. Ord a => Steps Id a -> a
maximum = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 forall a. Ord a => a -> a -> a
max
  {-# INLINE maximum #-}
  minimum :: forall a. Ord a => Steps Id a -> a
minimum = forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 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 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall ix (m :: * -> *). (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)
    PrefIndexLinear Int -> e
gi ->
      forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 (coerce :: forall a b. Coercible a b => a -> b
coerce Int
k))
      where
        !k :: Int
k = forall ix. Index ix => Sz ix -> Int
totalElem forall a b. (a -> b) -> a -> b
$ forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr
        step :: Int -> m (Step Int e)
step !Int
i
          | Int
i forall a. Ord a => a -> a -> Bool
< Int
k = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
S.Yield (Int -> e
gi Int
i) (Int
i forall a. Num a => a -> a -> a
+ Int
1)
          | Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 = forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz
    !initStep :: Maybe ix
initStep = if Sz1
k forall a. Eq a => a -> a -> Bool
== forall ix. Index ix => Sz ix
zeroSz then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just forall ix. Index ix => ix
zeroIndex
    step :: Maybe ix -> m (Step (Maybe ix) ix)
step (Just ix
ix) = forall ix (f :: * -> *) a.
Index ix =>
ix -> ix -> ix -> (Int -> Int -> Bool) -> (Maybe ix -> f a) -> f a
stepNextMF ix
ix (forall ix. Sz ix -> ix
unSz Sz ix
sz) forall ix. Index ix => ix
oneIndex forall a. Ord a => a -> a -> Bool
(<) forall a b. (a -> b) -> a -> b
$ \Maybe ix
mIx -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
S.Yield ix
ix Maybe ix
mIx
    step Maybe ix
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 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 seq :: forall a b. a -> b -> b
`seq` (ix
ix, e
e)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall ix (m :: * -> *). (Monad m, Index ix) => Sz ix -> Steps m ix
ixRangeSteps Sz ix
sz
    PrefIndexLinear Int -> e
gi ->
      let k :: Int
k = forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz
          step :: Int -> m (Step Int (ix, e))
step Int
i
            | Int
i forall a. Ord a => a -> a -> Bool
< Int
k =
                let e :: e
e = Int -> e
gi Int
i
                 in e
e seq :: forall a b. a -> b -> b
`seq` forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
S.Yield (forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex Sz ix
sz Int
i, e
e) (Int
i forall a. Num a => a -> a -> a
+ Int
1)
            | Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
S.Done
          {-# INLINE [0] step #-}
       in forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 (coerce :: forall a b. Coercible a b => a -> b
coerce Int
k))
  where
    !sz :: Sz ix
sz = forall r ix e. Size r => 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 -> forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown Stream Id e
str
    Just Int
k -> 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 <- 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 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown (forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
    Just Int
k -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
k (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 <- forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m e
str
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall r ix e.
(Manifest r e, Index ix) =>
Sz ix -> Stream Id e -> Array r ix e
unstreamExact Sz ix
sz (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 forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ 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
_ -> 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)
unsafeLinearShrink MVector (PrimState m) r a
marr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. ix -> Sz ix
SafeSz forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< 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 -> 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 a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int e
marr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (forall ix. ix -> Sz ix
SafeSz Int
kMax)
    Int
k <- 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
marr Stream Id e
str
    MArray s r Int e
marrShrunk <-
      if Int
k forall a. Eq a => a -> a -> Bool
== Int
kMax
        then forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray s r Int e
marr
        else 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)
unsafeLinearShrink MArray s r Int e
marr (forall ix. ix -> Sz ix
SafeSz Int
k)
    forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray 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 = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Id a -> a
unId)
  where
    fillAtIndex :: Int -> a -> m Int
fillAtIndex Int
i a
x = (Int
i forall a. Num a => a -> a -> a
+ Int
1) forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> 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 a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int a
marr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew forall ix. Index ix => Sz ix
zeroSz
    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
marr Stream Id a
str forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
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) <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM' 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) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Id a -> a
unId) Stream Id a
str
  if Int
k forall a. Ord a => a -> a -> Bool
< forall ix. Sz ix -> ix
unSz (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 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)
unsafeLinearShrink MVector (PrimState m) r a
marr' (forall ix. ix -> Sz ix
SafeSz Int
k)
    else 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 = forall ix. Sz ix -> ix
unSz (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 forall a. Ord a => a -> a -> Bool
< Int
k
          then forall (f :: * -> *) a. Applicative f => a -> f a
pure MArray (PrimState m) r Int e
ma
          else 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)
unsafeLinearGrow MArray (PrimState m) r Int e
ma (forall ix. ix -> Sz ix
SafeSz (forall a. Ord a => a -> a -> a
max Int
1 Int
k forall a. Num a => a -> a -> a
* Int
2))
      (MArray (PrimState m) r Int e
ma', Int
i forall a. Num a => a -> a -> a
+ Int
1) forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall r e ix (m :: * -> *).
(Manifest r e, 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 a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MArray s r ix e
marr <- forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
    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
marr Stream Id e
str
    forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray 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 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
k
    LengthHint
_ -> 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 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Sz1
k forall a. Eq a => a -> a -> Bool
== forall ix. Index ix => Sz ix
zeroSz)
    LengthHint
_ -> 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps forall (m :: * -> *) a. Monad m => Stream m a
S.empty (Sz1 -> LengthHint
LengthExact 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton e
e) (Sz1 -> LengthHint
LengthExact 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
S.generate (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
k) Int -> e
f) (Sz1 -> LengthHint
LengthExact Sz1
k)
{-# INLINE generate #-}

-- | First element of the 'Stream' or error if empty
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
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ 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 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 #-}

-- | First element of the `Steps` or `Nothing` if empty
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 -> (,forall (m :: * -> *) a. Monad m => Sz1 -> Steps m a -> Steps m a
drop forall ix. Index ix => Sz ix
oneSz Steps m e
sts) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe e
mx) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
k) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e
str1 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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
_) = 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
    (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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
    (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
_) = 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 = forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume forall a b. (a -> b) -> a -> b
$ 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 = forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume forall a b. (a -> b) -> a -> b
$ 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 = forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume forall a b. (a -> b) -> a -> b
$ 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 = forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume forall a b. (a -> b) -> a -> b
$ 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 -> 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
_)) = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
sz) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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) <- forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Sz1, Stream n a)
transListNM Stream m e
strM
  forall (f :: * -> *) a. Applicative f => a -> f a
pure (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 = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl' b -> a -> b
f b
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1' a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
_) = 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 = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr a -> b -> b
f b
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldr1 a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl b -> a -> b
f b
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1 a -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
_) = 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
_) = 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 = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldr1M a -> a -> m a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.or forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.and forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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
_) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
S.concatMap (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream 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) = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA (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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [Maybe a] -> [a]
catMaybes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t 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) = 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
          forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
            case Maybe b
b of
              Maybe b
Nothing -> forall s a. s -> Step s a
S.Skip s
s'
              Just b
b' -> forall a s. a -> s -> Step s a
S.Yield b
b' s
s'
        S.Skip s
s' -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
S.Skip s
s'
        Step s a
S.Done -> forall (f :: * -> *) a. Applicative f => a -> f a
pure 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA (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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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) =
  forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.take (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
n) Stream m a
str) forall a b. (a -> b) -> a -> b
$!
    case LengthHint
sz of
      LengthExact Sz1
k -> Sz1 -> LengthHint
LengthExact (forall a b. (a -> b) -> a -> b
inline0 forall a. Ord a => a -> a -> a
min Sz1
n Sz1
k)
      LengthMax Sz1
k -> Sz1 -> LengthHint
LengthMax (forall a b. (a -> b) -> a -> b
inline0 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) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.drop (coerce :: 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
_) = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a.
Monad m =>
Int -> Int -> Stream m a -> Stream m a
S.slice Int
i (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a.
Monad m =>
Int -> (a -> a) -> a -> Stream m a
S.iterateN (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
S.iterateNM (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
S.generateM (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (coerce :: 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM (coerce :: 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 = forall (m :: * -> *) s a.
Monad m =>
Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Sz1
n (forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (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, 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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
S.Done
      | Bool
otherwise = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x, s
s') -> forall a s. a -> s -> Step s a
S.Yield a
x (s
s', Int
i 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 = forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
S.enumFromStepN a
x a
step (coerce :: 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
_) = forall a. Id a -> a
unId (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 = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = (forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax Sz1
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN (coerce :: 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 = forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> f [b]
f (forall a. Id a -> a
unId (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 <- forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m a
str
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ 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) <- forall (m :: * -> *) a. Monad m => Stream m a -> m (Int, [a])
toListN Stream m a
str
  forall (f :: * -> *) a. Applicative f => a -> f a
pure (coerce :: forall a b. Coercible a b => a -> b
coerce Int
n, 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 = 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 forall a. Num a => a -> a -> a
+ Int
1, a
x 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 forall a. Eq a => a -> a -> Bool
== coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
sz = Sz1 -> LengthHint
hint Sz1
sz
  | Bool
otherwise = LengthHint
LengthUnknown -- overflow
  where
    k :: Int
k = Int
m forall a. Num a => a -> a -> a
+ Int
n
    sz :: Sz1
sz = 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 (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
m) (coerce :: 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 (coerce :: 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 (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
m) (coerce :: 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 (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
m) (coerce :: 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 (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
m) (coerce :: 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 (coerce :: forall a b. Coercible a b => a -> b
coerce Sz1
m) (coerce :: 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 forall a. Num a => a -> a -> a
- Sz1
n)
subtractLengthHint (LengthMax Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax (Sz1
m 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 (forall a b. (a -> b) -> a -> b
inline0 forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthExact Sz1
m) (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax (forall a b. (a -> b) -> a -> b
inline0 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 (forall a b. (a -> b) -> a -> b
inline0 forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthMax Sz1
m) (LengthMax Sz1
n) = Sz1 -> LengthHint
LengthMax (forall a b. (a -> b) -> a -> b
inline0 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 #-}

-- | Prefix scan with strict accumulator and a monadic operator
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 :: Stream m a
stepsStream = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.prescanlM' a -> b -> m a
f a
acc (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m b
ss)}
{-# INLINE prescanlM #-}

-- | Suffix scan with a monadic operator
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 :: Stream m a
stepsStream = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.postscanlM' a -> b -> m a
f a
acc (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m b
ss)}
{-# INLINE postscanlM #-}

-- | Suffix scan with a monadic operator
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 :: Stream m a
stepsStream = 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
acc (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m b
ss)}
{-# INLINE postscanlAccM #-}

-- | Suffix scan with strict acccumulator and a monadic operator
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 seq :: forall a b. a -> b -> b
`seq` 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 seq :: 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 seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
S.Yield a
a (s
s', c
z))
            S.Skip s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
S.Skip (s
s', c
x)
            Step s b
S.Done -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
S.Done
    {-# INLINE [0] step' #-}
{-# INLINE postscanlAccStreamM #-}

-- | Haskell-style scan with a monadic operator
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 :: Stream m b
stepsStream :: forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream, LengthHint
stepsSize :: LengthHint
stepsSize :: forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize} =
  Steps
    { stepsStream :: Stream m a
stepsStream = 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 #-}

-- | Initial-value free scan over a 'Stream' with a strict accumulator
-- and a monadic operator
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 :: Stream m a
stepsStream = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
S.scanl1M' a -> a -> m a
f (forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m a
ss)}
{-# INLINE scanl1M #-}