Module

Data.Vec

Package
purescript-sized-vectors
Repository
bodil/purescript-sized-vectors

#Vec Source

newtype Vec s a

Vec s a is an array with a fixed size s defined at the type level.

Instances

#empty Source

empty :: forall a. Vec D0 a

An empty vector.

#cons Source

cons :: forall s s' a. Succ s s' => a -> Vec s a -> Vec s' a

Prepend a value to the front of a vector, creating a vector of size Succ s.

#(+>) Source

Operator alias for Data.Vec.cons (right-associative / precedence 5)

#snoc Source

snoc :: forall s s' a. Succ s s' => a -> Vec s a -> Vec s' a

Append a value to the end of a vector, creating a vector of size Succ s.

#uncons Source

uncons :: forall s1 s2 a. Pred s1 s2 => Vec s1 a -> { head :: a, tail :: Vec s2 a }

Get the head and the tail of a non-empty vector.

#singleton Source

singleton :: forall a. a -> Vec D1 a

Construct a vector containing only a single element.

#vec2 Source

vec2 :: forall a. a -> a -> Vec D2 a

shortcut for creating a 2d-Vec

#vec3 Source

vec3 :: forall a. a -> a -> a -> Vec D3 a

shortcut for creating a 3d-Vec

#fill Source

fill :: forall a s. Nat s => (Int -> a) -> Vec s a

fill vec using a function which is given indices

#range' Source

range' :: forall s. Nat s => Int -> Vec s Int

#range Source

range :: forall s. Nat s => Int -> s -> Vec s Int

#replicate Source

replicate :: forall s a. Nat s => s -> a -> Vec s a

Construct a vector of a given length containing the same element repeated.

#replicate' Source

replicate' :: forall s a. Nat s => a -> Vec s a

#fromArray Source

fromArray :: forall s a. Nat s => Array a -> Maybe (Vec s a)

Convert an array to a vector.

#length Source

length :: forall s a. Nat s => Vec s a -> Int

Get the length of a vector as an integer.

#lengthT Source

lengthT :: forall s a. Nat s => Vec s a -> s

Get the length of a vector as a type level number.

#toArray Source

toArray :: forall s a. Nat s => Vec s a -> Array a

Convert a vector into an array. This simply unwraps the underlying array, so it has no runtime cost.

#toUnfoldable Source

toUnfoldable :: forall f s a. Unfoldable f => Nat s => Vec s a -> f a

Convert a vector into any Unfoldable.

#index Source

index :: forall i s a. Nat i => Lt i s => Vec s a -> i -> a

Get the element at a given index inside a vector. Index out of bounds errors are caught at compile time.

Example:

myVector = 1 +> 2 +> 3 +> 4 +> empty
value = index myVector d2
-- value == 3
value = index myVector d4
-- out of bounds so does not type check

#(!!) Source

Operator alias for Data.Vec.index (left-associative / precedence 8)

#index' Source

index' :: forall s a. Vec s a -> Int -> Maybe a

Value-level indexation with runtime bounds check.

#concat Source

concat :: forall s1 s2 s3 a. Add s1 s2 s3 => Vec s1 a -> Vec s2 a -> Vec s3 a

Concatenate two vectors together.

#updateAt Source

updateAt :: forall i s a. Nat i => Lt i s => i -> a -> Vec s a -> Vec s a

Update a vector with a given value inserted at a given index.

#modifyAt Source

modifyAt :: forall i s a. Nat i => Lt i s => i -> (a -> a) -> Vec s a -> Vec s a

Update a vector at a given index using a function.

#insertAt Source

insertAt :: forall i s1 s2 a. Nat i => Lt i s1 => Succ s1 s2 => i -> a -> Vec s1 a -> Vec s2 a

Insert a value at a given index inside a vector, returning a vector that is one element larger.

#deleteAt Source

