{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_HADDOCK hide, not-home #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module      : Data.Massiv.Vector.Stream
-- Copyright   : (c) Alexey Kuleshevich 2019-2020
-- 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
  -- * Bundle
  , toBundle
  , fromBundle
  , fromBundleM
  -- * 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
  -- ** Enumeration
  , enumFromStepN
  -- * Lists
  , toList
  , fromList
  , fromListN
  , unsafeFromListN
  -- ** Filter
  , mapMaybe
  , mapMaybeA
  , mapMaybeM
  , filter
  , filterA
  , filterM
  -- * Transformations
  , transSteps
  , transStepsId
  -- * Useful re-exports
  , module Data.Vector.Fusion.Bundle.Size
  , module Data.Vector.Fusion.Util
  , Id(..)
  ) where

import qualified Control.Monad as M
import Control.Monad.ST
import qualified Data.Foldable as F
import Data.Massiv.Core.Common hiding (empty, singleton)
import Data.Maybe (catMaybes)
import qualified Data.Traversable as Traversable (traverse)
import qualified Data.Vector.Fusion.Bundle.Monadic as B
import Data.Vector.Fusion.Bundle.Size
import qualified Data.Vector.Fusion.Stream.Monadic as S
import Data.Vector.Fusion.Util
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 :: (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 = (a -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
S.map a -> b
f (Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m a
str)}
  {-# INLINE fmap #-}
  <$ :: a -> Steps m b -> Steps m a
(<$) a
e Steps m b
str =
    case Steps m b -> Size
forall (m :: * -> *) e. Steps m e -> Size
stepsSize Steps m b
str of
      Exact Int
n -> Steps m b
str {stepsStream :: Stream m a
stepsStream = Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate Int
n a
e}
      Size
_       -> (b -> a) -> Steps m b -> Steps m a
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 #-}
  mappend :: Steps m e -> Steps m e -> Steps m e
mappend = 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 mappend #-}


instance Foldable (Steps Id) where
  foldr :: (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 :: (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' :: (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 :: (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 :: (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 :: Steps Id a -> [a]
toList = Steps Id a -> [a]
forall a. Steps Id a -> [a]
toList
  {-# INLINE toList #-}
  length :: 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 :: 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 :: 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 :: 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 :: 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 :: 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 #-}


-- TODO: benchmark: `fmap snd . isteps`
steps :: forall r ix e m . (Monad m, Source r ix e) => Array r ix e -> Steps m e
steps :: Array r ix e -> Steps m e
steps Array r ix e
arr = Int
k Int -> Steps m e -> Steps m e
`seq` Array r ix e
arr Array r ix e -> Steps m e -> Steps m e
`seq` Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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)
forall (m :: * -> *). Monad m => Int -> m (Step Int e)
step Int
0) (Int -> Size
Exact 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. Load r 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 =
        let e :: e
e = Array r ix e -> Int -> e
forall r ix e. Source r ix e => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr Int
i
         in e
e e -> m (Step Int e) -> m (Step Int e)
`seq` Step Int e -> m (Step Int e)
forall (m :: * -> *) a. Monad m => a -> m a
return (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 e
e (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      | Bool
otherwise = Step Int e -> m (Step Int e)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int e
forall s a. Step s a
S.Done
    {-# INLINE step #-}
{-# INLINE steps #-}


isteps :: forall r ix e m . (Monad m, Source r ix e) => Array r ix e -> Steps m (ix, e)
isteps :: Array r ix e -> Steps m (ix, e)
isteps Array r ix e
arr = Int
k Int -> Steps m (ix, e) -> Steps m (ix, e)
`seq` Array r ix e
arr Array r ix e -> Steps m (ix, e) -> Steps m (ix, e)
`seq` Stream m (ix, e) -> Size -> Steps m (ix, e)
forall (m :: * -> *) e. Stream m e -> Size -> 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))
forall (m :: * -> *). Monad m => Int -> m (Step Int (ix, e))
step Int
0) (Int -> Size
Exact Int
k)
  where
    sz :: Sz ix
sz = Array r ix e -> Sz ix
forall r ix e. Load r ix e => Array r ix e -> Sz ix
size Array r ix e
arr
    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 = Array r ix e -> Int -> e
forall r ix e. Source r ix e => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr Int
i
         in e
e e -> m (Step Int (ix, e)) -> m (Step Int (ix, e))
`seq` Step Int (ix, e) -> m (Step Int (ix, e))
forall (m :: * -> *) a. Monad m => a -> m a
return (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 (m :: * -> *) a. Monad m => a -> m a
return Step Int (ix, e)
forall s a. Step s a
S.Done
    {-# INLINE step #-}
{-# INLINE isteps #-}

toBundle :: (Monad m, Source r ix e) => Array r ix e -> B.Bundle m v e
toBundle :: Array r ix e -> Bundle m v e
toBundle Array r ix e
arr =
  let Steps Stream m e
str Size
k = Array r ix e -> Steps m e
forall r ix e (m :: * -> *).
(Monad m, Source r ix e) =>
Array r ix e -> Steps m e
steps Array r ix e
arr
   in Stream m e -> Size -> Bundle m v e
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
B.fromStream Stream m e
str Size
k
{-# INLINE toBundle #-}

fromBundle :: Mutable r Ix1 e => B.Bundle Id v e -> Array r Ix1 e
fromBundle :: Bundle Id v e -> Array r Int e
fromBundle Bundle Id v e
bundle = Size -> Stream Id e -> Array r Int e
forall r e. Mutable r Int e => Size -> Stream Id e -> Array r Int e
fromStream (Bundle Id v e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
B.sSize Bundle Id v e
bundle) (Bundle Id v e -> Stream Id e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
B.sElems Bundle Id v e
bundle)
{-# INLINE fromBundle #-}


fromBundleM :: (Monad m, Mutable r Ix1 e) => B.Bundle m v e -> m (Array r Ix1 e)
fromBundleM :: Bundle m v e -> m (Array r Int e)
fromBundleM Bundle m v e
bundle = Size -> Stream m e -> m (Array r Int e)
forall r e (m :: * -> *).
(Monad m, Mutable r Int e) =>
Size -> Stream m e -> m (Array r Int e)
fromStreamM (Bundle m v e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
B.sSize Bundle m v e
bundle) (Bundle m v e -> Stream m e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
B.sElems Bundle m v e
bundle)
{-# INLINE fromBundleM #-}


fromStream :: forall r e . Mutable r Ix1 e => Size -> S.Stream Id e -> Array r Ix1 e
fromStream :: Size -> Stream Id e -> Array r Int e
fromStream Size
sz Stream Id e
str =
  case Size -> Maybe Int
upperBound Size
sz of
    Maybe Int
Nothing -> Stream Id e -> Array r Int e
forall r a. Mutable r Int a => Stream Id a -> Array r Int a
unstreamUnknown Stream Id e
str
    Just Int
k  -> Int -> Stream Id e -> Array r Int e
forall r e. Mutable r Int e => Int -> Stream Id e -> Array r Int e
unstreamMax Int
k Stream Id e
str
{-# INLINE fromStream #-}

fromStreamM :: forall r e m. (Monad m, Mutable r Ix1 e) => Size -> S.Stream m e -> m (Array r Ix1 e)
fromStreamM :: Size -> Stream m e -> m (Array r Int 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
upperBound Size
sz of
    Maybe Int
Nothing -> Array r Int e -> m (Array r Int e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array r Int e -> m (Array r Int e))
-> Array r Int e -> m (Array r Int e)
forall a b. (a -> b) -> a -> b
$! Stream Id e -> Array r Int e
forall r a. Mutable r Int a => Stream Id a -> Array r Int a
unstreamUnknown ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
    Just Int
k  -> Array r Int e -> m (Array r Int e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array r Int e -> m (Array r Int e))
-> Array r Int e -> m (Array r Int e)
forall a b. (a -> b) -> a -> b
$! Int -> Stream Id e -> Array r Int e
forall r e. Mutable r Int e => Int -> Stream Id e -> Array r Int 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, Mutable r ix e)
  => Sz ix
  -> S.Stream m e
  -> m (Array r ix e)
fromStreamExactM :: 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 (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.
Mutable r ix e =>
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 ::
     (Mutable r Ix1 a, PrimMonad m)
  => MArray (PrimState m) r Ix1 a
  -> Size
  -> S.Stream Id a
  -> m (MArray (PrimState m) r Ix1 a)
unstreamIntoM :: MArray (PrimState m) r Int a
-> Size -> Stream Id a -> m (MArray (PrimState m) r Int a)
unstreamIntoM MArray (PrimState m) r Int a
marr Size
sz Stream Id a
str =
  case Size
sz of
    Exact Int
_ -> MArray (PrimState m) r Int a
marr MArray (PrimState m) r Int a
-> m Int -> m (MArray (PrimState m) r Int a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MArray (PrimState m) r Int a -> Stream Id a -> m Int
forall r ix a (m :: * -> *).
(Mutable r ix a, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray (PrimState m) r Int a
marr Stream Id a
str
    Max Int
_   -> MArray (PrimState m) r Int a
-> Sz Int -> m (MArray (PrimState m) r Int a)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MArray (PrimState m) r Int a
marr (Sz Int -> m (MArray (PrimState m) r Int a))
-> (Int -> Sz Int) -> Int -> m (MArray (PrimState m) r Int a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Sz Int
forall ix. ix -> Sz ix
SafeSz (Int -> m (MArray (PrimState m) r Int a))
-> m Int -> m (MArray (PrimState m) r Int a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MArray (PrimState m) r Int a -> Stream Id a -> m Int
forall r ix a (m :: * -> *).
(Mutable r ix a, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray (PrimState m) r Int a
marr Stream Id a
str
    Size
Unknown -> MArray (PrimState m) r Int a
-> Stream Id a -> m (MArray (PrimState m) r Int a)
forall r a (m :: * -> *).
(Mutable r Int a, PrimMonad m) =>
MArray (PrimState m) r Int a
-> Stream Id a -> m (MArray (PrimState m) r Int a)
unstreamUnknownM MArray (PrimState m) r Int a
marr Stream Id a
str
{-# INLINE unstreamIntoM #-}



unstreamMax ::
     forall r e. (Mutable r Ix1 e)
  => Int
  -> S.Stream Id e
  -> Array r Ix1 e
unstreamMax :: Int -> Stream Id e -> Array r Int e
unstreamMax Int
kMax Stream Id e
str =
  (forall s. ST s (Array r Int e)) -> Array r Int e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Array r Int e)) -> Array r Int e)
-> (forall s. ST s (Array r Int e)) -> Array r Int e
forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int e
marr <- Sz Int -> ST s (MArray (PrimState (ST s)) r Int e)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (Int -> Sz Int
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 ix a (m :: * -> *).
(Mutable r ix a, 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 (PrimState (ST s)) r Int e
-> Sz Int -> ST s (MArray (PrimState (ST s)) r Int e)
forall r ix e (m :: * -> *).
(Mutable r ix e, 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 -> Sz Int
forall ix. ix -> Sz ix
SafeSz Int
k) ST s (MArray s r Int e)
-> (MArray s r Int e -> ST s (Array r Int e))
-> ST s (Array r Int e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Comp -> MArray (PrimState (ST s)) r Int e -> ST s (Array r Int e)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq
{-# INLINE unstreamMax #-}


unstreamMaxM ::
     (Mutable r ix a, PrimMonad m) => MArray (PrimState m) r ix a -> S.Stream Id a -> m Int
unstreamMaxM :: MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray (PrimState m) r ix a
marr (S.Stream s -> Id (Step s a)
step s
s) = s -> Int -> m Int
stepLoad s
s Int
0
  where
    stepLoad :: s -> Int -> m Int
stepLoad s
t Int
i =
      case Id (Step s a) -> Step s a
forall a. Id a -> a
unId (s -> Id (Step s a)
step s
t) of
        S.Yield a
e' s
t' -> do
          MArray (PrimState m) r ix a -> Int -> a -> m ()
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix a
marr Int
i a
e'
          s -> Int -> m Int
stepLoad s
t' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
        S.Skip s
t' -> s -> Int -> m Int
stepLoad s
t' Int
i
        Step s a
S.Done -> Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
    {-# INLINE stepLoad #-}
{-# INLINE unstreamMaxM #-}


unstreamUnknown :: Mutable r Ix1 a => S.Stream Id a -> Array r Ix1 a
unstreamUnknown :: Stream Id a -> Array r Int a
unstreamUnknown Stream Id a
str =
  (forall s. ST s (Array r Int a)) -> Array r Int a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Array r Int a)) -> Array r Int a)
-> (forall s. ST s (Array r Int a)) -> Array r Int a
forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int a
marr <- Sz Int -> ST s (MArray (PrimState (ST s)) r Int a)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz Int
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 :: * -> *).
(Mutable r Int a, PrimMonad m) =>
MArray (PrimState m) r Int a
-> Stream Id a -> m (MArray (PrimState m) r Int 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 (Array r Int a))
-> ST s (Array r Int a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Comp -> MArray (PrimState (ST s)) r Int a -> ST s (Array r Int a)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq
{-# INLINE unstreamUnknown #-}


unstreamUnknownM ::
     (Mutable r Ix1 a, PrimMonad m)
  => MArray (PrimState m) r Ix1 a
  -> S.Stream Id a
  -> m (MArray (PrimState m) r Ix1 a)
unstreamUnknownM :: MArray (PrimState m) r Int a
-> Stream Id a -> m (MArray (PrimState m) r Int a)
unstreamUnknownM MArray (PrimState m) r Int a
marrInit (S.Stream s -> Id (Step s a)
step s
s) = s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
forall (m :: * -> *) r.
(Mutable r Int a, PrimMonad m) =>
s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
stepLoad s
s Int
0 (Sz Int -> Int
forall ix. Sz ix -> ix
unSz (MArray (PrimState m) r Int a -> Sz Int
forall r ix e s. Mutable r ix e => MArray s r ix e -> Sz ix
msize MArray (PrimState m) r Int a
marrInit)) MArray (PrimState m) r Int a
marrInit
  where
    stepLoad :: s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
stepLoad s
t Int
i Int
kMax MArray (PrimState m) r Int a
marr
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
kMax =
        case Id (Step s a) -> Step s a
forall a. Id a -> a
unId (s -> Id (Step s a)
step s
t) of
          S.Yield a
e' s
t' -> do
            MArray (PrimState m) r Int a -> Int -> a -> m ()
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r Int a
marr Int
i a
e'
            s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
stepLoad s
t' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
kMax MArray (PrimState m) r Int a
marr
          S.Skip s
t' -> s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
stepLoad s
t' Int
i Int
kMax MArray (PrimState m) r Int a
marr
          Step s a
S.Done -> MArray (PrimState m) r Int a
-> Sz Int -> m (MArray (PrimState m) r Int a)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MArray (PrimState m) r Int a
marr (Int -> Sz Int
forall ix. ix -> Sz ix
SafeSz Int
i)
      | Bool
otherwise = do
        let kMax' :: Int
kMax' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int
kMax Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
        MArray (PrimState m) r Int a
marr' <- MArray (PrimState m) r Int a
-> Sz Int -> m (MArray (PrimState m) r Int a)
forall r ix e (m :: * -> *).
(Mutable r ix e, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearGrow MArray (PrimState m) r Int a
marr (Int -> Sz Int
forall ix. ix -> Sz ix
SafeSz Int
kMax')
        s
-> Int
-> Int
-> MArray (PrimState m) r Int a
-> m (MArray (PrimState m) r Int a)
stepLoad s
t Int
i Int
kMax' MArray (PrimState m) r Int a
marr'
    {-# INLINE stepLoad #-}
{-# INLINE unstreamUnknownM #-}


unstreamExact ::
     forall r ix e. (Mutable r ix e)
  => Sz ix
  -> S.Stream Id e
  -> Array r ix e
unstreamExact :: 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 ix e (m :: * -> *).
(Mutable r ix e, 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 ix a (m :: * -> *).
(Mutable r ix a, 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 ix e (m :: * -> *).
(Mutable r ix e, 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 :: Steps m a -> m Int
length (Steps Stream m a
str Size
sz) =
  case Size
sz of
    Exact Int
k -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
k
    Size
_       -> 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 :: Steps m a -> m Bool
null (Steps Stream m a
str Size
sz) =
  case Size
sz of
    Exact Int
k -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
    Size
_       -> 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 :: Steps m e
empty = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps Stream m e
forall (m :: * -> *) a. Monad m => Stream m a
S.empty (Int -> Size
Exact Int
0)
{-# INLINE empty #-}

singleton :: Monad m => e -> Steps m e
singleton :: e -> Steps m e
singleton e
e = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps (e -> Stream m e
forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton e
e) (Int -> Size
Exact Int
1)
{-# INLINE singleton #-}

generate :: Monad m => Int -> (Int -> e) -> Steps m e
generate :: Int -> (Int -> e) -> Steps m e
generate Int
k Int -> e
f = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps (Int -> (Int -> e) -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
S.generate Int
k Int -> e
f) (Int -> Size
Exact Int
k)
{-# INLINE generate #-}

-- | First element of the 'Stream' or error if empty
headMaybe :: Monad m => Steps m a -> m (Maybe a)
headMaybe :: Steps m a -> m (Maybe a)
headMaybe (Steps (S.Stream s -> m (Step s a)
step s
t) Size
_) = 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 (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 (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 :: e -> Steps m e -> Steps m e
cons e
e (Steps Stream m e
str Size
k) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size
k Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
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 :: Steps m e -> m (Maybe (e, Steps m e))
uncons Steps m e
sts = (\Maybe e
mx -> (\e
x -> (e
x, Int -> Steps m e -> Steps m e
forall (m :: * -> *) a. Monad m => Int -> Steps m a -> Steps m a
drop Int
1 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 :: Steps m e -> e -> Steps m e
snoc (Steps Stream m e
str Size
k) e
e = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size
k Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1)
{-# INLINE snoc #-}

traverse :: (Monad m, Applicative f) => (e -> f a) -> Steps Id e -> f (Steps m a)
traverse :: (e -> f a) -> Steps Id e -> f (Steps m a)
traverse e -> f a
f (Steps Stream Id e
str Size
k) = (Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size
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)
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 :: Steps m e -> Steps m e -> Steps m e
append (Steps Stream m e
str1 Size
k1) (Steps Stream m e
str2 Size
k2) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size
k1 Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
k2)
{-# INLINE append #-}

map :: Monad m => (e -> a) -> Steps m e -> Steps m a
map :: (e -> a) -> Steps m e -> Steps m a
map e -> a
f (Steps Stream m e
str Size
k) = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
k
{-# INLINE map #-}

indexed :: Monad m => Steps m e -> Steps m (Int, e)
indexed :: Steps m e -> Steps m (Int, e)
indexed (Steps Stream m e
str Size
k) = Stream m (Int, e) -> Size -> Steps m (Int, e)
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
k
{-# INLINE indexed #-}

mapM :: Monad m => (e -> m a) -> Steps m e -> Steps m a
mapM :: (e -> m a) -> Steps m e -> Steps m a
mapM e -> m a
f (Steps Stream m e
str Size
k) = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
k
{-# INLINE mapM #-}

mapM_ :: Monad m => (e -> m a) -> Steps m e -> m ()
mapM_ :: (e -> m a) -> Steps m e -> m ()
mapM_ e -> m a
f (Steps Stream m e
str Size
_) = (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 :: (a -> b -> e) -> Steps m a -> Steps m b -> Steps m e
zipWith a -> b -> e
f (Steps Stream m a
sa Size
ka) (Steps Stream m b
sb Size
kb) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka Size
kb)
{-# INLINE zipWith #-}

zipWith3 :: Monad m => (a -> b -> c -> d) -> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) =
  Stream m d -> Size -> Steps m d
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) =
  Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) (Steps Stream m e
se Size
ke) =
  Stream m f -> Size -> Steps m f
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc (Size -> Size -> Size
smaller Size
kd Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) (Steps Stream m e
se Size
ke) (Steps Stream m f
sf Size
kf) =
  Stream m g -> Size -> Steps m g
forall (m :: * -> *) e. Stream m e -> Size -> 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)
    (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc (Size -> Size -> Size
smaller Size
kd (Size -> Size -> Size
smaller Size
ke Size
kf)))))
{-# INLINE zipWith6 #-}

zipWithM :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM :: (a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM a -> b -> m c
f (Steps Stream m a
sa Size
ka) (Steps Stream m b
sb Size
kb) = Stream m c -> Size -> Steps m c
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka Size
kb)
{-# INLINE zipWithM #-}


zipWith3M :: 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)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M a -> b -> c -> m d
f (Steps Stream m a
sa Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) =
  Stream m d -> Size -> Steps m d
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) =
  Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) (Steps Stream m e
se Size
ke) =
  Stream m f -> Size -> Steps m f
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc (Size -> Size -> Size
smaller Size
kd Size
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 :: (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 Size
ka) (Steps Stream m b
sb Size
kb) (Steps Stream m c
sc Size
kc) (Steps Stream m d
sd Size
kd) (Steps Stream m e
se Size
ke) (Steps Stream m f
sf Size
kf) =
  Stream m g -> Size -> Steps m g
forall (m :: * -> *) e. Stream m e -> Size -> 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)
    (Size -> Size -> Size
smaller Size
ka (Size -> Size -> Size
smaller Size
kb (Size -> Size -> Size
smaller Size
kc (Size -> Size -> Size
smaller Size
kd (Size -> Size -> Size
smaller Size
ke Size
kf)))))
{-# INLINE zipWith6M #-}


zipWithM_ :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ :: (a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ a -> b -> m c
f (Steps Stream m a
str1 Size
_) (Steps Stream m b
str2 Size
_) = (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_ :: (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_ :: (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_ :: (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_ :: (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 :: Steps m a -> m ()
consume (Steps (S.Stream s -> m (Step s a)
step s
t) Size
_) = 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 (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE consume #-}

transStepsId :: Monad m => Steps Id e -> Steps m e
transStepsId :: Steps Id e -> Steps m e
transStepsId (Steps Stream Id e
sts Size
k) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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 (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) Size
k
{-# INLINE transStepsId #-}

transSteps :: (Monad m, Monad n) => Steps m e -> m (Steps n e)
transSteps :: Steps m e -> m (Steps n e)
transSteps (Steps Stream m e
strM sz :: Size
sz@(Exact Int
_)) = (Stream n e -> Size -> Steps n e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size
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 Size
_) = do
  (Int
n, Stream n e
strN) <- Stream m e -> m (Int, Stream n e)
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Int, Stream n a)
transListNM Stream m e
strM
  Steps n e -> m (Steps n e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Stream n e -> Size -> Steps n e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps Stream n e
strN (Int -> Size
Exact Int
n))
{-# INLINE transSteps #-}


foldl :: Monad m => (b -> a -> b) -> b -> Steps m a -> m b
foldl :: (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 :: (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 :: (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 :: (a -> a -> m a) -> Steps m a -> m a
foldl1M a -> a -> m a
f (Steps Stream m a
sts Size
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
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 :: (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 :: (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 :: (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 :: (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 :: (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 :: (a -> a -> m a) -> Steps m a -> m a
foldl1LazyM a -> a -> m a
f (Steps Stream m a
sts Size
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
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 :: (b -> a -> m a) -> a -> Steps m b -> m a
foldrLazyM b -> a -> m a
f a
acc (Steps Stream m b
sts Size
_) = (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 :: (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.
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 :: 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 :: 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 :: (a -> Maybe e) -> Steps m a -> Steps m e
mapMaybe a -> Maybe e
f (Steps Stream m a
str Size
k) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size
toMax Size
k)
{-# INLINE mapMaybe #-}

concatMap :: Monad m => (a -> Steps m e) -> Steps m a -> Steps m e
concatMap :: (a -> Steps m e) -> Steps m a -> Steps m e
concatMap a -> Steps m e
f (Steps Stream m a
str Size
_) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
Unknown
{-# INLINE concatMap #-}


mapMaybeA :: (Monad m, Applicative f) => (a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA :: (a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA a -> f (Maybe e)
f (Steps Stream Id a
str Size
k) = (Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size -> Size
toMax Size
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 :: (a -> m (Maybe b)) -> Steps m a -> Steps m b
mapMaybeM a -> m (Maybe b)
f (Steps Stream m a
str Size
k) = Stream m b -> Size -> Steps m b
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size
toMax Size
k)
{-# INLINE mapMaybeM #-}

mapMaybeListA :: Applicative f => (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA :: (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA a -> f (Maybe b)
f = ([Maybe b] -> [b]) -> f [Maybe b] -> 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)
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 :: (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 (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$
            case Maybe b
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 (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
S.Skip s
s'
        Step s a
S.Done -> Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
forall s a. Step s a
S.Done
    {-# INLINE [0] step' #-}
{-# INLINE mapMaybeStreamM #-}

filter :: Monad m => (a -> Bool) -> Steps m a -> Steps m a
filter :: (a -> Bool) -> Steps m a -> Steps m a
filter a -> Bool
f (Steps Stream m a
str Size
k) = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size
toMax Size
k)
{-# INLINE filter #-}


filterA :: (Monad m, Applicative f) => (e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA :: (e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA e -> f Bool
f (Steps Stream Id e
str Size
k) = (Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size -> Size
toMax Size
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 :: (e -> m Bool) -> Steps m e -> Steps m e
filterM e -> m Bool
f (Steps Stream m e
str Size
k) = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Size -> Size
toMax Size
k)
{-# INLINE filterM #-}

take :: Monad m => Int -> Steps m a -> Steps m a
take :: Int -> Steps m a -> Steps m a
take Int
n (Steps Stream m a
str Size
sz) =
  Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n Stream m a
str) (Size -> Steps m a) -> Size -> Steps m a
forall a b. (a -> b) -> a -> b
$!
  case Size
sz of
    Exact Int
k -> Int -> Size
Exact (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n Int
k)
    Max Int
k -> Int -> Size
Max (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n Int
k)
    Size
Unknown -> Size
Unknown
{-# INLINE take #-}

drop :: Monad m => Int -> Steps m a -> Steps m a
drop :: Int -> Steps m a -> Steps m a
drop Int
n (Steps Stream m a
str Size
k) = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n Stream m a
str) (Size
k Size -> Size -> Size
`clampedSubtract` Int -> Size
Exact Int
n)
{-# INLINE drop #-}

slice :: Monad m => Int -> Int -> Steps m a -> Steps m a
slice :: Int -> Int -> Steps m a -> Steps m a
slice Int
i Int
k (Steps Stream m a
str Size
_) = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
k Stream m a
str) (Int -> Size
Max Int
k)
{-# INLINE slice #-}

iterateN :: Monad m => Int -> (a -> a) -> a -> Steps m a
iterateN :: Int -> (a -> a) -> a -> Steps m a
iterateN Int
n a -> a
f a
a = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n a -> a
f a
a) (Int -> Size
Exact Int
n)
{-# INLINE iterateN #-}

iterateNM :: Monad m => Int -> (a -> m a) -> a -> Steps m a
iterateNM :: Int -> (a -> m a) -> a -> Steps m a
iterateNM Int
n a -> m a
f a
a = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n a -> m a
f a
a) (Int -> Size
Exact Int
n)
{-# INLINE iterateNM #-}

replicate :: Monad m => Int -> a -> Steps m a
replicate :: Int -> a -> Steps m a
replicate Int
n a
a = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps (Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate Int
n a
a) (Int -> Size
Exact Int
n)
{-# INLINE replicate #-}

replicateM :: Monad m => Int -> m a -> Steps m a
replicateM :: Int -> m a -> Steps m a
replicateM Int
n m a
f = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
Steps (Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM Int
n m a
f) (Int -> Size
Exact Int
n)
{-# INLINE replicateM #-}


generateM :: Monad m => Int -> (Int -> m a) -> Steps m a
generateM :: Int -> (Int -> m a) -> Steps m a
generateM Int
n Int -> m a
f = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n Int -> m a
f) (Int -> Size
Exact Int
n)
{-# INLINE generateM #-}


unfoldr :: Monad m => (s -> Maybe (e, s)) -> s -> Steps m e
unfoldr :: (s -> Maybe (e, s)) -> s -> Steps m e
unfoldr s -> Maybe (e, s)
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
Unknown
{-# INLINE unfoldr #-}

unfoldrN :: Monad m => Int -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN :: Int -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN Int
n s -> Maybe (e, s)
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n s -> Maybe (e, s)
f s
e0) Size
Unknown
{-# INLINE unfoldrN #-}

unsafeUnfoldrN :: Monad m => Int -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN :: Int -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN Int
n s -> Maybe (e, s)
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
n s -> Maybe (e, s)
f s
e0) (Int -> Size
Max Int
n)
{-# INLINE unsafeUnfoldrN #-}

unfoldrM :: Monad m => (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM :: (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM s -> m (Maybe (e, s))
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
Unknown
{-# INLINE unfoldrM #-}

unfoldrNM :: Monad m => Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM :: Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) Size
Unknown
{-# INLINE unfoldrNM #-}

unsafeUnfoldrNM :: Monad m => Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM :: Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> 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) (Int -> Size
Max Int
n)
{-# INLINE unsafeUnfoldrNM #-}

unfoldrExactN :: Monad m => Int -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN :: Int -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN Int
n s -> (a, s)
f = Int -> (s -> m (a, s)) -> s -> Steps m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Int
n ((a, s) -> m (a, s)
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 => Int -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM :: Int -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Int
n s -> m (a, s)
f s
t = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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)
forall b. (Ord b, Num b) => (s, b) -> m (Step (s, b) a)
step (s
t, Int
n)) (Int -> Size
Exact Int
n)
  where
    step :: (s, b) -> m (Step (s, b) a)
step (s
s, b
i)
      | b
i b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Step (s, b) a -> m (Step (s, b) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step (s, b) a
forall s a. Step s a
S.Done
      | Bool
otherwise = ((a, s) -> Step (s, b) a) -> m (a, s) -> m (Step (s, b) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x, s
s') -> a -> (s, b) -> Step (s, b) a
forall a s. a -> s -> Step s a
S.Yield a
x (s
s', b
i b -> b -> b
forall a. Num a => a -> a -> a
- b
1)) (s -> m (a, s)
f s
s)
    {-# INLINE [0] step #-}
{-# INLINE unfoldrExactNM #-}


enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Steps m a
enumFromStepN :: a -> a -> Int -> Steps m a
enumFromStepN a
x a
step Int
k = Stream m a -> Size -> Steps m a
forall (m :: * -> *) e. Stream m e -> Size -> 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 Int
k) (Int -> Size
Exact Int
k)
{-# INLINE enumFromStepN #-}




toList :: Steps Id e -> [e]
toList :: Steps Id e -> [e]
toList (Steps Stream Id e
str Size
_) = 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 :: [e] -> Steps m e
fromList = (Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size
Unknown) (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 :: Int -> [e] -> Steps m e
fromListN Int
n  = (Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Size
Unknown) (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 => Int -> [e] -> Steps m e
unsafeFromListN :: Int -> [e] -> Steps m e
unsafeFromListN Int
n  = (Stream m e -> Size -> Steps m e
forall (m :: * -> *) e. Stream m e -> Size -> Steps m e
`Steps` Int -> Size
Max Int
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 Int
n
{-# INLINE unsafeFromListN #-}

liftListA :: (Monad m, Functor f) => ([a] -> f [b]) -> S.Stream Id a -> f (S.Stream m b)
liftListA :: ([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 :: 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 (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 (Int, S.Stream n a)
transListNM :: Stream m a -> m (Int, 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
  (Int, Stream n a) -> m (Int, Stream n a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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 :: 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
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)) (Int
0, [])
{-# INLINE toListN #-}