Lists
Lists are an ordered list of elements of possibly different types identified by a number index. Each element in a list can be accessed individually by their index. Lists are constructed as a comma separated list of elements, can contain any type of value, and are enclosed by square brackets:
[
"Zaid",
57.3,
function (x) {
return x * x
},
]
Accessing Elements
You can access any element in a list by calling the subscript operator on it with the index of the element you want. Like most languages, indices start at zero:
vocabulary = ["activation", "propogate", "execute", "initialize"]
print(vocabulary[0]) // >> activation
print(vocabulary[1]) // >> propogate
print(vocabulary[2]) // >> execute
print(vocabulary[3]) // >> initialize
Methods
For the remainder of this documentation, we'll discuss each method available on lists.
first()
The first
method returns the first element in the list. If the list is empty, it returns null
:
[1, 2, 3, 4].first()
// 1
join()
The join
method joins the items in a list. It takes a single argument, the string to use as the "glue" between the items in the list.
[1, 2, 3, 4].join('-')
// 1-2-3-4
last()
The last
method returns the last element in the list. If the list is empty, it returns null
:
[1, 2, 3, 4].last()
// 4
length()
The length
method returns the number of elements in the list:
[1, 2, 3, 4].length()
// 4
pop()
The pop
method removes the last element from the list and returns it:
list = [1, 2, 3, 4]
list.pop()
// 4
print(list)
// [1, 2, 3]
push()
The push
method adds an element to the end of the list:
list = [1, 2, 3, 4]
list.push(5)
print(list)
// [1, 2, 3, 4, 5]
tail()
The tail
method returns a new list containing all but the first element of the list:
[1, 2, 3, 4].tail()
// [2, 3, 4]
toString()
The toString
method returns a string representation of the list:
[1, 2, 3, 4].toString()
// [1, 2, 3, 4]