deleteAt :: forall i s1 s2 a. Nat i => Lt i s1 => Pred s1 s2 => i -> Vec s1 a -> Vec s2 a

Remove an element at a given index inside a vector, returning a vector that is one element smaller.

#head Source

head :: forall s a. Pos s => Vec s a -> a

Get the head of a non-empty vector.

#last Source

last :: forall s a. Pos s => Vec s a -> a

Get the last element of a non-empty vector.

#tail Source

tail :: forall s1 s2 a. Pred s1 s2 => Vec s1 a -> Vec s2 a

Get the tail of a non-empty vector.

#init Source

init :: forall s1 s2 a. Pred s1 s2 => Vec s1 a -> Vec s2 a

Get all but the last element of a non-empty vector.

#insert Source

insert :: forall s1 s2 a. Succ s1 s2 => Ord a => a -> Vec s1 a -> Vec s2 a

Insert an element into a sorted vector.

#insertBy Source

insertBy :: forall s1 s2 a. Succ s1 s2 => (a -> a -> Ordering) -> a -> Vec s1 a -> Vec s2 a

Insert an element into a sorted vector using an ordering function.

#slice Source

slice :: forall i1 i2 s1 s2 a. Nat i1 => Nat i2 => LtEq i1 s1 => LtEq i2 s1 => LtEq i1 i2 => Sub i2 i1 s2 => i1 -> i2 -> Vec s1 a -> Vec s2 a

Get a sub-vector from index i1 up to but not including index i2.

#slice' Source

slice' :: forall i1 i2 s1 s2 a. Nat i1 => Nat i2 => LtEq i1 s1 => LtEq i2 s1 => LtEq i1 i2 => Sub i2 i1 s2 => i1 -> Vec s1 a -> Vec s2 a

#take Source

take :: forall c s a. Nat c => LtEq c s => c -> Vec s a -> Vec c a

Get the first c elements from a vector.

#take' Source

take' :: forall c s a. Nat c => LtEq c s => Vec s a -> Vec c a

#drop Source

drop :: forall c s1 s2 a. Nat c => LtEq c s1 => Sub s1 c s2 => c -> Vec s1 a -> Vec s2 a

Drop the first c elements from a vector.

#drop' Source

drop' :: forall c s1 s2 a. Nat c => LtEq c s1 => Sub s1 c s2 => Vec s1 a -> Vec s2 a

#zip Source

zip :: forall s1 s2 s3 a b. Min s1 s2 s3 => Vec s1 a -> Vec s2 b -> Vec s3 (Tuple a b)

Zip two vectors together into a vector of tuples.

The new vector will be the size of the smallest input vector, and superfluous elements from the other will be discarded.

#zipWith Source

zipWith :: forall s1 s2 s3 a b c. Nat s1 => Nat s2 => Min s1 s2 s3 => (a -> b -> c) -> Vec s1 a -> Vec s2 b -> Vec s3 c

Zip two vectors together using a combining function.

The new vector will be the size of the smallest input vector, and superfluous elements from the other will be discarded.

#zipWithE Source

zipWithE :: forall s a b c. Nat s => (a -> b -> c) -> Vec s a -> Vec s b -> Vec s c

Zip two vectors with equal length together using a combining function.

#unzip Source

unzip :: forall s a b. Nat s => Vec s (Tuple a b) -> Tuple (Vec s a) (Vec s b)

Unzip a vector of tuples into a tuple of vectors.

#sort Source

sort :: forall s a. Nat s => Ord a => Vec s a -> Vec s a

Sort a vector of Ords.

#sortBy Source

sortBy :: forall s a. Nat s => (a -> a -> Ordering) -> Vec s a -> Vec s a

Sort a vector using an ordering function.

#reverse Source

reverse :: forall s a. Nat s => Vec s a -> Vec s a

Reverse a vector.

#dotProduct Source

dotProduct :: forall s a. Nat s => Semiring a => Vec s a -> Vec s a -> a
Modules
Data.Vec