inline-r-0.9.1: Seamlessly call R from Haskell and vice versa. No FFI required.

Copyright(C) 2013 Amgen Inc.
Safe HaskellNone
LanguageHaskell2010

Data.Vector.SEXP

Contents

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 s (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

VECTOR s ty a => IsList (Vector s ty a) Source # 

Associated Types

type Item (Vector s ty a) :: * #

Methods

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

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

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

(Eq a, VECTOR s ty a) => Eq (Vector s ty a) Source # 

Methods

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

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

(Show a, VECTOR s ty a) => Show (Vector s ty a) Source # 

Methods

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

show :: Vector s ty a -> String #

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

VECTOR V ty a => Literal (Vector V ty a) ty Source # 

Methods

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

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

type Item (Vector s ty a) Source # 
type Item (Vector s 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

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

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, ElemRep s ty ~ a) Source #

Constraint synonym for all operations on vectors.

fromSEXP :: VECTOR s ty a => SEXP s ty -> Vector s 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 :: VECTOR s ty a => SEXP s ty -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> SEXP s ty Source #

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

unsafeToSEXP :: VECTOR s ty a => Vector s 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 :: VECTOR s ty a => Vector s ty a -> Int Source #

O(1) Yield the length of the vector.

null :: VECTOR s ty a => Vector s ty a -> Bool Source #

O(1) Test whether a vector if empty

Indexing

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

O(1) Indexing

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

O(1) Safe indexing

head :: VECTOR s ty a => Vector s ty a -> a Source #

O(1) First element

last :: VECTOR s ty a => Vector s ty a -> a Source #

O(1) Last element

unsafeIndex :: VECTOR s ty a => Vector s ty a -> Int -> a Source #

O(1) Unsafe indexing without bounds checking

unsafeHead :: VECTOR s ty a => Vector s ty a -> a Source #

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

unsafeLast :: VECTOR s ty a => Vector s ty a -> a Source #

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

Monadic indexing

indexM :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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 :: (VECTOR s ty a, Monad m) => Vector s 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

:: VECTOR s ty a 
=> Int

i starting index

-> Int

n length

-> Vector s ty a 
-> Vector s ty a 

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

init :: VECTOR s ty a => Vector s ty a -> Vector s ty a Source #

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

take :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s ty a Source #

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

splitAt :: VECTOR s ty a => Int -> Vector s ty a -> (Vector s ty a, Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s 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

:: VECTOR s ty a 
=> Int

i starting index

-> Int

n length

-> Vector s ty a 
-> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Int -> Vector s ty a -> Vector s 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 :: VECTOR s ty a => Vector s ty a -> Vector s 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 :: VECTOR s ty a => Vector s ty a Source #

O(1) Empty vector

singleton :: VECTOR s ty a => a -> Vector s ty a Source #

O(1) Vector with exactly one element

replicate :: VECTOR s ty a => Int -> a -> Vector s ty a Source #

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

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

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

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

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

Monadic initialisation

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

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

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

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

create :: VECTOR s ty a => (forall r. ST r (MVector r ty a)) -> Vector s 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 :: VECTOR s ty a => (b -> Maybe (a, b)) -> b -> Vector s ty a Source #

O(n) Construct a Vector s 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 :: VECTOR s ty a => Int -> (b -> Maybe (a, b)) -> b -> Vector s 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 :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s 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 :: VECTOR s ty a => Int -> (Vector s ty a -> a) -> Vector s 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 :: (VECTOR s ty a, Num a) => a -> Int -> Vector s 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 :: (VECTOR s ty a, Num a) => a -> a -> Int -> Vector s 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 :: (VECTOR s ty a, Enum a) => a -> a -> Vector s 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 :: (VECTOR s ty a, Enum a) => a -> a -> a -> Vector s 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 :: VECTOR s ty a => a -> Vector s ty a -> Vector s ty a Source #

O(n) Prepend an element

snoc :: VECTOR s ty a => Vector s ty a -> a -> Vector s ty a Source #

O(n) Append an element

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

O(m+n) Concatenate two vectors

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

O(n) Concatenate all vectors in the list

Restricting memory usage

force :: VECTOR s ty a => Vector s ty a -> Vector s 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

:: VECTOR s ty a 
=> Vector s ty a

initial vector (of length m)

-> [(Int, a)]

list of index/value pairs (of length n)

-> Vector s 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 :: VECTOR s ty a => Vector s ty a -> [(Int, a)] -> Vector s ty a Source #

Same as (//) but without bounds checking.

Accumulations

accum Source #

Arguments

:: VECTOR s ty a 
=> (a -> b -> a)

accumulating function f

-> Vector s ty a

initial vector (of length m)

-> [(Int, b)]

list of index/value pairs (of length n)

-> Vector s 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 :: VECTOR s ty a => (a -> b -> a) -> Vector s ty a -> [(Int, b)] -> Vector s ty a Source #

Same as accum but without bounds checking.

Permutations

reverse :: VECTOR s ty a => Vector s ty a -> Vector s ty a Source #

O(n) Reverse a vector

Safe destructive updates

Elementwise operations

Mapping

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

O(n) Map a function over a vector

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

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

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

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

Monadic mapping

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

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

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

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

forM :: (Monad m, VECTOR s ty a, VECTOR s ty b) => Vector s ty a -> (a -> m b) -> m (Vector s 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, VECTOR s ty a) => Vector s ty a -> (a -> m b) -> m () Source #

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

Zipping

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

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

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

Zip three vectors with the given function.

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

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

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

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

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

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

Zip three vectors and their indices with the given function.

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

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

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

Monadic zipping

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

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

zipWithM_ :: (Monad m, VECTOR s tya a, VECTOR s tyb b) => (a -> b -> m c) -> Vector s tya a -> Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> Vector s ty a Source #

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

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

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

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

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

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

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

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

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

Partitioning

partition :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s ty a) Source #

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

break :: VECTOR s ty a => (a -> Bool) -> Vector s ty a -> (Vector s ty a, Vector s 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 :: (VECTOR s ty a, Eq a) => a -> Vector s ty a -> Bool infix 4 Source #

O(n) Check if the vector contains an element

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

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

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

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

findIndex :: VECTOR s ty a => (a -> Bool) -> Vector s 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 :: (VECTOR s ty a, Eq a) => a -> Vector s 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 :: VECTOR s ty b => (a -> b -> a) -> a -> Vector s ty b -> a Source #

O(n) Left fold

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

O(n) Left fold on non-empty vectors

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

O(n) Left fold with strict accumulator

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

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

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

O(n) Right fold

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

O(n) Right fold on non-empty vectors

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

O(n) Right fold with a strict accumulator

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

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

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

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

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

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

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

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

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

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

Specialised folds

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

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

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

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

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

O(n) Compute the sum of the elements

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

O(n) Compute the produce of the elements

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Monadic folds

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

O(n) Monadic fold

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

O(n) Monadic fold with strict accumulator

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

O(n) Monadic fold over non-empty vectors

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

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

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

O(n) Monadic fold that discards the result

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

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

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

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

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

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

Prefix sums (scans)

prescanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s 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' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #

O(n) Prescan with strict accumulator

postscanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s 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' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #

O(n) Scan with strict accumulator

scanl :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s 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' :: (VECTOR s ty a, VECTOR s ty b) => (a -> b -> a) -> a -> Vector s ty b -> Vector s ty a Source #

O(n) Haskell-style scan with strict accumulator

scanl1 :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s 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' :: VECTOR s ty a => (a -> a -> a) -> Vector s ty a -> Vector s ty a Source #

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

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

O(n) Right-to-left prescan

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

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

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

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

O(n) Right-to-left scan

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

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

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

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

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

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

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

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

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

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

Conversions

Lists

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

O(n) Convert a vector to a list

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

O(n) Convert a list to a vector

fromListN :: forall s ty a. VECTOR s ty a => Int -> [a] -> Vector s 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) => MVector (Region m) ty a -> m (Vector (Region m) ty a) Source #

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

thaw :: (MonadR m, VECTOR (Region m) ty a) => Vector (Region m) 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) => MVector (Region m) ty a -> Vector (Region m) 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) => MVector (Region m) ty a -> m (Vector (Region m) 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) => Vector (Region m) 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) => MVector (Region m) ty a -> Vector (Region m) 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 s Char Word8 -> String Source #

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

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

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