Standard library - array

dataArray

There is also the module dataArray which is identical to array except that it is limited to array(data).

Basics

  def empty : array(S)                            // since PR#379
  def singleton : S -> array(S)                  // since PR#379
  def length : array(S) -> number
  def get : number -> array(S) -> S
  def safeGet : number -> data -> data           // since PR#379
  def set : number -> S -> array(S) -> array(S)  // since PR#379
  def add : array(S) -> array(S) -> array(S)     // since PR#379

empty is the empty array (same as []).

singleton(x) is the array with the single element x (same as [x]).

The length function returns the number of array elements.

The get function returns the k-th element, where 0 <= k < length(a). Another notation is a[k]. Accesses outside the valid range for k result in a runtime error.

The safeGet function returns undefined if the array access is impossible (instead of the runtime error). (Since PR#379.)

The set function returns a new array where the given index is changed to the given value.

The add function concatenates two arrays and returns the sum.

Array literals have the notation: [x0, x1, x2, ...].

Updating arrays: In functional context, the with notation constructs a new array where the given index is changed to antoher value: update(a with [k] = v).

In imperative pieces of code, you can also declare a mutable variable, and update the array contents:

def changeIt(a) {
  var b = a;
  b[1] = 42;     // assignment only valid for "var" variables
  return b
}

The addition can be used to concatenate arrays: [1,2] + [3,4] returns [1,2,3,4].

null

(Since PR#338.)

The simple observers array.length and array.keys treat null like an empty array. This doesn’t extend to any iterations, though.