inline-r-0.10.3: Seamlessly call R from Haskell and vice versa. No FFI required.
Copyright(C) 2013 Amgen Inc.
Safe HaskellNone
LanguageHaskell2010

Data.Vector.SEXP

Description

Vectors that can be passed to and from R with no copying at all. These vectors are an instance of Data.Vector.Storable, where the memory is allocated from the R heap, in such a way that they can be converted to a SEXP through simple pointer arithmetic (see toSEXP) in constant time.

The main difference between Data.Vector.SEXP and Data.Vector.Storable is that the former uses a header-prefixed data layout (the header immediately precedes the payload of the vector). This means that no additional pointer dereferencing is needed to reach the vector data. The trade-off is that most slicing operations are O(N) instead of O(1).

If you make heavy use of slicing, then it's best to convert to a Data.Vector.Storable vector first, using unsafeToStorable.

Note that since unstream relies on slicing operations, it will still be an O(N) operation but it will copy vector data twice (instead of once).

Synopsis

Documentation

data Vector (ty :: SEXPTYPE) a Source #

Immutable vectors. The second type paramater is a phantom parameter reflecting at the type level the tag of the vector when viewed as a SEXP. The tag of the vector and the representation type are related via ElemRep.

Constructors

Vector 

Fields

Instances

Instances details
SVECTOR ty a => IsList (Vector ty a) Source # 
Instance details

Defined in Data.Vector.SEXP

Associated Types

type Item (Vector ty a) #

Methods

fromList :: [Item (Vector ty a)] -> Vector ty a #

fromListN :: Int -> [Item (Vector ty a)] -> Vector ty a #

toList :: Vector ty a -> [Item (Vector ty a)] #

(Eq a, SVECTOR ty a) => Eq (Vector ty a) Source # 
Instance details

Defined in Data.Vector.SEXP

Methods

(==) :: Vector ty a -> Vector ty a -> Bool #

(/=) :: Vector ty a -> Vector ty a -> Bool #

(Show a, SVECTOR ty a) => Show (Vector ty a) Source # 
Instance details

Defined in Data.Vector.SEXP

Methods

showsPrec :: Int -> Vector ty a -> ShowS #

show :: Vector ty a -> String #

showList :: [Vector ty a] -> ShowS #

SVECTOR ty a => Literal (Vector ty a) ty Source # 
Instance details

Defined in Language.R.Literal

Methods

mkSEXPIO :: Vector ty a -> IO (SEXP V ty) Source #

fromSEXP :: SEXP s ty -> Vector ty a Source #

type Item (Vector ty a) Source # 
Instance details

Defined in Data.Vector.SEXP

type Item (Vector ty a) = a

data MVector s ty a Source #

Mutable R vector. Represented in memory with the same header as SEXP nodes. The second type parameter is phantom, reflecting at the type level the tag of the vector when viewed as a SEXP. The tag of the vector and the representation type are related via ElemRep.

Constructors

MVector 

Fields

Instances

Instances details
VECTOR V ty a => Literal (MVector V ty a) ty Source # 
Instance details

Defined in Language.R.Literal

Methods

mkSEXPIO :: MVector V ty a -> IO (SEXP V ty) Source #

fromSEXP :: SEXP s ty -> MVector V ty a Source #

type family ElemRep s (a :: SEXPTYPE) where ... Source #

Function from R types to the types of the representations of each element in the vector.

type VECTOR s ty a = (Storable a, IsVector ty, SingI ty) Source #

Constraint synonym for all operations on vectors.

type SVECTOR ty a = (Storable a, IsVector ty, SingI ty, ElemRep V ty ~ a) Source #

Constraint synonym for all operations on vectors.

fromSEXP :: SVECTOR ty a => SEXP s ty -> Vector ty a Source #

O(n) Create an immutable vector from a SEXP. Because SEXPs are mutable, this function yields an immutable copy of the SEXP.

unsafeFromSEXP :: SVECTOR ty a => SEXP s ty -> Vector ty a Source #

O(1) Unsafe convert a mutable SEXP to an immutable vector without copying. The mutable vector must not be used after this operation, lest one runs the risk of breaking referential transparency.

toSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty Source #

O(n) Yield a (mutable) copy of the vector as a SEXP.

unsafeToSEXP :: SVECTOR ty a => Vector ty a -> SEXP s ty Source #

O(1) Unsafely convert an immutable vector to a (mutable) SEXP without copying. The immutable vector must not be used after this operation.

