Skip to content

Array Functions

Array manipulation and transformation.

Length

len(array)

Get array length.

let numbers = [1, 2, 3, 4, 5];
print len(numbers);  // 5

let empty = [];
print len(empty);  // 0

Modification

push(array, value)

Add element to end.

let arr = [1, 2, 3];
arr = push(arr, 4);
print arr;  // [1, 2, 3, 4]

pop(array)

Remove and return last element.

let arr = [1, 2, 3, 4];
let last = pop(arr);
print last;  // 4

Transformation

map(array, lambda)

Transform each element.

let numbers = [1, 2, 3, 4, 5];
let doubled = map(numbers, |x| => { return x * 2; });
print doubled;  // [2, 4, 6, 8, 10]

let squared = map(numbers, |x| => { return x * x; });
print squared;  // [1, 4, 9, 16, 25]

filter(array, lambda)

Keep matching elements.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let evens = filter(numbers, |x| => { return x % 2 == 0; });
print evens;  // [2, 4, 6, 8]

let large = filter(numbers, |x| => { return x > 5; });
print large;  // [6, 7, 8]

reduce(array, initial, lambda)

Combine elements.

let numbers = [1, 2, 3, 4, 5];

// Sum
let sum = reduce(numbers, 0, |acc, x| => { return acc + x; });
print sum;  // 15

// Product
let product = reduce(numbers, 1, |acc, x| => { return acc * x; });
print product;  // 120

// Max
let max = reduce(numbers, numbers[0], |acc, x| => {
    if x > acc {
        return x;
    }
    return acc;
});
print max;  // 5