List Methods
Lists in Risor have built-in methods for adding, removing, searching, and transforming elements. Some methods mutate the list in place, while others return a new value.
Method Reference
| Method | Mutates | Description |
|---|---|---|
append(value) | Yes | Add an element to the end |
extend(list) | Yes | Append all elements from another list |
insert(index, value) | Yes | Insert an element at a position |
pop(index) | Yes | Remove and return an element |
remove(value) | Yes | Remove the first occurrence of a value |
reverse() | Yes | Reverse the list in place |
sort() | Yes | Sort the list in place |
clear() | Yes | Remove all elements |
copy() | No | Return a shallow copy |
count(value) | No | Count occurrences of a value |
index(value) | No | Index of the first occurrence |
filter(fn) | No | Return a new filtered list |
map(fn) | No | Return a new mapped list |
each(fn) | No | Iterate over elements (returns nil) |
reduce(init, fn) | No | Reduce to a single value |
Mutating Methods
These methods modify the list in place.
append
Add a single element to the end of the list.
let items = [1, 2, 3]
items.append(4)
// items == [1, 2, 3, 4]extend
Append all elements from another list.
let items = [1, 2]
items.extend([3, 4, 5])
// items == [1, 2, 3, 4, 5]insert
Insert an element at a given index, shifting subsequent elements to the right.
let items = [1, 2, 3]
items.insert(0, 0)
// items == [0, 1, 2, 3]
items.insert(2, 99)
// items == [0, 1, 99, 2, 3]pop
Remove and return an element at the given index position. The index must be provided.
let items = [10, 20, 30]
items.pop(2) // 30, items == [10, 20]
items.pop(0) // 10, items == [20]remove
Remove the first occurrence of a value from the list.
let items = [1, 2, 3, 2]
items.remove(2)
// items == [1, 3, 2]reverse
Reverse the list in place.
let items = [1, 2, 3]
items.reverse()
// items == [3, 2, 1]sort
Sort the list in place.
let items = [3, 1, 2]
items.sort()
// items == [1, 2, 3]clear
Remove all elements from the list.
let items = [1, 2, 3]
items.clear()
// items == []Non-Mutating Methods
These methods return a new value without modifying the original list.
copy
Return a shallow copy of the list.
let original = [1, 2, 3]
let cloned = original.copy()
cloned.append(4)
// original == [1, 2, 3]
// cloned == [1, 2, 3, 4]count
Count the number of occurrences of a value.
[1, 2, 2, 3, 2].count(2) // 3
[1, 2, 3].count(5) // 0index
Return the index of the first occurrence of a value.
[10, 20, 30].index(20) // 1filter
Return a new list containing only the elements for which the given function returns true.
let items = [1, 2, 3, 4, 5]
items.filter(x => x > 3) // [4, 5]
items.filter(x => x % 2 == 0) // [2, 4]map
Return a new list with each element transformed by the given function.
let items = [1, 2, 3]
items.map(x => x * 2) // [2, 4, 6]
items.map(x => string(x)) // ["1", "2", "3"]each
Iterate over every element, calling the given function for each one. Returns nil. Use this for side effects.
let total = 0
[1, 2, 3].each(x => { total += x })
// total is 6reduce
Reduce a list to a single value by applying a function that accumulates a result. The first argument is the initial accumulator value.
[1, 2, 3, 4].reduce(0, (acc, x) => acc + x) // 10
[1, 2, 3, 4].reduce(1, (acc, x) => acc * x) // 24
// Build a string
["a", "b", "c"].reduce("", (acc, x) => acc + x) // "abc"