Accessors

Length information

length :: SVECTOR ty a => Vector ty a -> Int Source #

O(1) Yield the length of the vector.

null :: SVECTOR ty a => Vector ty a -> Bool Source #

O(1) Test whether a vector if empty

Indexing

(!) :: SVECTOR ty a => Vector ty a -> Int -> a Source #

O(1) Indexing

(!?) :: SVECTOR ty a => Vector ty a -> Int -> Maybe a Source #

O(1) Safe indexing

head :: SVECTOR ty a => Vector ty a -> a Source #

O(1) First element

last :: SVECTOR ty a => Vector ty a -> a Source #

O(1) Last element

unsafeIndex :: SVECTOR ty a => Vector ty a -> Int -> a Source #

O(1) Unsafe indexing without bounds checking

unsafeHead :: SVECTOR ty a => Vector ty a -> a Source #

O(1) First element without checking if the vector is empty

unsafeLast :: SVECTOR ty a => Vector ty a -> a Source #

O(1) Last element without checking if the vector is empty

Monadic indexing

indexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a Source #

O(1) Indexing in a monad.

The monad allows operations to be strict in the vector when necessary. Suppose vector copying is implemented like this:

copy mv v = ... write mv i (v ! i) ...

For lazy vectors, v ! i would not be evaluated which means that mv would unnecessarily retain a reference to v in each element written.

With indexM, copying can be implemented like this instead:

copy mv v = ... do
                  x <- indexM v i
                  write mv i x

Here, no references to v are retained because indexing (but not the elements) is evaluated eagerly.

headM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #

O(1) First element of a vector in a monad. See indexM for an explanation of why this is useful.

lastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #

O(1) Last element of a vector in a monad. See indexM for an explanation of why this is useful.

unsafeIndexM :: (SVECTOR ty a, Monad m) => Vector ty a -> Int -> m a Source #

O(1) Indexing in a monad without bounds checks. See indexM for an explanation of why this is useful.

unsafeHeadM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #

O(1) First element in a monad without checking for empty vectors. See indexM for an explanation of why this is useful.

unsafeLastM :: (SVECTOR ty a, Monad m) => Vector ty a -> m a Source #

O(1) Last element in a monad without checking for empty vectors. See indexM for an explanation of why this is useful.

Extracting subvectors (slicing)

slice Source #

Arguments

:: SVECTOR ty a 
=> Int

i starting index

-> Int

n length

-> Vector ty a 
-> Vector ty a 

O(N) Yield a slice of the vector with copying it. The vector must contain at least i+n elements.

init :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(N) Yield all but the last element, this operation will copy an array. The vector may not be empty.

take :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #

O(N) Yield at the first n elements with copying. The vector may contain less than n elements in which case it is returned unchanged.

drop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #

O(N) Yield all but the first n elements with copying. The vector may contain less than n elements in which case an empty vector is returned.

tail :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(N) Copy all but the first element. The vector may not be empty.

splitAt :: SVECTOR ty a => Int -> Vector ty a -> (Vector ty a, Vector ty a) Source #

O(N) Yield the first n elements paired with the remainder with copying.

Note that splitAt n v is equivalent to (take n v, drop n v) but slightly more efficient.

unsafeTail :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(N) Yield all but the first element with copying. The vector may not be empty but this is not checked.

unsafeSlice Source #

Arguments

:: SVECTOR ty a 
=> Int

i starting index

-> Int

n length

-> Vector ty a 
-> Vector ty a 

O(N) Yield a slice of the vector with copying. The vector must contain at least i+n elements but this is not checked.

unsafeDrop :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #

O(N) Yield all but the first n elements with copying. The vector must contain at least n elements but this is not checked.

unsafeTake :: SVECTOR ty a => Int -> Vector ty a -> Vector ty a Source #

O(N) Yield the first n elements with copying. The vector must contain at least n elements but this is not checked.

unsafeInit :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(N) Yield all but the last element with copying. The vector may not be empty but this is not checked.

Construction

Initialisation

empty :: SVECTOR ty a => Vector ty a Source #

O(1) Empty vector

singleton :: SVECTOR ty a => a -> Vector ty a Source #

O(1) Vector with exactly one element

replicate :: SVECTOR ty a => Int -> a -> Vector ty a Source #

O(n) Vector of the given length with the same value in each position

generate :: SVECTOR ty a => Int -> (Int -> a) -> Vector ty a Source #

