Data.Vec

lib/Data/Vec.gob

The growable array: push and pop change its length.

An array literal builds one directly when inference decides it is a Vec, out of the two records below by name: Vec over a VecStorage with the fields storage and length. Renaming any of them means changing how the compiler lowers a literal in the same commit.

type Vec a
fun new : fun(Int) -> Vec a

An empty Vec with room for capacity elements before it must grow.

fun filled : fun(Int, a) -> Vec a
fun fromArray : fun(Array a) -> Vec a

Both copy: a Vec and an Array never share their elements.

fun toArray : fun(Vec a) -> Array a
fun extendFromRaw : fun(Vec a, Prim.Ptr, Int) -> Unit

Push count elements from raw memory at source, laid out as the storage lays them out, for the rest of the library: a program cannot hold a Ptr. Untraced elements only, which the compiler checks.

fun push : fun(Vec a, a) -> Unit
fun pop : fun(Vec a) -> Option a
fun clear : fun(Vec a) -> Unit

Drop every element, keeping the capacity. The elements themselves are still reachable from the storage until they are overwritten, which costs nothing a compiler notices and saves the reallocation a fresh Vec would need.

fun map : [Iterator c, Length c, Item c ~ a] fun(c, fun(a) -> b) -> Vec b

f of each element of xs, in order. xs is anything with a length that can be iterated, so an Array maps straight to a Vec.

fun filter : fun(Vec a, fun(a) -> Bool) -> Vec a
fun fold : fun(Vec a, b, fun(b, a) -> b) -> b

Returns the result of calling step on the accumulator and each element in turn. Iter.fold takes the same arguments and works on anything iterable.

fun reverse : fun(Vec a) -> Vec a
fun append : fun(Vec a, Vec a) -> Vec a
fun copy : fun(Vec a, Int, Vec a, Int, Int) -> Unit

Copy count elements of source, from index from on, over those of target from index at on, as Array.copy does. Only over elements target has: this never changes its length, and panics unless both runs are inside their Vecs.

fun fill : fun(Vec a, Int, Int, a) -> Unit

Set the count elements of target from index at on to value. Panics unless they are all elements of target.

fun at : fun(Vec a, Int) -> Option a
fun last : fun(Vec a) -> Option a
fun swap : fun(Vec a, Int, Int) -> Unit
fun slice : fun(Vec a, Int, Int) -> Vec a

A copy, not a view. A view would alias storage that push is free to reallocate out from under it, and the growth policy is deliberately not part of this type's contract.

fun indexOf : [Eq a] fun(Vec a, a) -> Option Int
fun contains : [Eq a] fun(Vec a, a) -> Bool
fun sortBy : fun(Vec a, fun(a, a) -> Ordering) -> Vec a

A stable sort, answering a new Vec; Turkey.Array says how.

fun sort : [Ord a] fun(Vec a) -> Vec a
fun sortByKey : [Ord k] fun(Vec a, fun(a) -> k) -> Vec a

Sorted by key, stably, calling key once per element, as Array.sortByKey is: two Vecs of pairs bought so that key is not called per comparison.

fun threeWay : [Ord a] fun(a, a) -> Ordering

Ord answers in Bool, so a three-way comparison costs two questions. Ordering is the shape a sort wants, and this is the adapter.

instance Index (Vec a)
instance SetIndex (Vec a)
instance Length (Vec a)
instance Iterator (Vec a)
instance Add (Vec a)
instance Semigroup (Vec a)
instance Monoid (Vec a)
instance Foldable Vec
instance Functor Vec
instance Applicative Vec
instance Monad Vec
instance Eq (Vec a) : Eq a

Equal when they have the same length and equal elements at each index.

instance Ord (Vec a) : Ord a

Compares element by element from the front: the first index where the two differ decides, and a vector that is a proper prefix of the other is less.

instance Debug (Vec a) : Debug a

Its elements, as the array literal that builds it. Only the first length slots are read: the rest may never have been written.

instance Show (Vec a) : Debug a

What its Debug prints: a container's text shows its elements' Debug, so that a string in it is quoted.