Standard library - array

module array
  // 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
  // Mapping and filtering:
  def map : (S -> T) -> array(S) -> array(T)
  def map2 : (S -> T -> U) -> array(S) -> array(T) -> array(U)                  // since PR#769
  def indexedMap : (number -> S -> T) -> array(S) -> array(T)
  def indexedMap2 : (number -> S -> T -> U) -> array(S) -> array(T) -> array(U) // since PR#769
  def indexOnlyMap : (number -> T) -> array(S) -> array(T)
  def flatMap : (S -> array(T)) -> array(S) -> array(T)
  def flatten : array(array(T)) -> array(T)                                     // since PR#769
  def zip : array(S) -> array(T) -> array([S,T])                                // since PR#769
  def partition : (S -> bool) -> array(A) -> [array(A), array(A)]               // since PR#1822
  def filter : (S -> bool) -> array(S) -> array(S)
  def indexedFilter : (number -> S -> bool) -> array(S) -> array(S)             // since PR#1822
  def deduplicateKeepFirst : (S -> string) -> array(S) -> array(S)              // since PR#775
  def deduplicateKeepLast : (S -> string) -> array(S) -> array(S)               // since PR#775
  // Reducing:
  def reduce : (S -> T -> T) -> T -> array(S) -> T
  def reduceRight : (S -> T -> T) -> T -> array(S) -> T
  def indexedReduce : (number -> S -> T -> T) -> T -> array(S) -> T
  def indexedReduceRight : (S -> T -> T) -> T -> array(S) -> T
  // Result chaining:
  def andThen : (T -> result(S, U)) ->   // since PR1353
                result(S, array(T)) ->
                result(S, array(U))
  // min/max reductions:
  def minIndex : array(S) -> number               // since PR#695
  def minValue : array(S) -> S                    // since PR#695
  def minIndexBy : (S -> T) -> array(S) -> number // since PR#695
  def minValueBy : (S -> T) -> array(S) -> S      // since PR#695
  def maxIndex : array(S) -> number               // since PR#695
  def maxValue : array(S) -> S                    // since PR#695
  def maxIndexBy : (S -> T) -> array(S) -> number // since PR#695
  def maxValueBy : (S -> T) -> array(S) -> S      // since PR#695
  // Construction:
  def initialize : number -> (number -> S) -> array(S)
  def enumerate : number -> array(number)
  def keys : array(S) -> array(number)
  def rev : array(S) -> array(S)
  // Streaming:
  def indexRange : array(S) -> number*
  def indexRangeRev : array(S) -> number*    // since PR#379
  def valueRange : array(S) -> S*
  def valueRangeRev : array(S) -> S*         // since PR#379
  // Other iterations:
  def iter : (S -> null) -> array(S) -> null
  def exists : (S -> bool) -> array(S) -> bool
  def forAll : (S -> bool) -> array(S) -> bool               // since PR#1822
  def find : (S -> bool) -> array(S) -> S                    // since PR#379
  def findIndex : (data -> bool) -> data -> number           // since PR#379
  def findMapped : (data -> T) -> data -> T                  // since PR#379
  def findIndexMapped : (number -> data -> T) -> data -> T   // since PR#379
  // Queries on arrays:
  def contains : S -> array(S) -> bool
  def featuresOne : array(S) -> array(S) -> bool             // since PR#811
  def featuresAll : array(S) -> array(S) -> bool             // since PR#811
  // Special elements, slicing:
  def first : array(S) -> S
  def firstWithDefault : S -> array(S) -> S
  def last : array(S) -> S
  def lastWithDefault : S -> array(S) -> S
  def skip : array(S) -> array(S)
  def skipn : number -> array(S) -> array(S)
  def firstn : number -> array(S) -> array(S)
  def lastn : number -> array(S) -> array(S)
  // Sort and merge:
  def grouped : (S -> data) -> array(S) -> array(array(S))
  def merge : (S -> data) -> (S -> data) -> array(S) -> array(S) -> array(S)
  def sort : (S -> data) -> array(S) -> array(S)
  def sort_ci : (S -> string) -> array(S) -> array(S)                            // since PR#1821
  def dirSort : bool -> (S -> data) -> array(S) -> array(S)                      // since PR#769
  def dirSort_ci : bool -> (S -> string) -> array(S) -> array(S)                 // since PR#1821
  def sortBy : (A -> A -> bool) -> array(A) -> array(A)                          // since PR#1821
  def sortLike : string -> array(string) -> array(map(data)) -> array(map(data)) // since PR1715
  // Relations
  def innerJoin : (S -> string) -> array(S) ->
                  (T -> string) -> array(T) -> (S -> T -> U) -> array(U)  // since PR#769
  // Conversions:
  def toStream : array(S) -> stream(S)
  def toStreamPairs : array(S) -> stream([number, S])
module end

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.)