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 a s' s. 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 a s' s. 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 a s2 s1. 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.

#replicate Source

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

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

#replicate' Source

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

#fromArray Source

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

Convert an array to a vector.

#length Source

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

Get the length of a vector as an integer.

#lengthT Source

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

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

#toArray Source

toArray :: forall a s. 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 a s f. Unfoldable f => Nat s => Vec s a -> f a

Convert a vector into any Unfoldable.

#index Source

index :: forall a s i. 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)

#concat Source

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

Concatenate two vectors together.

#updateAt Source

updateAt :: forall a s i. 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 a s i. 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 a s2 s1 i. 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 a s2 s1 i. 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 a s. Pos s => Vec s a -> a

Get the head of a non-empty vector.

#last Source

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

Get the last element of a non-empty vector.

#tail Source

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

Get the tail of a non-empty vector.

#init Source

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

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

#insert Source

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

Insert an element into a sorted vector.

#insertBy Source

insertBy :: forall a s2 s1. 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 a s2 s1 i2 i1. 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 a s2 s1 i2 i1. 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 a s c. Nat c => LtEq c s => c -> Vec s a -> Vec c a

Get the first c elements from a vector.

#take' Source

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

#drop Source

drop :: forall a s2 s1 c. 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 a s2 s1 c. Nat c => LtEq c s1 => Sub s1 c s2 => Vec s1 a -> Vec s2 a

#zip Source

zip :: forall b a s3 s2 s1. 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 c b a s3 s2 s1. 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.

#unzip Source

unzip :: forall b a s. 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 a s. Nat s => Ord a => Vec s a -> Vec s a

Sort a vector of Ords.

#sortBy Source

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

Sort a vector using an ordering function.

#reverse Source

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

Reverse a vector.

Modules
Data.Vec