{-# LANGUAGE CPP, FlexibleInstances, Rank2Types, BangPatterns #-}

-- |
-- Module      : Data.Vector.Fusion.Bundle
-- Copyright   : (c) Roman Leshchinskiy 2008-2010
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- 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 hiding ( length, null,
                        replicate, (++),
                        head, last, (!!),
                        init, tail, take, drop,
                        map, concatMap,
                        zipWith, zipWith3, zip, zip3,
                        filter, takeWhile, dropWhile,
                        elem, notElem,
                        foldl, foldl1, foldr, foldr1,
                        and, or,
                        scanl, scanl1,
                        enumFromTo, enumFromThenTo,
                        mapM, mapM_ )

#if MIN_VERSION_base(4,9,0)
import Data.Functor.Classes (Eq1 (..), Ord1 (..))
#endif

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 (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
`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 :: Bundle v a -> Size
size = Bundle 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 :: Bundle v a -> Size -> Bundle v a
sized = Bundle v a -> Size -> Bundle 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 :: 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 :: 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 :: Bundle v a
empty = Bundle v a
forall (m :: * -> *) (v :: * -> *) a. Monad m => Bundle m v a
M.empty

-- | Singleton 'Bundle'
singleton :: a -> Bundle v a
{-# INLINE singleton #-}
singleton :: a -> Bundle v a
singleton = a -> Bundle 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 :: Int -> a -> Bundle v a
replicate = Int -> a -> Bundle 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 :: Int -> (Int -> a) -> Bundle v a
generate = Int -> (Int -> a) -> Bundle 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 :: a -> Bundle v a -> Bundle v a
cons = a -> Bundle v a -> Bundle 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 :: Bundle v a -> a -> Bundle v a
snoc = Bundle v a -> a -> Bundle 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 (++) #-}
++ :: Bundle v a -> Bundle v a -> Bundle v a
(++) = Bundle v a -> Bundle v a -> Bundle 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 :: 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 :: 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 !! :: 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 !? :: 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 :: Int -> Int -> Bundle v a -> Bundle v a
slice = Int -> Int -> Bundle v a -> Bundle 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 :: Bundle v a -> Bundle v a
init = Bundle v a -> Bundle 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 :: Bundle v a -> Bundle v a
tail = Bundle v a -> Bundle 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 :: Int -> Bundle v a -> Bundle v a
take = Int -> Bundle v a -> Bundle 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 :: Int -> Bundle v a -> Bundle v a
drop = Int -> Bundle v a -> Bundle 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 :: (a -> b) -> Bundle v a -> Bundle v b
map = (a -> b) -> Bundle v a -> Bundle 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 :: Bundle v (Box a) -> Bundle v a
unbox = Bundle v (Box a) -> Bundle 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 :: (a -> Bundle v b) -> Bundle v a -> Bundle v b
concatMap = (a -> Bundle v b) -> Bundle v a -> Bundle 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 :: Bundle v a -> Bundle v (Int, a)
indexed = Bundle v a -> Bundle 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 :: Int -> Bundle v a -> Bundle v (Int, a)
indexedR = Int -> Bundle v a -> Bundle 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 :: (a -> b -> c) -> Bundle v a -> Bundle v b -> Bundle v c
zipWith = (a -> b -> c) -> Bundle v a -> Bundle v b -> Bundle 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 :: (a -> b -> c -> d)
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
zipWith3 = (a -> b -> c -> d)
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle 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 :: (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 v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle 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 :: (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 v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle 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 :: (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 v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v f
-> Bundle 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 :: Bundle v a -> Bundle v b -> Bundle v (a, b)
zip = Bundle v a -> Bundle v b -> Bundle 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 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v (a, b, c)
zip3 = Bundle v a -> Bundle v b -> Bundle v c -> Bundle 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 :: Bundle v a
-> Bundle v b -> Bundle v c -> Bundle v d -> Bundle v (a, b, c, d)
zip4 = Bundle v a
-> Bundle v b -> Bundle v c -> Bundle v d -> Bundle 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 :: Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle v (a, b, c, d, e)
zip5 = Bundle v a
-> Bundle v b
-> Bundle v c
-> Bundle v d
-> Bundle v e
-> Bundle 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 :: 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 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)
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 :: (a -> Bool) -> Bundle v a -> Bundle v a
filter = (a -> Bool) -> Bundle v a -> Bundle 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 :: (a -> Bool) -> Bundle v a -> Bundle v a
takeWhile = (a -> Bool) -> Bundle v a -> Bundle 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 :: (a -> Bool) -> Bundle v a -> Bundle v a
dropWhile = (a -> Bool) -> Bundle v a -> Bundle 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 :: 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 :: 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 :: (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 :: (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 :: (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 :: (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' :: (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' :: (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 :: (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 :: (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 :: 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 :: 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 :: (s -> Maybe (a, s)) -> s -> Bundle v a
unfoldr = (s -> Maybe (a, s)) -> s -> Bundle 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 :: Int -> (s -> Maybe (a, s)) -> s -> Bundle v a
unfoldrN = Int -> (s -> Maybe (a, s)) -> s -> Bundle 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 :: Int -> (s -> (a, s)) -> s -> Bundle v a
unfoldrExactN = Int -> (s -> (a, s)) -> s -> Bundle 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 :: Int -> (a -> a) -> a -> Bundle v a
iterateN = Int -> (a -> a) -> a -> Bundle 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 :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
prescanl = (a -> b -> a) -> a -> Bundle v b -> Bundle 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' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
prescanl' = (a -> b -> a) -> a -> Bundle v b -> Bundle 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 :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
postscanl = (a -> b -> a) -> a -> Bundle v b -> Bundle 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' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
postscanl' = (a -> b -> a) -> a -> Bundle v b -> Bundle 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 :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
scanl = (a -> b -> a) -> a -> Bundle v b -> Bundle 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' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
scanl' = (a -> b -> a) -> a -> Bundle v b -> Bundle v a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
M.scanl'

-- | Scan over a non-empty 'Bundle'
scanl1 :: (a -> a -> a) -> Bundle v a -> Bundle v a
{-# INLINE scanl1 #-}
scanl1 :: (a -> a -> a) -> Bundle v a -> Bundle v a
scanl1 = (a -> a -> a) -> Bundle v a -> Bundle v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> Bundle m v a
M.scanl1

-- | Scan over a non-empty 'Bundle' with a strict accumulator
scanl1' :: (a -> a -> a) -> Bundle v a -> Bundle v a
{-# INLINE scanl1' #-}
scanl1' :: (a -> a -> a) -> Bundle v a -> Bundle v a
scanl1' = (a -> a -> a) -> Bundle v a -> Bundle 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 :: 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 :: (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 :: 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 :: (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 Id v a -> Bundle Id 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 Id v a -> Bundle Id v a -> Ordering
cmp

#if MIN_VERSION_base(4,9,0)
instance Eq1 (M.Bundle Id v) where
  {-# INLINE liftEq #-}
  liftEq :: (a -> b -> Bool) -> Bundle Id v a -> Bundle Id v b -> Bool
liftEq = (a -> b -> Bool) -> Bundle Id v a -> Bundle Id 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 :: (a -> b -> Ordering) -> Bundle Id v a -> Bundle Id v b -> Ordering
liftCompare = (a -> b -> Ordering) -> Bundle Id v a -> Bundle Id v b -> Ordering
forall a b (v :: * -> *).
(a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
cmpBy
#endif

-- 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 :: (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_ :: (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 :: (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_ :: (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 :: (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 :: (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 :: (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 :: (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' :: (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' :: (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 :: a -> a -> Int -> Bundle v a
enumFromStepN = a -> a -> Int -> Bundle 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 :: a -> a -> Bundle v a
enumFromTo = a -> a -> Bundle 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 :: a -> a -> a -> Bundle v a
enumFromThenTo = a -> a -> a -> Bundle 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 :: 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 :: (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 :: [a] -> Bundle v a
fromList = [a] -> Bundle 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 :: Int -> [a] -> Bundle v a
fromListN = Int -> [a] -> Bundle 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 :: Size -> [a] -> Bundle v a
unsafeFromList = Size -> [a] -> Bundle 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 :: v a -> Bundle v a
fromVector = v a -> Bundle 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 :: Bundle u a -> Bundle v a
reVector = Bundle u a -> Bundle 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 :: [v a] -> Bundle v a
fromVectors = [v a] -> Bundle 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 :: Bundle u (v a) -> Bundle v a
concatVectors = Bundle u (v a) -> Bundle 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 :: (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 v a -> Bundle 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 (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 (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 v a -> Bundle v b)
-> (Bundle v a -> Bundle v a) -> Bundle v a -> Bundle v b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle v a -> Bundle v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift