Array module

The design of Motoko strives to minimize built-in operations, and whenever possible, to instead implement array operations as Motoko functions in a standard library module dedicated to extending that type’s suite of supported operations.

Append

Appends one array to a second array.

public func append<A>(xs : [A], ys : [A]) : [A]

Apply

Applies an array of functions to an array of arguments. The arrays must be of equal length.

public func apply<A, B>(fs : [A -> B], xs : [A]) : [B]

Bind

Applies a function to each array element to produce an array of all results.

public func bind<A, B>(xs : [A], f : A -> [B]) : [B]

Folds

Folds left over the array.

public func foldl<A, B>(f : (B, A) -> B, initial : B, xs : [A]) : B

Folds right over the array.

public func foldr<A, B>(f : (A, B) -> B, initial : B, xs : [A]) : B

Find

Finds an element in the array.

public func find<A>(f : A -> Bool, xs : [A]) : ?A

Freeze

Converts a mutable array into an immutable one.

public func freeze<A>(xs : [var A]) : [A]

Join

Converts an array of arrays into a single (flattened) array.

public func join<A>(xs : [[A]]) : [A]

Map

Maps each array element by the given mapping function.

public func map<A, B>(f : A -> B, xs : [A]) : [B]

Pure

public func pure<A>(x: A) : [A] ;

Thaw

Converts an immutable array into a mutable one.

public func thaw<A>(xs : [A]) : [var A]