Lists
A list is a compound data type, it is mutable and can hold other values including itself.
Creating a list
Creating a list can be done like so:
[]; //empty
[1,2,3];
[1, "hi", true];
[[1,2,3]]; //nested
Indexing
You can access a specific element by its index in a list. Lists are zero-indexed.
[1,2,3][0]; //1
[1,2,3][1]; //2
[1,2,3][2]; //3
let v = [1,2];
v[0] //1
Assigning
Unlike strings, you can update the values of a list.
let v = [1,2,3];
v[0] = 2; //[2, 2, 3]
[5,5,5][0] = 6; //[6,5,5]
Methods
All the current methods of a list object.
length()
Returns the item's count of the given list.
[1,2,3].length(); //3
append(item: any)
Adds a new item at the end of the given list.
[1,2,3].append(4); //[1,2,3,4]
remove(index: number: optional)
Removes the item at the given index from the given list when the index is provided, Otherwise it removes the last item.
[1,2,3].remove(2); //[1,2]
[1,2].remove(); //[1]
contains(item: any)
Returns true if the given item exists in the given list, Otherwise false.
[1,2,3].contains(1); //true
//false
["wow", "no!"].contains("yes!");
index(index: number)
Returns the item at the given index if it exists in the given list, Otherwise false.
[1,2,3].index(0); //1
[1,2,3].index(4); //false
clear()
Removes all the items of the given list.
[1,2,3].clear(); //[]
all()
Returns true if all the items of the list evaluate to true. Otherwise false.
[1,2,3].all(); //true
[1,2,false].all(); //false
any()
Returns true if any item from the list evaluate to true. Otherwise false.
[1,2,3].any(); //true
[1,"",false].any(); //true
[false,false,false].any(); //false
reverse()
Returns a reversed version of the given list.
[1,2,3].reverse(); //[3,2,1]
//[1, 2, "im first"]
["im first", 2, 1].reverse();
copy()
Returns an exact copy of the given list.
let v = [1,2,3];
let c = v.copy(); //[1,2,3]
c.reverse();
// v: [1,2,3]
// c: [3,2,1]
flatten()
Merges and flatten nested lists into a 1 giant list.
let v = [ [1,2,3],[4,5],6 ];
// [1,2,3,4,5,6]
v.flatten();
slice(start: number, end: number)
Returns a copy of a portion of the given list specified by the start & end arguments.
let v = [1,2,3,4];
// [1, 2, 3]
v.slice(0, 2);
//[]
v.slice(0, -1);
repeat(callback: function)
Runs the passed callback on every element of the given list. The callback must take 1 argument.
let v = [1,2,3];
v.repeat(lambda (x) -> {
// short for x = x + 1
x++;
return x;
});
// [2,3,4]