# The Standard ML Basis Library

### The `Vector` structure

The Vector structure provides polymorphic immutable sequences.

#### Synopsis

```signature VECTOR structure Vector : VECTOR ```

#### Interface

```eqtype '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 extract : ('a vector * int * int option) -> 'a vector val concat : 'a vector list -> 'a vector val mapi : ((int * 'a) -> 'b) -> ('a vector * int * int option) -> 'b vector val map : ('a -> 'b) -> 'a vector -> 'b vector val appi : ((int * 'a) -> unit) -> ('a vector * int * int option) -> unit val app : ('a -> unit) -> 'a vector -> unit val foldli : ((int * 'a * 'b) -> 'b) -> 'b -> ('a vector * int * int option) -> 'b val foldri : ((int * 'a * 'b) -> 'b) -> 'b -> ('a vector * int * int option) -> 'b val foldl : (('a * 'b) -> 'b) -> 'b -> 'a vector -> 'b val foldr : (('a * 'b) -> 'b) -> 'b -> 'a vector -> 'b ```

#### Description

`eqtype 'a vector`

```maxLen ```
is the maximum length of vectors supported by this implementation. Attempts to create larger vectors will result in the Size exception being raised.

```fromList l ```
creates a new vector from a list of elements. If the length of the list is greater than maxLen, then the Size exception is raised.

```tabulate (n, f) ```
creates an vector of n elements, where the elements are defined in order of increasing index by applying f to the element's index. This is equivalent to the expression:
```	  fromList (List.tabulate (n, f))

```
If n < 0 or `maxLen` < n, then the Size exception is raised.

```length vec ```
returns |vec|, the length of the array vec.

```sub (vec, i) ```
returns the ith element of the vector vec. If i < 0 or |vec| <= i, then the Subscript exception is raised.

```extract slice ```
extracts the vector slice slice from the vector vec, and returns it as a vector. If the slice is not valid, then the exception Subscript is raised.

```concat l ```
returns the vector that is the concatenation of the vectors in the list l. If the total length of these vectors exceeds maxLen, then the Size exception is raised.

```mapi f slice ```
``` map f vec ```
produce new vectors by mapping the function f from left to right over the argument vector or slice. The more general mapi function applies f to the elements of the vector slice slice and supplies both the element and the element's index to the function f. If slice is not valid, then the exception Subscript is raised. The expression `mapi f slice` is equivalent to:
```        fromList (List.map f (foldri (fn (i,a,l) => (i,a)::l) [] slice))

```

The function map applies f to the whole vector and does not supply the element index to f. Thus the expression `map f vec` is equivalent to:

```	    mapi (f o #2) (vec, 0, NONE)

```

```appi f slice ```
``` app f vec ```
apply the function f to the elements of a vector in left to right order (i.e., increasing indices). The more general appi function applies f to the elements of the vector slice slice and supplies both the element and the element's index to the function f. If slice is not valid, then the exception Subscript is raised.

The function app applies f to the whole vector and does not supply the element index to f. Thus the expression `app f vec` is equivalent to:

```	    appi (f o #2) (vec, 0, NONE)

```

```foldli f init slice ```
``` foldri f init slice ```
``` foldl f init vec ```
``` foldr f init vec ```
fold the function f over the elements of a vector, using the value init as the initial value. The functions foldli and foldl apply the function f from left to right (increasing indices), while the functions foldri and foldr work from right to left (decreasing indices). The more general functions foldli and foldri work on vector slices, and supply both the element and the element's index to the function f.

The functions foldl and foldr work on the whole vector vec and do not supply the element index to f. Thus the expression `foldl f init vec` is equivalent to:

```	    foldli (fn (_, a, x) => f(a, x))
init (vec, 0, NONE)

```
Example:

One can extract the list of elements in a vector vec by the expression:

```	      foldr (op ::) [] vec

```