{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
-- |
-- Module      : Data.Vector.Fusion.Bundle
-- Copyright   : (c) Roman Leshchinskiy 2008-2010
--                   Alexey Kuleshevich 2020-2022
--                   Aleksey Khudyakov 2020-2022
--                   Andrew Lelechenko 2020-2022
-- License     : BSD-style
--
-- Maintainer  : Haskell Libraries Team <libraries@haskell.org>
-- Stability   : experimental
-- Portability : non-portable
--
-- Bundles for stream fusion
--

module Data.Vector.Fusion.Bundle (
  -- * Types
  Step(..), Chunk(..), Bundle, MBundle,

  -- * In-place markers
  inplace,

  -- * Size hints
  size, sized,

  -- * Length information
  length, null,

  -- * Construction
  empty, singleton, cons, snoc, replicate, generate, (++),

  -- * Accessing individual elements
  head, last, (!!), (!?),

  -- * Substreams
  slice, init, tail, take, drop,

  -- * Mapping
  map, concatMap, flatten, unbox,

  -- * Zipping
  indexed, indexedR,
  zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
  zip, zip3, zip4, zip5, zip6,

  -- * Filtering
  filter, takeWhile, dropWhile,

  -- * Searching
  elem, notElem, find, findIndex,

  -- * Folding
  foldl, foldl1, foldl', foldl1', foldr, foldr1,

  -- * Specialised folds
  and, or,

  -- * Unfolding
  unfoldr, unfoldrN, unfoldrExactN, iterateN,

  -- * Scans
  prescanl, prescanl',
  postscanl, postscanl',
  scanl, scanl',
  scanl1, scanl1',

  -- * Enumerations
  enumFromStepN, enumFromTo, enumFromThenTo,

  -- * Conversions
  toList, fromList, fromListN, unsafeFromList, lift,
  fromVector, reVector, fromVectors, concatVectors,

  -- * Monadic combinators
  mapM, mapM_, zipWithM, zipWithM_, filterM, mapMaybeM, foldM, fold1M, foldM', fold1M',

  eq, cmp, eqBy, cmpBy
) where

import Data.Vector.Generic.Base ( Vector )
import Data.Vector.Fusion.Bundle.Size
import Data.Vector.Fusion.Util
import Data.Vector.Fusion.Stream.Monadic ( Stream(..), Step(..) )
import Data.Vector.Fusion.Bundle.Monadic ( Chunk(..), lift )
import qualified Data.Vector.Fusion.Bundle.Monadic as M
import qualified Data.Vector.Fusion.Stream.Monadic as S

import Prelude
  ( Eq, Ord, Num, Enum, Monad, Bool, Ordering, Int, Maybe
  , compare, return, seq
  , (==), (.) )

import Data.Functor.Classes (Eq1 (..), Ord1 (..))
import GHC.Base ( build )

-- Data.Vector.Internal.Check is unused
#define NOT_VECTOR_MODULE
#include "vector.h"

-- | The type of pure streams
type Bundle = M.Bundle Id

-- | Alternative name for monadic streams
type MBundle = M.Bundle

inplace :: (forall m. Monad m => S.Stream m a -> S.Stream m b)
        -> (Size -> Size) -> Bundle v a -> Bundle v b
{-# INLINE_FUSED inplace #-}
inplace :: forall a b (v :: * -> *).
(forall (m :: * -> *). Monad m => Stream m a -> Stream m b)
-> (Size -> Size) -> Bundle v a -> Bundle v b
inplace forall (m :: * -> *). Monad m => Stream m a -> Stream m b
f Size -> Size
g Bundle v a
b = Bundle v a
b Bundle v a -> Bundle v b -> Bundle v b
forall a b. a -> b -> b
`seq` Stream Id b -> Size -> Bundle v b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
M.fromStream (Stream Id a -> Stream Id b
forall (m :: * -> *). Monad m => Stream m a -> Stream m b
f (Bundle v a -> Stream Id a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
M.elements Bundle v a
b)) (Size -> Size
g (Bundle v a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
M.size Bundle v a
b))

{-# RULES

"inplace/inplace [Vector]"
  forall (f1 :: forall m. Monad m => S.Stream m a -> S.Stream m a)
         (f2 :: forall m. Monad m => S.Stream m a -> S.Stream m a)
         g1 g2 s.
  inplace f1 g1 (inplace f2 g2 s) = inplace (f1 . f2) (g1 . g2) s   #-}


-- | 'Size' hint of a 'Bundle'
size :: Bundle v a -> Size
{-# INLINE size #-}
size :: forall (v :: * -> *) a. Bundle v a -> Size
size = Bundle Id v a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
M.size

-- | Attach a 'Size' hint to a 'Bundle'
sized :: Bundle v a -> Size -> Bundle v a
{-# INLINE sized #-}
sized :: forall (v :: * -> *) a. Bundle v a -> Size -> Bundle v a
sized = Bundle Id v a -> Size -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Size -> Bundle m v a
M.sized

-- Length
-- ------

-- | Length of a 'Bundle'
length :: Bundle v a -> Int
{-# INLINE length #-}
length :: forall (v :: * -> *) a. Bundle v a -> Int
length = Id Int -> Int
forall a. Id a -> a
unId (Id Int -> Int) -> (Bundle v a -> Id Int) -> Bundle v a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Id Int
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m Int
M.length

-- | Check if a 'Bundle' is empty
null :: Bundle v a -> Bool
{-# INLINE null #-}
null :: forall (v :: * -> *) a. Bundle v a -> Bool
null = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool) -> (Bundle v a -> Id Bool) -> Bundle v a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Id Bool
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m Bool
M.null

-- Construction
-- ------------

-- | Empty 'Bundle'
empty :: Bundle v a
{-# INLINE empty #-}
empty :: forall (v :: * -> *) a. Bundle v a
empty = Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a. Monad m => Bundle m v a
M.empty

-- | Singleton 'Bundle'
singleton :: a -> Bundle v a
{-# INLINE singleton #-}
singleton :: forall a (v :: * -> *). a -> Bundle v a
singleton = a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *). Monad m => a -> Bundle m v a
M.singleton

-- | Replicate a value to a given length
replicate :: Int -> a -> Bundle v a
{-# INLINE replicate #-}
replicate :: forall a (v :: * -> *). Int -> a -> Bundle v a
replicate = Int -> a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> a -> Bundle m v a
M.replicate

-- | Generate a stream from its indices
generate :: Int -> (Int -> a) -> Bundle v a
{-# INLINE generate #-}
generate :: forall a (v :: * -> *). Int -> (Int -> a) -> Bundle v a
generate = Int -> (Int -> a) -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> (Int -> a) -> Bundle m v a
M.generate

-- | Prepend an element
cons :: a -> Bundle v a -> Bundle v a
{-# INLINE cons #-}
cons :: forall a (v :: * -> *). a -> Bundle v a -> Bundle v a
cons = a -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
a -> Bundle m v a -> Bundle m v a
M.cons

-- | Append an element
snoc :: Bundle v a -> a -> Bundle v a
{-# INLINE snoc #-}
snoc :: forall (v :: * -> *) a. Bundle v a -> a -> Bundle v a
snoc = Bundle Id v a -> a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> a -> Bundle m v a
M.snoc

infixr 5 ++
-- | Concatenate two 'Bundle's
(++) :: Bundle v a -> Bundle v a -> Bundle v a
{-# INLINE (++) #-}
++ :: forall (v :: * -> *) a. Bundle v a -> Bundle v a -> Bundle v a
(++) = Bundle Id v a -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a -> Bundle m v a
(M.++)

-- Accessing elements
-- ------------------

-- | First element of the 'Bundle' or error if empty
head :: Bundle v a -> a
{-# INLINE head #-}
head :: forall (v :: * -> *) a. Bundle v a -> a
head = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v a -> Id a) -> Bundle v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Id a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m a
M.head

-- | Last element of the 'Bundle' or error if empty
last :: Bundle v a -> a
{-# INLINE last #-}
last :: forall (v :: * -> *) a. Bundle v a -> a
last = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v a -> Id a) -> Bundle v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Id a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m a
M.last

infixl 9 !!
-- | Element at the given position
(!!) :: Bundle v a -> Int -> a
{-# INLINE (!!) #-}
Bundle v a
s !! :: forall (v :: * -> *) a. Bundle v a -> Int -> a
!! Int
i = Id a -> a
forall a. Id a -> a
unId (Bundle v a
s Bundle v a -> Int -> Id a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Int -> m a
M.!! Int
i)

infixl 9 !?
-- | Element at the given position or 'Nothing' if out of bounds
(!?) :: Bundle v a -> Int -> Maybe a
{-# INLINE (!?) #-}
Bundle v a
s !? :: forall (v :: * -> *) a. Bundle v a -> Int -> Maybe a
!? Int
i = Id (Maybe a) -> Maybe a
forall a. Id a -> a
unId (Bundle v a
s Bundle v a -> Int -> Id (Maybe a)
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Int -> m (Maybe a)
M.!? Int
i)

-- Substreams
-- ----------

-- | Extract a substream of the given length starting at the given position.
slice :: Int   -- ^ starting index
      -> Int   -- ^ length
      -> Bundle v a
      -> Bundle v a
{-# INLINE slice #-}
slice :: forall (v :: * -> *) a. Int -> Int -> Bundle v a -> Bundle v a
slice = Int -> Int -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Int -> Bundle m v a -> Bundle m v a
M.slice

-- | All but the last element
init :: Bundle v a -> Bundle v a
{-# INLINE init #-}
init :: forall (v :: * -> *) a. Bundle v a -> Bundle v a
init = Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a
M.init

-- | All but the first element
tail :: Bundle v a -> Bundle v a
{-# INLINE tail #-}
tail :: forall (v :: * -> *) a. Bundle v a -> Bundle v a
tail = Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a
M.tail

-- | The first @n@ elements
take :: Int -> Bundle v a -> Bundle v a
{-# INLINE take #-}
take :: forall (v :: * -> *) a. Int -> Bundle v a -> Bundle v a
take = Int -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v a
M.take

-- | All but the first @n@ elements
drop :: Int -> Bundle v a -> Bundle v a
{-# INLINE drop #-}
drop :: forall (v :: * -> *) a. Int -> Bundle v a -> Bundle v a
drop = Int -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v a
M.drop

-- Mapping
-- ---------------

-- | Map a function over a 'Bundle'
map :: (a -> b) -> Bundle v a -> Bundle v b
{-# INLINE map #-}
map :: forall a b (v :: * -> *). (a -> b) -> Bundle v a -> Bundle v b
map = (a -> b) -> Bundle Id v a -> Bundle Id v b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b) -> Bundle m v a -> Bundle m v b
M.map

unbox :: Bundle v (Box a) -> Bundle v a
{-# INLINE unbox #-}
unbox :: forall (v :: * -> *) a. Bundle v (Box a) -> Bundle v a
unbox = Bundle Id v (Box a) -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v (Box a) -> Bundle m v a
M.unbox

concatMap :: (a -> Bundle v b) -> Bundle v a -> Bundle v b
{-# INLINE concatMap #-}
concatMap :: forall a (v :: * -> *) b.
(a -> Bundle v b) -> Bundle v a -> Bundle v b
concatMap = (a -> Bundle Id v b) -> Bundle Id v a -> Bundle Id v b
forall (m :: * -> *) a (v :: * -> *) b.
Monad m =>
(a -> Bundle m v b) -> Bundle m v a -> Bundle m v b
M.concatMap

-- Zipping
-- -------

-- | Pair each element in a 'Bundle' with its index
indexed :: Bundle v a -> Bundle v (Int,a)
{-# INLINE indexed #-}
indexed :: forall (v :: * -> *) a. Bundle v a -> Bundle v (Int, a)
indexed = Bundle Id v a -> Bundle Id v (Int, a)
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v (Int, a)
M.indexed

-- | Pair each element in a 'Bundle' with its index, starting from the right
-- and counting down
indexedR :: Int -> Bundle v a -> Bundle v (Int,a)
{-# INLINE_FUSED indexedR #-}
indexedR :: forall (v :: * -> *) a. Int -> Bundle v a -> Bundle v (Int, a)
indexedR = Int -> Bundle Id v a -> Bundle Id v (Int, a)
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v (Int, a)
M.indexedR

-- | Zip two 'Bundle's with the given function
zipWith :: (a -> b -> c) -> Bundle v a -> Bundle v b -> Bundle v c
{-# INLINE zipWith #-}
zipWith :: forall a b c (v :: * -> *).
(a -> b -> c) -> Bundle v a -> Bundle v b -> Bundle v c
zipWith = (a -> b -> c) -> Bundle Id v a -> Bundle Id v b -> Bundle Id v c
forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
M.zipWith

-- | Zip three 'Bundle's with the given function
zipWith3 :: (a -> b -> c -> d) -> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
{-# INLINE zipWith3 #-}
zipWith3 :: forall a b c d (v :: * -> *).
(a -> b -> c -> d)
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
zipWith3 = (a -> b -> c -> d)
-> Bundle Id v a -> Bundle Id v b -> Bundle Id v c -> Bundle Id v d
forall (m :: * -> *) a b c d (v :: * -> *).
Monad m =>
(a -> b -> c -> d)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
M.zipWith3

zipWith4 :: (a -> b -> c -> d -> e)
                    -> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                    -> Bundle v e
{-# INLINE zipWith4 #-}
zipWith4 :: forall a b c d e (v :: * -> *).
(a -> b -> c -> d -> e)
-> Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
zipWith4 = (a -> b -> c -> d -> e)
-> Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v e
forall (m :: * -> *) a b c d e (v :: * -> *).
Monad m =>
(a -> b -> c -> d -> e)
-> Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v e
M.zipWith4

zipWith5 :: (a -> b -> c -> d -> e -> f)
                    -> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                    -> Bundle v e -> Bundle v f
{-# INLINE zipWith5 #-}
zipWith5 :: forall a b c d e f (v :: * -> *).
(a -> b -> c -> d -> e -> f)
-> Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v f
zipWith5 = (a -> b -> c -> d -> e -> f)
-> Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v e
-> Bundle Id v f
forall (m :: * -> *) a b c d e f (v :: * -> *).
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v e
-> Bundle m v f
M.zipWith5

zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
                    -> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                    -> Bundle v e -> Bundle v f -> Bundle v g
{-# INLINE zipWith6 #-}
zipWith6 :: forall a b c d e f g (v :: * -> *).
(a -> b -> c -> d -> e -> f -> g)
-> Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v f
-> Bundle v g
zipWith6 = (a -> b -> c -> d -> e -> f -> g)
-> Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v e
-> Bundle Id v f
-> Bundle Id v g
forall (m :: * -> *) a b c d e f g (v :: * -> *).
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v e
-> Bundle m v f
-> Bundle m v g
M.zipWith6

zip :: Bundle v a -> Bundle v b -> Bundle v (a,b)
{-# INLINE zip #-}
zip :: forall (v :: * -> *) a b.
Bundle v a -> Bundle v b -> Bundle v (a, b)
zip = Bundle Id v a -> Bundle Id v b -> Bundle Id v (a, b)
forall (m :: * -> *) (v :: * -> *) a b.
Monad m =>
Bundle m v a -> Bundle m v b -> Bundle m v (a, b)
M.zip

zip3 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v (a,b,c)
{-# INLINE zip3 #-}
zip3 :: forall (v :: * -> *) a b c.
Bundle v a -> Bundle v b -> Bundle v c -> Bundle v (a, b, c)
zip3 = Bundle Id v a
-> Bundle Id v b -> Bundle Id v c -> Bundle Id v (a, b, c)
forall (m :: * -> *) (v :: * -> *) a b c.
Monad m =>
Bundle m v a
-> Bundle m v b -> Bundle m v c -> Bundle m v (a, b, c)
M.zip3

zip4 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                -> Bundle v (a,b,c,d)
{-# INLINE zip4 #-}
zip4 :: forall (v :: * -> *) a b c d.
Bundle v a
-> Bundle v b -> Bundle v c -> Bundle v d -> Bundle v (a, b, c, d)
zip4 = Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v (a, b, c, d)
forall (m :: * -> *) (v :: * -> *) a b c d.
Monad m =>
Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v (a, b, c, d)
M.zip4

zip5 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                -> Bundle v e -> Bundle v (a,b,c,d,e)
{-# INLINE zip5 #-}
zip5 :: forall (v :: * -> *) a b c d e.
Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v (a, b, c, d, e)
zip5 = Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v e
-> Bundle Id v (a, b, c, d, e)
forall (m :: * -> *) (v :: * -> *) a b c d e.
Monad m =>
Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v e
-> Bundle m v (a, b, c, d, e)
M.zip5

zip6 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
                -> Bundle v e -> Bundle v f -> Bundle v (a,b,c,d,e,f)
{-# INLINE zip6 #-}
zip6 :: forall (v :: * -> *) a b c d e f.
Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v f
-> Bundle v (a, b, c, d, e, f)
zip6 = Bundle Id v a
-> Bundle Id v b
-> Bundle Id v c
-> Bundle Id v d
-> Bundle Id v e
-> Bundle Id v f
-> Bundle Id v (a, b, c, d, e, f)
forall (m :: * -> *) (v :: * -> *) a b c d e f.
Monad m =>
Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v e
-> Bundle m v f
-> Bundle m v (a, b, c, d, e, f)
M.zip6

-- Filtering
-- ---------

-- | Drop elements which do not satisfy the predicate
filter :: (a -> Bool) -> Bundle v a -> Bundle v a
{-# INLINE filter #-}
filter :: forall a (v :: * -> *). (a -> Bool) -> Bundle v a -> Bundle v a
filter = (a -> Bool) -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> Bool) -> Bundle m v a -> Bundle m v a
M.filter

-- | Longest prefix of elements that satisfy the predicate
takeWhile :: (a -> Bool) -> Bundle v a -> Bundle v a
{-# INLINE takeWhile #-}
takeWhile :: forall a (v :: * -> *). (a -> Bool) -> Bundle v a -> Bundle v a
takeWhile = (a -> Bool) -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> Bool) -> Bundle m v a -> Bundle m v a
M.takeWhile

-- | Drop the longest prefix of elements that satisfy the predicate
dropWhile :: (a -> Bool) -> Bundle v a -> Bundle v a
{-# INLINE dropWhile #-}
dropWhile :: forall a (v :: * -> *). (a -> Bool) -> Bundle v a -> Bundle v a
dropWhile = (a -> Bool) -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> Bool) -> Bundle m v a -> Bundle m v a
M.dropWhile

-- Searching
-- ---------

infix 4 `elem`
-- | Check whether the 'Bundle' contains an element
elem :: Eq a => a -> Bundle v a -> Bool
{-# INLINE elem #-}
elem :: forall a (v :: * -> *). Eq a => a -> Bundle v a -> Bool
elem a
x = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool) -> (Bundle v a -> Id Bool) -> Bundle v a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bundle v a -> Id Bool
forall (m :: * -> *) a (v :: * -> *).
(Monad m, Eq a) =>
a -> Bundle m v a -> m Bool
M.elem a
x

infix 4 `notElem`
-- | Inverse of `elem`
notElem :: Eq a => a -> Bundle v a -> Bool
{-# INLINE notElem #-}
notElem :: forall a (v :: * -> *). Eq a => a -> Bundle v a -> Bool
notElem a
x = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool) -> (Bundle v a -> Id Bool) -> Bundle v a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bundle v a -> Id Bool
forall (m :: * -> *) a (v :: * -> *).
(Monad m, Eq a) =>
a -> Bundle m v a -> m Bool
M.notElem a
x

-- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
-- such element exists.
find :: (a -> Bool) -> Bundle v a -> Maybe a
{-# INLINE find #-}
find :: forall a (v :: * -> *). (a -> Bool) -> Bundle v a -> Maybe a
find a -> Bool
f = Id (Maybe a) -> Maybe a
forall a. Id a -> a
unId (Id (Maybe a) -> Maybe a)
-> (Bundle v a -> Id (Maybe a)) -> Bundle v a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Bundle v a -> Id (Maybe a)
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> Bool) -> Bundle m v a -> m (Maybe a)
M.find a -> Bool
f

-- | Yield 'Just' the index of the first element matching the predicate or
-- 'Nothing' if no such element exists.
findIndex :: (a -> Bool) -> Bundle v a -> Maybe Int
{-# INLINE findIndex #-}
findIndex :: forall a (v :: * -> *). (a -> Bool) -> Bundle v a -> Maybe Int
findIndex a -> Bool
f = Id (Maybe Int) -> Maybe Int
forall a. Id a -> a
unId (Id (Maybe Int) -> Maybe Int)
-> (Bundle v a -> Id (Maybe Int)) -> Bundle v a -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> Bundle v a -> Id (Maybe Int)
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> Bool) -> Bundle m v a -> m (Maybe Int)
M.findIndex a -> Bool
f

-- Folding
-- -------

-- | Left fold
foldl :: (a -> b -> a) -> a -> Bundle v b -> a
{-# INLINE foldl #-}
foldl :: forall a b (v :: * -> *). (a -> b -> a) -> a -> Bundle v b -> a
foldl a -> b -> a
f a
z = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v b -> Id a) -> Bundle v b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> a) -> a -> Bundle v b -> Id a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> m a
M.foldl a -> b -> a
f a
z

-- | Left fold on non-empty 'Bundle's
foldl1 :: (a -> a -> a) -> Bundle v a -> a
{-# INLINE foldl1 #-}
foldl1 :: forall a (v :: * -> *). (a -> a -> a) -> Bundle v a -> a
foldl1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v a -> Id a) -> Bundle v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Bundle v a -> Id a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> m a
M.foldl1 a -> a -> a
f

-- | Left fold with strict accumulator
foldl' :: (a -> b -> a) -> a -> Bundle v b -> a
{-# INLINE foldl' #-}
foldl' :: forall a b (v :: * -> *). (a -> b -> a) -> a -> Bundle v b -> a
foldl' a -> b -> a
f a
z = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v b -> Id a) -> Bundle v b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> a) -> a -> Bundle v b -> Id a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> m a
M.foldl' a -> b -> a
f a
z

-- | Left fold on non-empty 'Bundle's with strict accumulator
foldl1' :: (a -> a -> a) -> Bundle v a -> a
{-# INLINE foldl1' #-}
foldl1' :: forall a (v :: * -> *). (a -> a -> a) -> Bundle v a -> a
foldl1' a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v a -> Id a) -> Bundle v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Bundle v a -> Id a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> m a
M.foldl1' a -> a -> a
f

-- | Right fold
foldr :: (a -> b -> b) -> b -> Bundle v a -> b
{-# INLINE foldr #-}
foldr :: forall a b (v :: * -> *). (a -> b -> b) -> b -> Bundle v a -> b
foldr a -> b -> b
f b
z = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Bundle v a -> Id b) -> Bundle v a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> b -> Bundle v a -> Id b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> b) -> b -> Bundle m v a -> m b
M.foldr a -> b -> b
f b
z

-- | Right fold on non-empty 'Bundle's
foldr1 :: (a -> a -> a) -> Bundle v a -> a
{-# INLINE foldr1 #-}
foldr1 :: forall a (v :: * -> *). (a -> a -> a) -> Bundle v a -> a
foldr1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Bundle v a -> Id a) -> Bundle v a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Bundle v a -> Id a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> m a
M.foldr1 a -> a -> a
f

-- Specialised folds
-- -----------------

and :: Bundle v Bool -> Bool
{-# INLINE and #-}
and :: forall (v :: * -> *). Bundle v Bool -> Bool
and = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool)
-> (Bundle v Bool -> Id Bool) -> Bundle v Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v Bool -> Id Bool
forall (m :: * -> *) (v :: * -> *).
Monad m =>
Bundle m v Bool -> m Bool
M.and

or :: Bundle v Bool -> Bool
{-# INLINE or #-}
or :: forall (v :: * -> *). Bundle v Bool -> Bool
or = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool)
-> (Bundle v Bool -> Id Bool) -> Bundle v Bool -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v Bool -> Id Bool
forall (m :: * -> *) (v :: * -> *).
Monad m =>
Bundle m v Bool -> m Bool
M.or

-- Unfolding
-- ---------

-- | Unfold
unfoldr :: (s -> Maybe (a, s)) -> s -> Bundle v a
{-# INLINE unfoldr #-}
unfoldr :: forall s a (v :: * -> *). (s -> Maybe (a, s)) -> s -> Bundle v a
unfoldr = (s -> Maybe (a, s)) -> s -> Bundle Id v a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
(s -> Maybe (a, s)) -> s -> Bundle m u a
M.unfoldr

-- | Unfold at most @n@ elements
unfoldrN :: Int -> (s -> Maybe (a, s)) -> s -> Bundle v a
{-# INLINE unfoldrN #-}
unfoldrN :: forall s a (v :: * -> *).
Int -> (s -> Maybe (a, s)) -> s -> Bundle v a
unfoldrN = Int -> (s -> Maybe (a, s)) -> s -> Bundle Id v a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Bundle m u a
M.unfoldrN

-- | Unfold exactly @n@ elements
--
-- @since 0.12.2.0
unfoldrExactN :: Int -> (s -> (a, s)) -> s -> Bundle v a
{-# INLINE unfoldrExactN #-}
unfoldrExactN :: forall s a (v :: * -> *). Int -> (s -> (a, s)) -> s -> Bundle v a
unfoldrExactN = Int -> (s -> (a, s)) -> s -> Bundle Id v a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> (a, s)) -> s -> Bundle m u a
M.unfoldrExactN

-- | /O(n)/ Apply function \(\max(n - 1, 0)\) times to an initial value, producing a pure
-- bundle of exact length \(\max(n, 0)\). Zeroth element will contain the initial value.
iterateN :: Int -> (a -> a) -> a -> Bundle v a
{-# INLINE iterateN #-}
iterateN :: forall a (v :: * -> *). Int -> (a -> a) -> a -> Bundle v a
iterateN = Int -> (a -> a) -> a -> Bundle Id v a
forall (m :: * -> *) a (u :: * -> *).
Monad m =>
Int -> (a -> a) -> a -> Bundle m u a
M.iterateN

-- Scans
-- -----

-- | Prefix scan
prescanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE prescanl #-}
prescanl :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
prescanl = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.prescanl

-- | Prefix scan with strict accumulator
prescanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE prescanl' #-}
prescanl' :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
prescanl' = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.prescanl'

-- | Suffix scan
postscanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE postscanl #-}
postscanl :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
postscanl = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.postscanl

-- | Suffix scan with strict accumulator
postscanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE postscanl' #-}
postscanl' :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
postscanl' = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.postscanl'

-- | Haskell-style scan
scanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE scanl #-}
scanl :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
scanl = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.scanl

-- | Haskell-style scan with strict accumulator
scanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
{-# INLINE scanl' #-}
scanl' :: forall a b (v :: * -> *).
(a -> b -> a) -> a -> Bundle v b -> Bundle v a
scanl' = (a -> b -> a) -> a -> Bundle Id v b -> Bundle Id v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.scanl'

-- | Initial-value free scan over a 'Bundle'
scanl1 :: (a -> a -> a) -> Bundle v a -> Bundle v a
{-# INLINE scanl1 #-}
scanl1 :: forall a (v :: * -> *). (a -> a -> a) -> Bundle v a -> Bundle v a
scanl1 = (a -> a -> a) -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> Bundle m v a
M.scanl1

-- | Initial-value free scan over a 'Bundle' with a strict accumulator
scanl1' :: (a -> a -> a) -> Bundle v a -> Bundle v a
{-# INLINE scanl1' #-}
scanl1' :: forall a (v :: * -> *). (a -> a -> a) -> Bundle v a -> Bundle v a
scanl1' = (a -> a -> a) -> Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> Bundle m v a
M.scanl1'


-- Comparisons
-- -----------

-- | Check if two 'Bundle's are equal
eq :: (Eq a) => Bundle v a -> Bundle v a -> Bool
{-# INLINE eq #-}
eq :: forall a (v :: * -> *). Eq a => Bundle v a -> Bundle v a -> Bool
eq = (a -> a -> Bool) -> Bundle v a -> Bundle v a -> Bool
forall a b (v :: * -> *).
(a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
eqBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)

eqBy :: (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
{-# INLINE eqBy #-}
eqBy :: forall a b (v :: * -> *).
(a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
eqBy a -> b -> Bool
e Bundle v a
x Bundle v b
y = Id Bool -> Bool
forall a. Id a -> a
unId ((a -> b -> Bool) -> Bundle v a -> Bundle v b -> Id Bool
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> Bool) -> Bundle m v a -> Bundle m v b -> m Bool
M.eqBy a -> b -> Bool
e Bundle v a
x Bundle v b
y)

-- | Lexicographically compare two 'Bundle's
cmp :: (Ord a) => Bundle v a -> Bundle v a -> Ordering
{-# INLINE cmp #-}
cmp :: forall a (v :: * -> *).
Ord a =>
Bundle v a -> Bundle v a -> Ordering
cmp = (a -> a -> Ordering) -> Bundle v a -> Bundle v a -> Ordering
forall a b (v :: * -> *).
(a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
cmpBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare

cmpBy :: (a ->  b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
{-# INLINE cmpBy #-}
cmpBy :: forall a b (v :: * -> *).
(a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
cmpBy a -> b -> Ordering
c Bundle v a
x Bundle v b
y = Id Ordering -> Ordering
forall a. Id a -> a
unId ((a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Id Ordering
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> Ordering) -> Bundle m v a -> Bundle m v b -> m Ordering
M.cmpBy a -> b -> Ordering
c Bundle v a
x Bundle v b
y)

instance Eq a => Eq (M.Bundle Id v a) where
  {-# INLINE (==) #-}
  == :: Bundle Id v a -> Bundle Id v a -> Bool
(==) = Bundle Id v a -> Bundle Id v a -> Bool
forall a (v :: * -> *). Eq a => Bundle v a -> Bundle v a -> Bool
eq

instance Ord a => Ord (M.Bundle Id v a) where
  {-# INLINE compare #-}
  compare :: Bundle Id v a -> Bundle Id v a -> Ordering
compare = Bundle Id v a -> Bundle Id v a -> Ordering
forall a (v :: * -> *).
Ord a =>
Bundle v a -> Bundle v a -> Ordering
cmp

instance Eq1 (M.Bundle Id v) where
  {-# INLINE liftEq #-}
  liftEq :: forall a b.
(a -> b -> Bool) -> Bundle Id v a -> Bundle Id v b -> Bool
liftEq = (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
forall a b (v :: * -> *).
(a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
eqBy

instance Ord1 (M.Bundle Id v) where
  {-# INLINE liftCompare #-}
  liftCompare :: forall a b.
(a -> b -> Ordering) -> Bundle Id v a -> Bundle Id v b -> Ordering
liftCompare = (a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
forall a b (v :: * -> *).
(a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
cmpBy

-- Monadic combinators
-- -------------------

-- | Apply a monadic action to each element of the stream, producing a monadic
-- stream of results
mapM :: Monad m => (a -> m b) -> Bundle v a -> M.Bundle m v b
{-# INLINE mapM #-}
mapM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> Bundle m v b
mapM a -> m b
f = (a -> m b) -> Bundle m v a -> Bundle m v b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle m v a -> Bundle m v b
M.mapM a -> m b
f (Bundle m v a -> Bundle m v b)
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> Bundle m v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | Apply a monadic action to each element of the stream
mapM_ :: Monad m => (a -> m b) -> Bundle v a -> m ()
{-# INLINE mapM_ #-}
mapM_ :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> m ()
mapM_ a -> m b
f = (a -> m b) -> Bundle m v a -> m ()
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle m v a -> m ()
M.mapM_ a -> m b
f (Bundle m v a -> m ())
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

zipWithM :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> M.Bundle m v c
{-# INLINE zipWithM #-}
zipWithM :: forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle v a -> Bundle v b -> Bundle m v c
zipWithM a -> b -> m c
f Bundle v a
as Bundle v b
bs = (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
M.zipWithM a -> b -> m c
f (Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift Bundle v a
as) (Bundle v b -> Bundle m v b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift Bundle v b
bs)

zipWithM_ :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> m ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle v a -> Bundle v b -> m ()
zipWithM_ a -> b -> m c
f Bundle v a
as Bundle v b
bs = (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> m ()
forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle m v a -> Bundle m v b -> m ()
M.zipWithM_ a -> b -> m c
f (Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift Bundle v a
as) (Bundle v b -> Bundle m v b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift Bundle v b
bs)

-- | Yield a monadic stream of elements that satisfy the monadic predicate
filterM :: Monad m => (a -> m Bool) -> Bundle v a -> M.Bundle m v a
{-# INLINE filterM #-}
filterM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle v a -> Bundle m v a
filterM a -> m Bool
f = (a -> m Bool) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
M.filterM a -> m Bool
f (Bundle m v a -> Bundle m v a)
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> Bundle m v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | /O(n)/ Apply monadic function to each element of a bundle and
-- discard elements returning Nothing.
--
-- @since 0.12.2.0
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Bundle v a -> M.Bundle m v b
{-# INLINE mapMaybeM #-}
mapMaybeM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m (Maybe b)) -> Bundle v a -> Bundle m v b
mapMaybeM a -> m (Maybe b)
f = (a -> m (Maybe b)) -> Bundle m v a -> Bundle m v b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m (Maybe b)) -> Bundle m v a -> Bundle m v b
M.mapMaybeM a -> m (Maybe b)
f (Bundle m v a -> Bundle m v b)
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> Bundle m v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | Monadic fold
foldM :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a
{-# INLINE foldM #-}
foldM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
foldM a -> b -> m a
m a
z = (a -> b -> m a) -> a -> Bundle m v b -> m a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
M.foldM a -> b -> m a
m a
z (Bundle m v b -> m a)
-> (Bundle v b -> Bundle m v b) -> Bundle v b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v b -> Bundle m v b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | Monadic fold over non-empty stream
fold1M :: Monad m => (a -> a -> m a) -> Bundle v a -> m a
{-# INLINE fold1M #-}
fold1M :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle v a -> m a
fold1M a -> a -> m a
m = (a -> a -> m a) -> Bundle m v a -> m a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
M.fold1M a -> a -> m a
m (Bundle m v a -> m a)
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | Monadic fold with strict accumulator
foldM' :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a
{-# INLINE foldM' #-}
foldM' :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
foldM' a -> b -> m a
m a
z = (a -> b -> m a) -> a -> Bundle m v b -> m a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
M.foldM' a -> b -> m a
m a
z (Bundle m v b -> m a)
-> (Bundle v b -> Bundle m v b) -> Bundle v b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v b -> Bundle m v b
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- | Monad fold over non-empty stream with strict accumulator
fold1M' :: Monad m => (a -> a -> m a) -> Bundle v a -> m a
{-# INLINE fold1M' #-}
fold1M' :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle v a -> m a
fold1M' a -> a -> m a
m = (a -> a -> m a) -> Bundle m v a -> m a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
M.fold1M' a -> a -> m a
m (Bundle m v a -> m a)
-> (Bundle v a -> Bundle m v a) -> Bundle v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift

-- Enumerations
-- ------------

-- | Yield a 'Bundle' of the given length containing the values @x@, @x+y@,
-- @x+y+y@ etc.
enumFromStepN :: Num a => a -> a -> Int -> Bundle v a
{-# INLINE enumFromStepN #-}
enumFromStepN :: forall a (v :: * -> *). Num a => a -> a -> Int -> Bundle v a
enumFromStepN = a -> a -> Int -> Bundle Id v a
forall a (m :: * -> *) (v :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Bundle m v a
M.enumFromStepN

-- | Enumerate values
--
-- /WARNING:/ This operations can be very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromTo :: Enum a => a -> a -> Bundle v a
{-# INLINE enumFromTo #-}
enumFromTo :: forall a (v :: * -> *). Enum a => a -> a -> Bundle v a
enumFromTo = a -> a -> Bundle Id v a
forall a (m :: * -> *) (v :: * -> *).
(Enum a, Monad m) =>
a -> a -> Bundle m v a
M.enumFromTo

-- | Enumerate values with a given step.
--
-- /WARNING:/ This operations is very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromThenTo :: Enum a => a -> a -> a -> Bundle v a
{-# INLINE enumFromThenTo #-}
enumFromThenTo :: forall a (v :: * -> *). Enum a => a -> a -> a -> Bundle v a
enumFromThenTo = a -> a -> a -> Bundle Id v a
forall a (m :: * -> *) (v :: * -> *).
(Enum a, Monad m) =>
a -> a -> a -> Bundle m v a
M.enumFromThenTo

-- Conversions
-- -----------

-- | Convert a 'Bundle' to a list
toList :: Bundle v a -> [a]
{-# INLINE toList #-}
-- toList s = unId (M.toList s)
toList :: forall (v :: * -> *) a. Bundle v a -> [a]
toList Bundle v a
s = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
c b
n -> (a -> b -> b) -> b -> Bundle v a -> b
forall a b (v :: * -> *). (a -> b -> b) -> b -> Bundle v a -> b
toListFB a -> b -> b
c b
n Bundle v a
s)

-- This supports foldr/build list fusion that GHC implements
toListFB :: (a -> b -> b) -> b -> Bundle v a -> b
{-# INLINE [0] toListFB #-}
toListFB :: forall a b (v :: * -> *). (a -> b -> b) -> b -> Bundle v a -> b
toListFB a -> b -> b
c b
n M.Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
M.sElems = Stream s -> Id (Step s a)
step s
t} = s -> b
go s
t
  where
    go :: s -> b
go s
s = case Id (Step s a) -> Step s a
forall a. Id a -> a
unId (s -> Id (Step s a)
step s
s) of
             Yield a
x s
s' -> a
x a -> b -> b
`c` s -> b
go s
s'
             Skip    s
s' -> s -> b
go s
s'
             Step s a
Done       -> b
n

-- | Create a 'Bundle' from a list
fromList :: [a] -> Bundle v a
{-# INLINE fromList #-}
fromList :: forall a (v :: * -> *). [a] -> Bundle v a
fromList = [a] -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
[a] -> Bundle m v a
M.fromList

-- | Create a 'Bundle' from the first @n@ elements of a list
--
-- > fromListN n xs = fromList (take n xs)
fromListN :: Int -> [a] -> Bundle v a
{-# INLINE fromListN #-}
fromListN :: forall a (v :: * -> *). Int -> [a] -> Bundle v a
fromListN = Int -> [a] -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> [a] -> Bundle m v a
M.fromListN

unsafeFromList :: Size -> [a] -> Bundle v a
{-# INLINE unsafeFromList #-}
unsafeFromList :: forall a (v :: * -> *). Size -> [a] -> Bundle v a
unsafeFromList = Size -> [a] -> Bundle Id v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Size -> [a] -> Bundle m v a
M.unsafeFromList

fromVector :: Vector v a => v a -> Bundle v a
{-# INLINE fromVector #-}
fromVector :: forall (v :: * -> *) a. Vector v a => v a -> Bundle v a
fromVector = v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
v a -> Bundle m v a
M.fromVector

reVector :: Bundle u a -> Bundle v a
{-# INLINE reVector #-}
reVector :: forall (u :: * -> *) a (v :: * -> *). Bundle u a -> Bundle v a
reVector = Bundle Id u a -> Bundle Id v a
forall (m :: * -> *) (u :: * -> *) a (v :: * -> *).
Monad m =>
Bundle m u a -> Bundle m v a
M.reVector

fromVectors :: Vector v a => [v a] -> Bundle v a
{-# INLINE fromVectors #-}
fromVectors :: forall (v :: * -> *) a. Vector v a => [v a] -> Bundle v a
fromVectors = [v a] -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
[v a] -> Bundle m v a
M.fromVectors

concatVectors :: Vector v a => Bundle u (v a) -> Bundle v a
{-# INLINE concatVectors #-}
concatVectors :: forall (v :: * -> *) a (u :: * -> *).
Vector v a =>
Bundle u (v a) -> Bundle v a
concatVectors = Bundle Id u (v a) -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a (u :: * -> *).
(Monad m, Vector v a) =>
Bundle m u (v a) -> Bundle m v a
M.concatVectors

-- | Create a 'Bundle' of values from a 'Bundle' of streamable things
flatten :: (a -> s) -> (s -> Step s b) -> Size -> Bundle v a -> Bundle v b
{-# INLINE_FUSED flatten #-}
flatten :: forall a s b (v :: * -> *).
(a -> s) -> (s -> Step s b) -> Size -> Bundle v a -> Bundle v b
flatten a -> s
mk s -> Step s b
istep Size
sz = (a -> Id s)
-> (s -> Id (Step s b)) -> Size -> Bundle Id v a -> Bundle Id v b
forall (m :: * -> *) a s b (v :: * -> *).
Monad m =>
(a -> m s)
-> (s -> m (Step s b)) -> Size -> Bundle m v a -> Bundle m v b
M.flatten (s -> Id s
forall a. a -> Id a
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> Id s) -> (a -> s) -> a -> Id s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> s
mk) (Step s b -> Id (Step s b)
forall a. a -> Id a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> Id (Step s b))
-> (s -> Step s b) -> s -> Id (Step s b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Step s b
istep) Size
sz (Bundle Id v a -> Bundle Id v b)
-> (Bundle Id v a -> Bundle Id v a)
-> Bundle Id v a
-> Bundle Id v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle Id v a -> Bundle Id v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift