Skip to content

List Prototypes

List values expose collection helpers.

let nums = [1, 2, 3]
println(nums.length())
println(nums.includes(2))

Prototype calls currently work reliably on values stored in variables.

let s = "string"
println(s.length())

Calling prototype methods directly on literals is currently limited by AST/parser behavior.

// Currently may not work:
println("string".length())

Prefer assigning the literal to a variable first.

let text = "string"
println(text.length())
MethodReturnsDescription
length()intReturns the list length.
join(sep: string)stringJoins items into a string.
each(fn: fn(T) -> void)voidCalls fn for each item.
map(fn: fn(T) -> any)[]anyMaps each item into a new list.
filter(fn: fn(T) -> bool)[]TKeeps items where the function returns true.
includes(search: any)boolChecks whether the list contains a value.
push(value: T)voidAdds a value to the end.
insert(index: int, value: T)voidInserts a value at index.
del(index: int)voidDeletes the value at index.
pop()TRemoves and returns the last item.
shift()TRemoves and returns the first item.
unshift(value: T)voidAdds a value to the beginning.
slice(start: int, end: int)[]TReturns a sub-list.
clear()voidRemoves all items.
reverse()voidReverses the list in place.

Lists implement internal __get__ and __set__, which power indexed access.

let items = ["a", "b", "c"]
println(items[0])
items[1] = "x"
println(items)
let items = ["a", "b", "c"]
println(items.length())
println(items.join(", "))
println(items.includes("b"))
let items = ["a", "b"]
items.push("c")
items.unshift("#")
items.insert(1, "start")
println(items)
let items = ["a", "b", "c"]
let last = items.pop()
let first = items.shift()
println(first)
println(last)
println(items)
let nums = [1, 2, 3, 4]
let doubled = nums.map(fn(item: int) int {
return item * 2
})
let even = nums.filter(fn(item: int) bool {
return item % 2 == 0
})
println(doubled)
println(even)
let items = ["a", "b", "c", "d"]
println(items.slice(1, 3))
items.reverse()
println(items)
items.clear()
println(items.length())