O(n) Construct a vector of the given length by applying the function to each index

iterateN :: SVECTOR ty a => Int -> (a -> a) -> a -> Vector ty a Source #

O(n) Apply function n times to value. Zeroth element is original value.

Monadic initialisation

replicateM :: (Monad m, SVECTOR ty a) => Int -> m a -> m (Vector ty a) Source #

O(n) Execute the monadic action the given number of times and store the results in a vector.

generateM :: (Monad m, SVECTOR ty a) => Int -> (Int -> m a) -> m (Vector ty a) Source #

O(n) Construct a vector of the given length by applying the monadic action to each index

create :: SVECTOR ty a => (forall r. ST r (MVector r ty a)) -> Vector ty a Source #

Execute the monadic action and freeze the resulting vector.

create (do { v <- new 2; write v 0 'a'; write v 1 'b'; return v }) = <a,b>

Unfolding

unfoldr :: SVECTOR ty a => (b -> Maybe (a, b)) -> b -> Vector ty a Source #

O(n) Construct a Vector ty by repeatedly applying the generator function to a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
 = <10,9,8,7,6,5,4,3,2,1>

unfoldrN :: SVECTOR ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector ty a Source #

O(n) Construct a vector with at most n by repeatedly applying the generator function to the a seed. The generator function yields Just the next element and the new seed or Nothing if there are no more elements.

unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8>

constructN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a Source #

O(n) Construct a vector with n elements by repeatedly applying the generator function to the already constructed part of the vector.

constructN 3 f = let a = f <> ; b = f <a> ; c = f <a,b> in f <a,b,c>

constructrN :: SVECTOR ty a => Int -> (Vector ty a -> a) -> Vector ty a Source #

O(n) Construct a vector with n elements from right to left by repeatedly applying the generator function to the already constructed part of the vector.

constructrN 3 f = let a = f <> ; b = f<a> ; c = f <b,a> in f <c,b,a>

Enumeration

enumFromN :: (SVECTOR ty a, Num a) => a -> Int -> Vector ty a Source #

O(n) Yield a vector of the given length containing the values x, x+1 etc. This operation is usually more efficient than enumFromTo.

enumFromN 5 3 = <5,6,7>

enumFromStepN :: (SVECTOR ty a, Num a) => a -> a -> Int -> Vector ty a Source #

O(n) Yield a vector of the given length containing the values x, x+y, x+y+y etc. This operations is usually more efficient than enumFromThenTo.

enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4>

enumFromTo :: (SVECTOR ty a, Enum a) => a -> a -> Vector ty a Source #

O(n) Enumerate values from x to y.

WARNING: This operation can be very inefficient. If at all possible, use enumFromN instead.

enumFromThenTo :: (SVECTOR ty a, Enum a) => a -> a -> a -> Vector ty a Source #

O(n) Enumerate values from x to y with a specific step z.

WARNING: This operation can be very inefficient. If at all possible, use enumFromStepN instead.

Concatenation

cons :: SVECTOR ty a => a -> Vector ty a -> Vector ty a Source #

O(n) Prepend an element

snoc :: SVECTOR ty a => Vector ty a -> a -> Vector ty a Source #

O(n) Append an element

(++) :: SVECTOR ty a => Vector ty a -> Vector ty a -> Vector ty a infixr 5 Source #

O(m+n) Concatenate two vectors

concat :: SVECTOR ty a => [Vector ty a] -> Vector ty a Source #

O(n) Concatenate all vectors in the list

Restricting memory usage

force :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(n) Yield the argument but force it not to retain any extra memory, possibly by copying it.

This is especially useful when dealing with slices. For example:

force (slice 0 2 <huge vector>)

Here, the slice retains a reference to the huge vector. Forcing it creates a copy of just the elements that belong to the slice and allows the huge vector to be garbage collected.

Modifying vectors

Bulk updates

