Array Class
Array is Alia's built-in array class, used to store and manipulate ordered collections of elements.
Creating Arrays
Literal Form
const arr1 = [1, 2, 3, 4, 5]
const arr2 = ['a', 'b', 'c']
const arr3 = [1, 'hello', true]
Constructor Form
const arr4 = new Array [1, 2, 3]
Instance Methods
length
length is the array's length property and returns a number.
const arr = [1, 2, 3, 4, 5]
print arr.length ~ outputs 5
push
push appends elements to the end of the array and returns a number representing the new length.
const arr = [1, 2, 3]
const len = arr.push 4
print len ~ outputs 4
print arr ~ outputs Array [Number 1, Number 2, Number 3, Number 4]
pop
pop removes and returns the element at the end of the array. When called without arguments, use the call form.
const arr = [1, 2, 3]
const last = call arr.pop
print last ~ outputs 3
print arr ~ outputs Array [Number 1, Number 2]
join
join joins the array elements into a string. The separator is a single argument; the default separator is ',', and the default form is invoked with call.
const arr = ['a', 'b', 'c']
print arr.join '-' ~ outputs "a-b-c"
print call arr.join ~ outputs "a,b,c"
slice
slice extracts a portion of the array and returns a new array. The two-argument form separates the start and end positions with the helper keyword to; the single-argument form only specifies the start position.
const arr = [1, 2, 3, 4, 5]
print arr.slice 1 to 4 ~ outputs Array [Number 2, Number 3, Number 4]
print arr.slice 2 ~ outputs Array [Number 3, Number 4, Number 5]
map
map takes a single Alias callback and returns a new array, with signature alias <Value fn> -> Array.
find
find takes a single Alias callback and returns the found value, with signature alias <Value fn> -> Value.
indexOf
indexOf takes a single element to search for and returns its numeric index. Returns -1 when no match is found.
const arr = [1, 2, 3, 2, 1]
print arr.indexOf 9 ~ outputs -1
includes
includes takes a single element to search for and returns a boolean. A missing element corresponds to false.
const arr = [1, 2, 3]
print arr.includes 5 ~ outputs false
toObject
toObject returns an Object with the indices as keys. When called without arguments, use the call form.
const arr = [1, 2]
const obj = call arr.toObject
toSheet
toSheet returns a Sheet with this array as a single row of data. When called without arguments, use the call form.
const arr = [1, 2]
const sheet = call arr.toSheet
toString
toString returns the array's description string, identical to the result of toDescription. When called without arguments, use the call form.
const arr = [1, 2, 3]
print call arr.toString ~ outputs "Array [Number Number 1, Number Number 2, Number Number 3]"
toDescription
toDescription returns the array's description string; an empty array corresponds to "Array []". When called without arguments, use the call form.
const empty = []
print call empty.toDescription ~ outputs "Array []"
toCode
toCode returns the code-form string of the array. When called without arguments, use the call form.
const arr = [1, 2, 3]
print call arr.toCode ~ outputs "[1, 2, 3]"
valueOf
valueOf returns the array's underlying storage. When called without arguments, use the call form.
const arr = [1, 2, 3]
print call arr.valueOf ~ outputs 1,2,3
Array Access
Array elements are accessed by index:
const arr = ['a', 'b', 'c']
print arr[0] ~ outputs "a"
print arr[1] ~ outputs "b"
print arr[2] ~ outputs "c"
Array Traversal
Index-based traversal uses the four-segment for loop:
const arr = ['a', 'b', 'c']
for { let i = 0 } i < arr.length { i ++ } { print arr[i] }
Example Code
~ Comprehensive array example
~ 1. Create an array
const numbers = [1, 2, 3, 4, 5]
const strings = ['hello', 'world']
~ 2. Get the length
print numbers.length ~ outputs 5
~ 3. Append an element
const len = numbers.push 6
print len ~ outputs 6
~ 4. Remove an element
const last = call numbers.pop
print last ~ outputs 6
~ 5. Slice an array
const sliced = numbers.slice 1 to 4
print sliced ~ outputs Array [Number 2, Number 3, Number 4]
~ 6. Join an array
print strings.join '-' ~ outputs "hello-world"
print call strings.join ~ outputs "hello,world"
~ 7. Absence checks
print strings.includes 'Alia' ~ outputs false
print numbers.indexOf 9 ~ outputs -1
~ 8. Code form
print call numbers.toCode ~ outputs "[1, 2, 3, 4, 5]"
~ 9. Index-based traversal
for { let i = 0 } i < strings.length { i ++ } { print strings[i] }
~ 10. Index access
const arr = ['first', 'second', 'third']
print arr[0] ~ outputs "first"
print arr[arr.length - 1] ~ outputs "third"