Skip to content

Array

std:array provides functions for working with arrays.

Functions

push ( array, value )

Addes the given value to the end of the array.

import "std:array";
 
let arr = [1, 2, 3];
push(arr, 4);
print(arr);
 
Console 
[1, 2, 3, 4] 

pop ( array )

Removes the last element from the array and returns it.

import "std:array";
 
let arr = [1, 2, 3];
let last = pop(arr);
print(last);
print(arr);
 
Console 
3
[1, 2] 

head ( array )

Returns the first element of the array.

import "std:array";
 
let arr = [1, 2, 3];
let first = head(arr);
print(first);
 
Console 
1

tail ( array )

Removes the first element of the array and returns the rest.

import "std:array";
 
let arr = [1, 2, 3];
let rest = tail(arr);
print(rest);
 
Console 
[2, 3] 

map ( array, callback )

Returns a new array with the results of calling a provided function on every element in the array.

import "std:array";
 
let arr = [1, 2, 3];
let doubled = map(arr, fn(x) {
    return x * 2;
});
print(doubled);
 
Console 
[2, 4, 6] 

includes ( array, value )

Returns true if the array contains the given value, otherwise false.

import "std:array";
 
let arr = [1, 2, 3];
let hasTwo = includes(arr, 2);
print(hasTwo);
 
Console 
true

slice ( array, first_index, last_index )

Returns a new array containing the elements from first_index to last_index (exclusive).

import "std:array";
 
let arr = [1, 2, 3, 4, 5];
let sliced = slice(arr, 1, 4);
print(sliced);
 
Console 
[2, 3, 4] 

swap ( array, index, value )

Replaces the element at the given index with the given value.

import "std:array";
 
let arr = [1, 2, 3];
swap(arr, 1, 4);
print(arr);
 
Console 
[1, 4, 3]