(//) Source #

Arguments

:: SVECTOR ty a 
=> Vector ty a

initial vector (of length m)

-> [(Int, a)]

list of index/value pairs (of length n)

-> Vector ty a 

O(m+n) For each pair (i,a) from the list, replace the vector element at position i by a.

<5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>

unsafeUpd :: SVECTOR ty a => Vector ty a -> [(Int, a)] -> Vector ty a Source #

Same as (//) but without bounds checking.

Accumulations

accum Source #

Arguments

:: SVECTOR ty a 
=> (a -> b -> a)

accumulating function f

-> Vector ty a

initial vector (of length m)

-> [(Int, b)]

list of index/value pairs (of length n)

-> Vector ty a 

O(m+n) For each pair (i,b) from the list, replace the vector element a at position i by f a b.

accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>

unsafeAccum :: SVECTOR ty a => (a -> b -> a) -> Vector ty a -> [(Int, b)] -> Vector ty a Source #

Same as accum but without bounds checking.

Permutations

reverse :: SVECTOR ty a => Vector ty a -> Vector ty a Source #

O(n) Reverse a vector

Safe destructive updates

Elementwise operations

Mapping

map :: (SVECTOR ty a, SVECTOR ty b) => (a -> b) -> Vector ty a -> Vector ty b Source #

O(n) Map a function over a vector

imap :: (SVECTOR ty a, SVECTOR ty b) => (Int -> a -> b) -> Vector ty a -> Vector ty b Source #

O(n) Apply a function to every element of a Vector ty and its index

concatMap :: (SVECTOR tya a, SVECTOR tyb b) => (a -> Vector tyb b) -> Vector tya a -> Vector tyb b Source #

Map a function over a Vector ty and concatenate the results.

Monadic mapping

mapM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => (a -> m b) -> Vector ty a -> m (Vector ty b) Source #

O(n) Apply the monadic action to all elements of the vector, yielding a vector of results

mapM_ :: (Monad m, SVECTOR ty a) => (a -> m b) -> Vector ty a -> m () Source #

O(n) Apply the monadic action to all elements of a Vector ty and ignore the results

forM :: (Monad m, SVECTOR ty a, SVECTOR ty b) => Vector ty a -> (a -> m b) -> m (Vector ty b) Source #

O(n) Apply the monadic action to all elements of the vector, yielding a vector of results. Equvalent to flip mapM.

forM_ :: (Monad m, SVECTOR ty a) => Vector ty a -> (a -> m b) -> m () Source #

O(n) Apply the monadic action to all elements of a Vector ty and ignore the results. Equivalent to flip mapM_.

Zipping

zipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c Source #

O(min(m,n)) Zip two vectors with the given function.

zipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d Source #

Zip three vectors with the given function.

zipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e Source #

zipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f Source #

zipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g Source #

izipWith :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c) => (Int -> a -> b -> c) -> Vector tya a -> Vector tyb b -> Vector tyc c Source #

O(min(m,n)) Zip two vectors with a function that also takes the elements' indices.

izipWith3 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d) => (Int -> a -> b -> c -> d) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d Source #

Zip three vectors and their indices with the given function.

izipWith4 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e) => (Int -> a -> b -> c -> d -> e) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e Source #

izipWith5 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f) => (Int -> a -> b -> c -> d -> e -> f) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f Source #

izipWith6 :: (SVECTOR tya a, SVECTOR tyb b, SVECTOR tyc c, SVECTOR tyd d, SVECTOR tye e, SVECTOR tyf f, SVECTOR tyg g) => (Int -> a -> b -> c -> d -> e -> f -> g) -> Vector tya a -> Vector tyb b -> Vector tyc c -> Vector tyd d -> Vector tye e -> Vector tyf f -> Vector tyg g Source #

Monadic zipping

zipWithM :: (MonadR m, VECTOR (Region m) tya a, VECTOR (Region m) tyb b, VECTOR (Region m) tyc c, ElemRep V tya ~ a, ElemRep V tyb ~ b, ElemRep V tyc ~ c) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m (Vector tyc c) Source #

O(min(m,n)) Zip the two vectors with the monadic action and yield a vector of results

zipWithM_ :: (Monad m, SVECTOR tya a, SVECTOR tyb b) => (a -> b -> m c) -> Vector tya a -> Vector tyb b -> m () Source #

O(min(m,n)) Zip the two vectors with the monadic action and ignore the results

Working with predicates

Filtering

filter :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #

O(n) Drop elements that do not satisfy the predicate

ifilter :: SVECTOR ty a => (Int -> a -> Bool) -> Vector ty a -> Vector ty a Source #

O(n) Drop elements that do not satisfy the predicate which is applied to values and their indices

filterM :: (Monad m, SVECTOR ty a) => (a -> m Bool) -> Vector ty a -> m (Vector ty a) Source #

O(n) Drop elements that do not satisfy the monadic predicate

takeWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #

O(n) Yield the longest prefix of elements satisfying the predicate with copying.

