Array Functions¶
Array manipulation and transformation.
Length¶
len(array)¶
Get array length.
Modification¶
push(array, value)¶
Add element to end.
pop(array)¶
Remove and return last element.
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