Vector structure
May 20, 2026 · View on GitHub
The Vector structure provides the vector type and operations for
creating, examining, and transforming immutable fixed-length sequences
of elements.
Specified by the Standard ML Basis Library.
Synopsis
type 'a vector val maxLen : int val fromList : 'a list -> 'a vector val tabulate : int * (int -> 'a) -> 'a vector val length : 'a vector -> int val sub : 'a vector * int -> 'a val update : 'a vector * int * 'a -> 'a vector val concat : 'a vector list -> 'a vector val appi : (int * 'a -> unit) -> 'a vector -> unit val app : ('a -> unit) -> 'a vector -> unit val mapi : (int * 'a -> 'b) -> 'a vector -> 'b vector val map : ('a -> 'b) -> 'a vector -> 'b vector val foldli : (int * 'a * 'b -> 'b) -> 'b -> 'a vector -> 'b val foldri : (int * 'a * 'b -> 'b) -> 'b -> 'a vector -> 'b val foldl : ('a * 'b -> 'b) -> 'b -> 'a vector -> 'b val foldr : ('a * 'b -> 'b) -> 'b -> 'a vector -> 'b val findi : (int * 'a -> bool) -> 'a vector -> (int * 'a) option val find : ('a -> bool) -> 'a vector -> 'a option val exists : ('a -> bool) -> 'a vector -> bool val all : ('a -> bool) -> 'a vector -> bool val collate : ('a * 'a -> order) -> 'a vector * 'a vector -> order
type 'a vector
is the type of immutable fixed-length arrays with elements of type
'a.
maxLen
maxLen returns the maximum length of vectors supported in this
implementation.
fromList
fromList l creates a new vector from l, whose length is length l
and with the ith element of l used as the
ith element of the vector. Raises Size if maxLen <
n.
tabulate
tabulate (n, f) returns a vector of length n equal to [f(0), f(1), ..., f(n-1)], created from left to right. This is equivalent
to the expression
fromList (List.tabulate (n, f))
Raises Size if n < 0 or maxLen < n.
length
length v (or v.length ()) returns the number of elements in the vector v.
sub
sub (vec, i) (or vec.sub i) returns the ith element of vector vec.
Raises Subscript if i < 0 or size vec ≤ i.
update
update (vec, i, x) returns a new vector, identical to vec, except
the ith element of vec is set to x. Raises
Subscript if i < 0 or size vec ≤ i.
concat
concat l returns the vector that is the concatenation of the vectors
in the list l. Raises Size if the total length of these vectors
exceeds maxLen
appi
appi f vec applies the function f to the elements of a vector in
left to right order (i.e., in order of increasing indices).
It is equivalent to
List.app f (foldri (fn (i,a,l) => (i,a)::l) [] vec)
app
app f vec applies the function f to the elements of a vector in
left to right order (i.e., in order of increasing indices).
It is equivalent to
List.app f (foldr (fn (a,l) => a::l) [] vec)
mapi
mapi f vec applies the function f to the elements of the argument
vector vec, supplying the vector index and element as arguments to
each call.
It is equivalent to
fromList (List.map f (foldri (fn (i,a,l) => (i,a)::l) [] vec))
map
map f vec applies the function f to the elements of the argument
vector vec.
It is equivalent to
fromList (List.map f (foldr (fn (a,l) => a::l) [] vec))
foldli
foldli f init vec folds the function f over all the (index,
element) pairs of vector vec, left to right, using the initial value
init.
foldri
foldri f init vec folds the function f over all the (index,
element) pairs of vector vec, right to left, using the initial value
init.
foldl
foldl f init vec folds the function f over all the elements of
vector vec, left to right, using the initial value init.
foldr
foldr f init vec folds the function f over all the elements of
vector vec, right to left, using the initial value init.
findi
findi f vec applies f to each element x and element index i of
the vector vec, from left to right, until f(i, x) evaluates to
true. It returns SOME (i, x) if such an x exists; otherwise it
returns NONE.
find
find f vec applies f to each element x of the vector vec, from
left to right, until f(x) evaluates to true. It returns SOME (x)
if such an x exists; otherwise it returns NONE.
exists
exists f vec applies f to each element x of the vector vec,
from left to right (i.e., increasing indices), until f(x) evaluates
to true; it returns true if such an x exists and false
otherwise.
all
all f vec applies f to each element x of the vector vec, from
left to right, until f(x) evaluates to false. It returns false
if such an x exists; otherwise it returns true. It is equivalent
to not(exists (not o f) vec).
collate
collate f (v1, v2) performs lexicographic comparison of the two
vectors using the given ordering f on elements.