dropWhile :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Vector ty a Source #

O(n) Drop the longest prefix of elements that satisfy the predicate with copying.

Partitioning

partition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #

O(n) Split the vector in two parts, the first one containing those elements that satisfy the predicate and the second one those that don't. The relative order of the elements is preserved at the cost of a sometimes reduced performance compared to unstablePartition.

unstablePartition :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #

O(n) Split the vector in two parts, the first one containing those elements that satisfy the predicate and the second one those that don't. The order of the elements is not preserved but the operation is often faster than partition.

span :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #

O(n) Split the vector into the longest prefix of elements that satisfy the predicate and the rest with copying.

break :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> (Vector ty a, Vector ty a) Source #

O(n) Split the vector into the longest prefix of elements that do not satisfy the predicate and the rest with copying.

Searching

elem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool infix 4 Source #

O(n) Check if the vector contains an element

notElem :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Bool infix 4 Source #

O(n) Check if the vector does not contain an element (inverse of elem)

find :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe a Source #

O(n) Yield Just the first element matching the predicate or Nothing if no such element exists.

findIndex :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Maybe Int Source #

O(n) Yield Just the index of the first element matching the predicate or Nothing if no such element exists.

elemIndex :: (SVECTOR ty a, Eq a) => a -> Vector ty a -> Maybe Int Source #

O(n) Yield Just the index of the first occurence of the given element or Nothing if the vector does not contain the element. This is a specialised version of findIndex.

Folding

foldl :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a Source #

O(n) Left fold

foldl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #

O(n) Left fold on non-empty vectors

foldl' :: SVECTOR ty b => (a -> b -> a) -> a -> Vector ty b -> a Source #

O(n) Left fold with strict accumulator

foldl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #

O(n) Left fold on non-empty vectors with strict accumulator

foldr :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b Source #

O(n) Right fold

foldr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #

O(n) Right fold on non-empty vectors

foldr' :: SVECTOR ty a => (a -> b -> b) -> b -> Vector ty a -> b Source #

O(n) Right fold with a strict accumulator

foldr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> a Source #

O(n) Right fold on non-empty vectors with strict accumulator

ifoldl :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a Source #

O(n) Left fold (function applied to each element and its index)

ifoldl' :: SVECTOR ty b => (a -> Int -> b -> a) -> a -> Vector ty b -> a Source #

O(n) Left fold with strict accumulator (function applied to each element and its index)

ifoldr :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b Source #

O(n) Right fold (function applied to each element and its index)

ifoldr' :: SVECTOR ty a => (Int -> a -> b -> b) -> b -> Vector ty a -> b Source #

O(n) Right fold with strict accumulator (function applied to each element and its index)

Specialised folds

all :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool Source #

O(n) Check if all elements satisfy the predicate.

any :: SVECTOR ty a => (a -> Bool) -> Vector ty a -> Bool Source #

O(n) Check if any element satisfies the predicate.

sum :: (SVECTOR ty a, Num a) => Vector ty a -> a Source #

O(n) Compute the sum of the elements

product :: (SVECTOR ty a, Num a) => Vector ty a -> a Source #

O(n) Compute the produce of the elements

maximum :: (SVECTOR ty a, Ord a) => Vector ty a -> a Source #

O(n) Yield the maximum element of the vector. The vector may not be empty.

maximumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a Source #

O(n) Yield the maximum element of the Vector ty according to the given comparison function. The vector may not be empty.

minimum :: (SVECTOR ty a, Ord a) => Vector ty a -> a Source #

O(n) Yield the minimum element of the vector. The vector may not be empty.

minimumBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> a Source #

O(n) Yield the minimum element of the Vector ty according to the given comparison function. The vector may not be empty.

minIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int Source #

O(n) Yield the index of the minimum element of the vector. The vector may not be empty.

minIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int Source #

O(n) Yield the index of the minimum element of the Vector ty according to the given comparison function. The vector may not be empty.

maxIndex :: (SVECTOR ty a, Ord a) => Vector ty a -> Int Source #

O(n) Yield the index of the maximum element of the vector. The vector may not be empty.

maxIndexBy :: SVECTOR ty a => (a -> a -> Ordering) -> Vector ty a -> Int Source #

O(n) Yield the index of the maximum element of the Vector ty according to the given comparison function. The vector may not be empty.

Monadic folds

foldM :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a Source #

O(n) Monadic fold

foldM' :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m a Source #

O(n) Monadic fold with strict accumulator

fold1M :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a Source #

O(n) Monadic fold over non-empty vectors

fold1M' :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m a Source #

O(n) Monadic fold over non-empty vectors with strict accumulator

foldM_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m () Source #

O(n) Monadic fold that discards the result

foldM'_ :: (Monad m, SVECTOR ty b) => (a -> b -> m a) -> a -> Vector ty b -> m () Source #

O(n) Monadic fold with strict accumulator that discards the result

fold1M_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m () Source #

O(n) Monadic fold over non-empty vectors that discards the result

fold1M'_ :: (Monad m, SVECTOR ty a) => (a -> a -> m a) -> Vector ty a -> m () Source #

O(n) Monadic fold over non-empty vectors with strict accumulator that discards the result

Prefix sums (scans)

prescanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Prescan

prescanl f z = init . scanl f z

Example: prescanl (+) 0 <1,2,3,4> = <0,1,3,6>

prescanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Prescan with strict accumulator

postscanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Scan

postscanl f z = tail . scanl f z

Example: postscanl (+) 0 <1,2,3,4> = <1,3,6,10>

postscanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Scan with strict accumulator

scanl :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Haskell-style scan

scanl f z <x1,...,xn> = <y1,...,y(n+1)>
  where y1 = z
        yi = f y(i-1) x(i-1)

Example: scanl (+) 0 <1,2,3,4> = <0,1,3,6,10>

scanl' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> a) -> a -> Vector ty b -> Vector ty a Source #

O(n) Haskell-style scan with strict accumulator

scanl1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #

O(n) Scan over a non-empty vector

scanl f <x1,...,xn> = <y1,...,yn>
  where y1 = x1
        yi = f y(i-1) xi

scanl1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #

O(n) Scan over a non-empty vector with a strict accumulator

prescanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left prescan

prescanr f z = reverse . prescanl (flip f) z . reverse

prescanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left prescan with strict accumulator

postscanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left scan

postscanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left scan with strict accumulator

scanr :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left Haskell-style scan

scanr' :: (SVECTOR ty a, SVECTOR ty b) => (a -> b -> b) -> b -> Vector ty a -> Vector ty b Source #

O(n) Right-to-left Haskell-style scan with strict accumulator

scanr1 :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #

O(n) Right-to-left scan over a non-empty vector

scanr1' :: SVECTOR ty a => (a -> a -> a) -> Vector ty a -> Vector ty a Source #

O(n) Right-to-left scan over a non-empty vector with a strict accumulator

Conversions

Lists

toList :: SVECTOR ty a => Vector ty a -> [a] Source #

O(n) Convert a vector to a list

fromList :: forall ty a. SVECTOR ty a => [a] -> Vector ty a Source #

O(n) Convert a list to a vector

fromListN :: forall ty a. SVECTOR ty a => Int -> [a] -> Vector ty a Source #

O(n) Convert the first n elements of a list to a vector

fromListN n xs = fromList (take n xs)

Mutable vectors

freeze :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a) Source #

O(n) Yield an immutable copy of the mutable vector.

thaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a) Source #

O(n) Yield a mutable copy of the immutable vector.

copy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m () Source #

O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length.

unsafeFreeze :: (VECTOR (Region m) ty a, MonadR m, ElemRep V ty ~ a) => MVector (Region m) ty a -> m (Vector ty a) Source #

O(1) Unsafe convert a mutable vector to an immutable one with copying. The mutable vector may not be used after this operation.

unsafeThaw :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => Vector ty a -> m (MVector (Region m) ty a) Source #

O(1) Unsafely convert an immutable vector to a mutable one with copying. The immutable vector may not be used after this operation.

unsafeCopy :: (MonadR m, VECTOR (Region m) ty a, ElemRep V ty ~ a) => MVector (Region m) ty a -> Vector ty a -> m () Source #

O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length. This is not checked.

SEXP specific helpers.

toString :: Vector 'Char Word8 -> String Source #

O(n) Convert a character vector into a String.

toByteString :: Vector 'Char Word8 -> ByteString Source #

O(n) Convert a character vector into a strict ByteString.

unsafeWithByteString :: NFData a => Vector 'Char Word8 -> (ByteString -> IO a) -> a Source #

This function is unsafe and ByteString should not be used outside of the function. Any change to bytestring will be reflected in the source vector, thus breaking referencial transparancy.