% API
Builtins
List
List.binary_search
List.binary_search : func(list: [T], by: func(x,y:&T->Int32) = T.compare -> Int)
Performs a binary search on a sorted list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The sorted list to search. | - |
| by | func(x,y:&T->Int32) |
The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: Assuming the input list is sorted according to the given comparison function, return the index where the given item would be inserted to maintain the sorted order. That is, if the item is found, return its index, otherwise return the place where it would be found if it were inserted and the list were sorted.
Example:
assert [1, 3, 5, 7, 9].binary_search(5) == 3
assert [1, 3, 5, 7, 9].binary_search(-999) == 1
assert [1, 3, 5, 7, 9].binary_search(999) == 6
List.by
List.by : func(list: [T], step: Int -> [T])
Creates a new list with elements spaced by the specified step value.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The original list. | - |
| step | Int |
The step value for selecting elements. | - |
Return: A new list with every step-th element from the original list.
Example:
assert [1, 2, 3, 4, 5, 6].by(2) == [1, 3, 5]
List.clear
List.clear : func(list: @[T] -> Void)
Clears all elements from the list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list to be cleared. | - |
Return: Nothing.
Example:
list := &[10, 20]
list.clear()
assert list[] == []
List.counts
List.counts : func(list: [T] -> {T=Int})
Counts the occurrences of each element in the list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to count elements in. | - |
Return: A table mapping each element to its count.
Example:
assert [10, 20, 30, 30, 30].counts() == {10:1, 20:1, 30:3}
List.find
List.find : func(list: [T], target: T -> Int?)
Finds the index of the first occurrence of an element (if any).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to search through. | - |
| target | T |
The item to search for. | - |
Return: The index of the first occurrence or none if not found.
Example:
assert [10, 20, 30, 40, 50].find(20) == 2
assert [10, 20, 30, 40, 50].find(9999) == none
List.from
List.from : func(list: [T], first: Int -> [T])
Returns a slice of the list starting from a specified index.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The original list. | - |
| first | Int |
The index to start from. | - |
Return: A new list starting from the specified index.
Example:
assert [10, 20, 30, 40, 50].from(3) == [30, 40, 50]
List.has
List.has : func(list: [T], target: T -> Bool)
Checks if the list has an element.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to check. | - |
| target | T |
The element to check for. | - |
Return: yes if the list has the element, no otherwise.
Example:
assert [10, 20, 30].has(20) == yes
List.heap_pop
List.heap_pop : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> T?)
Removes and returns the top element of a heap or none if the list is empty. By default, this is the minimum value in the heap.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the heap. | - |
| by | func(x,y:&T->Int32) |
The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: The removed top element of the heap or none if the list is empty.
Example:
my_heap := &[30, 10, 20]
my_heap.heapify()
assert my_heap.heap_pop() == 10
List.heap_push
List.heap_push : func(list: @[T], item: T, by = T.compare -> Void)
Adds an element to the heap and maintains the heap property. By default, this is a minimum heap.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the heap. | - |
| item | T |
The item to be added. | - |
| by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: Nothing.
Example:
my_heap : &[Int]
my_heap.heap_push(10)
assert my_heap.heap_pop() == 10
List.heapify
List.heapify : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> Void)
Converts a list into a heap.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list to be heapified. | - |
| by | func(x,y:&T->Int32) |
The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: Nothing.
Example:
my_heap := &[30, 10, 20]
my_heap.heapify()
List.insert
List.insert : func(list: @[T], item: T, at: Int = 0 -> Void)
Inserts an element at a specified position in the list.
Since indices are 1-indexed and negative indices mean "starting from the back", an index of 0 means "after the last item".
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list. | - |
| item | T |
The item to be inserted. | - |
| at | Int |
The index at which to insert the item. | 0 |
Return: Nothing.
Example:
list := &[10, 20]
list.insert(30)
assert list == [10, 20, 30]
list.insert(999, at=2)
assert list == [10, 999, 20, 30]
List.insert_all
List.insert_all : func(list: @[T], items: [T], at: Int = 0 -> Void)
Inserts a list of items at a specified position in the list.
Since indices are 1-indexed and negative indices mean "starting from the back", an index of 0 means "after the last item".
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list. | - |
| items | [T] |
The items to be inserted. | - |
| at | Int |
The index at which to insert the item. | 0 |
Return: Nothing.
Example:
list := &[10, 20]
list.insert_all([30, 40])
assert list == [10, 20, 30, 40]
list.insert_all([99, 100], at=2)
assert list == [10, 99, 100, 20, 30, 40]
List.pop
List.pop : func(list: &[T], index: Int = -1 -> T?)
Removes and returns an item from the list. If the given index is present in the list, the item at that index will be removed and the list will become one element shorter.
Since negative indices are counted from the back, the default behavior is to pop the last value.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | &[T] |
The list to remove an item from. | - |
| index | Int |
The index from which to remove the item. | -1 |
Return: none if the list is empty or the given index does not exist in the list, otherwise the item at the given index.
Example:
list := &[10, 20, 30, 40]
assert list.pop() == 40
assert list[] == [10, 20, 30]
assert list.pop(index=2) == 20
assert list[] == [10, 30]
List.random
List.random : func(list: [T], random: func(min,max:Int64->Int64)? = none -> T?)
Selects a random element from the list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list from which to select a random element. | - |
| random | func(min,max:Int64->Int64)? |
If provided, this function will be used to get a random index in the list. Returned values must be between min and max (inclusive). (Used for deterministic pseudorandom number generation) |
none |
Return: A random element from the list or none if the list is empty.
Example:
nums := [10, 20, 30]
pick := nums.random()!
assert nums.has(pick)
empty : [Int]
assert empty.random() == none
List.remove_at
List.remove_at : func(list: @[T], at: Int = -1, count: Int = 1 -> Void)
Removes elements from the list starting at a specified index.
Since negative indices are counted from the back, the default behavior is to remove the last item.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list. | - |
| at | Int |
The index at which to start removing elements. | -1 |
| count | Int |
The number of elements to remove. | 1 |
Return: Nothing.
Example:
list := &[10, 20, 30, 40, 50]
list.remove_at(2)
assert list == [10, 30, 40, 50]
list.remove_at(2, count=2)
assert list == [10, 50]
List.remove_item
List.remove_item : func(list: @[T], item: T, max_count: Int = -1 -> Void)
Removes all occurrences of a specified item from the list.
A negative max_count means "remove all occurrences".
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list. | - |
| item | T |
The item to be removed. | - |
| max_count | Int |
The maximum number of occurrences to remove. | -1 |
Return: Nothing.
Example:
list := &[10, 20, 10, 20, 30]
list.remove_item(10)
assert list == [20, 20, 30]
list.remove_item(20, max_count=1)
assert list == [20, 30]
List.reversed
List.reversed : func(list: [T] -> [T])
Returns a reversed slice of the list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to be reversed. | - |
Return: A slice of the list with elements in reverse order.
Example:
assert [10, 20, 30].reversed() == [30, 20, 10]
List.sample
List.sample : func(list: [T], count: Int, weights: [Num]? = none, random: func(->Num)? = none -> [T])
Selects a sample of elements from the list, optionally with weighted probabilities.
Errors will be raised if any of the following conditions occurs: - The given list has no elements and count >= 1 - count < 0 (negative count) - The number of weights provided doesn't match the length of the list. - Any weight in the weights list is negative, infinite, or NaN - The sum of the given weights is zero (zero probability for every element).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to sample from. | - |
| count | Int |
The number of elements to sample. | - |
| weights | [Num]? |
The probability weights for each element in the list. These values do not need to add up to any particular number, they are relative weights. If no weights are given, elements will be sampled with uniform probability. | none |
| random | func(->Num)? |
If provided, this function will be used to get random values for sampling the list. The provided function should return random numbers between 0.0 (inclusive) and 1.0 (exclusive). (Used for deterministic pseudorandom number generation) |
none |
Return: A list of sampled elements from the list.
Example:
_ := [10, 20, 30].sample(2, weights=[90%, 5%, 5%]) # E.g. [10, 10]
List.shuffle
List.shuffle : func(list: @[T], random: func(min,max:Int64->Int64)? = none -> Void)
Shuffles the elements of the list in place.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list to be shuffled. | - |
| random | func(min,max:Int64->Int64)? |
If provided, this function will be used to get a random index in the list. Returned values must be between min and max (inclusive). (Used for deterministic pseudorandom number generation) |
none |
Return: Nothing.
Example:
nums := &[10, 20, 30, 40]
nums.shuffle()
# E.g. [20, 40, 10, 30]
List.shuffled
List.shuffled : func(list: [T], random: func(min,max:Int64->Int64)? = none -> [T])
Creates a new list with elements shuffled.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to be shuffled. | - |
| random | func(min,max:Int64->Int64)? |
If provided, this function will be used to get a random index in the list. Returned values must be between min and max (inclusive). (Used for deterministic pseudorandom number generation) |
none |
Return: A new list with shuffled elements.
Example:
nums := [10, 20, 30, 40]
_ := nums.shuffled()
# E.g. [20, 40, 10, 30]
List.slice
List.slice : func(list: [T], from: Int, to: Int -> [T])
Returns a slice of the list spanning the given indices (inclusive).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The original list. | - |
| from | Int |
The first index to include. | - |
| to | Int |
The last index to include. | - |
Return: A new list spanning the given indices. Note: negative indices are counted from the back of the list, so -1 refers to the last element, -2 the second-to-last, and so on.
Example:
assert [10, 20, 30, 40, 50].slice(2, 4) == [20, 30, 40]
assert [10, 20, 30, 40, 50].slice(-3, -2) == [30, 40]
List.sort
List.sort : func(list: @[T], by = T.compare -> Void)
Sorts the elements of the list in place in ascending order (small to large).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | @[T] |
The mutable reference to the list to be sorted. | - |
| by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: Nothing.
Example:
list := &[40, 10, -30, 20]
list.sort()
assert list == [-30, 10, 20, 40]
list.sort(func(a,b:&Int) a.abs() <> b.abs())
assert list == [10, 20, -30, 40]
List.sorted
List.sorted : func(list: [T], by = T.compare -> [T])
Creates a new list with elements sorted.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to be sorted. | - |
| by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | T.compare |
Return: A new list with sorted elements.
Example:
assert [40, 10, -30, 20].sorted() == [-30, 10, 20, 40]
assert [40, 10, -30, 20].sorted(
func(a,b:&Int) a.abs() <> b.abs()
) == [10, 20, -30, 40]
List.to
List.to : func(list: [T], last: Int -> [T])
Returns a slice of the list from the start of the original list up to a specified index (inclusive).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The original list. | - |
| last | Int |
The index up to which elements should be included. | - |
Return: A new list containing elements from the start up to the specified index.
Example:
assert [10, 20, 30, 40, 50].to(3) == [10, 20, 30]
assert [10, 20, 30, 40, 50].to(-2) == [10, 20, 30, 40]
List.unique
List.unique : func(list: [T] -> {T})
Returns a set of the unique elements of the list.
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to process. | - |
Return: A set of the unique elements from the list.
Example:
assert [10, 20, 10, 10, 30].unique() == {10, 20, 30}
List.where
List.where : func(list: [T], predicate: func(item:&T -> Bool) -> Int)
Find the index of the first item that matches a predicate function (if any).
| Argument | Type | Description | Default |
|---|---|---|---|
| list | [T] |
The list to search through. | - |
| predicate | func(item:&T -> Bool) |
A function that returns yes if the item's index should be returned or no if it should not. |
- |
Return: Returns the index of the first item where the predicate is true or none if no item matches.
Example:
assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("A")) == 2
assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("X")) == none
1 % API3 # Builtins5 # List6 ## List.binary_search9 List.binary_search : func(list: [T], by: func(x,y:&T->Int32) = T.compare -> Int)10 ```12 Performs a binary search on a sorted list.14 Argument | Type | Description | Default15 ---------|------|-------------|---------17 by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`19 **Return:** Assuming the input list is sorted according to the given comparison function, return the index where the given item would be inserted to maintain the sorted order. That is, if the item is found, return its index, otherwise return the place where it would be found if it were inserted and the list were sorted.22 **Example:**24 assert [1, 3, 5, 7, 9].binary_search(5) == 325 assert [1, 3, 5, 7, 9].binary_search(-999) == 126 assert [1, 3, 5, 7, 9].binary_search(999) == 628 ```29 ## List.by32 List.by : func(list: [T], step: Int -> [T])33 ```35 Creates a new list with elements spaced by the specified step value.37 Argument | Type | Description | Default38 ---------|------|-------------|---------45 **Example:**47 assert [1, 2, 3, 4, 5, 6].by(2) == [1, 3, 5]49 ```50 ## List.clear53 List.clear : func(list: @[T] -> Void)54 ```56 Clears all elements from the list.58 Argument | Type | Description | Default59 ---------|------|-------------|---------65 **Example:**67 list := &[10, 20]68 list.clear()69 assert list[] == []71 ```72 ## List.counts75 List.counts : func(list: [T] -> {T=Int})76 ```78 Counts the occurrences of each element in the list.80 Argument | Type | Description | Default81 ---------|------|-------------|---------87 **Example:**89 assert [10, 20, 30, 30, 30].counts() == {10:1, 20:1, 30:3}91 ```92 ## List.find95 List.find : func(list: [T], target: T -> Int?)96 ```98 Finds the index of the first occurrence of an element (if any).100 Argument | Type | Description | Default101 ---------|------|-------------|---------108 **Example:**110 assert [10, 20, 30, 40, 50].find(20) == 2111 assert [10, 20, 30, 40, 50].find(9999) == none113 ```114 ## List.from117 List.from : func(list: [T], first: Int -> [T])118 ```120 Returns a slice of the list starting from a specified index.122 Argument | Type | Description | Default123 ---------|------|-------------|---------130 **Example:**132 assert [10, 20, 30, 40, 50].from(3) == [30, 40, 50]134 ```135 ## List.has138 List.has : func(list: [T], target: T -> Bool)139 ```141 Checks if the list has an element.143 Argument | Type | Description | Default144 ---------|------|-------------|---------151 **Example:**153 assert [10, 20, 30].has(20) == yes155 ```156 ## List.heap_pop159 List.heap_pop : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> T?)160 ```162 Removes and returns the top element of a heap or `none` if the list is empty. By default, this is the *minimum* value in the heap.164 Argument | Type | Description | Default165 ---------|------|-------------|---------167 by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`172 **Example:**174 my_heap := &[30, 10, 20]175 my_heap.heapify()176 assert my_heap.heap_pop() == 10178 ```179 ## List.heap_push182 List.heap_push : func(list: @[T], item: T, by = T.compare -> Void)183 ```185 Adds an element to the heap and maintains the heap property. By default, this is a *minimum* heap.187 Argument | Type | Description | Default188 ---------|------|-------------|---------191 by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`196 **Example:**198 my_heap : &[Int]199 my_heap.heap_push(10)200 assert my_heap.heap_pop() == 10202 ```203 ## List.heapify206 List.heapify : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> Void)207 ```209 Converts a list into a heap.211 Argument | Type | Description | Default212 ---------|------|-------------|---------214 by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`219 **Example:**221 my_heap := &[30, 10, 20]222 my_heap.heapify()224 ```225 ## List.insert228 List.insert : func(list: @[T], item: T, at: Int = 0 -> Void)229 ```231 Inserts an element at a specified position in the list.233 Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".235 Argument | Type | Description | Default236 ---------|------|-------------|---------244 **Example:**246 list := &[10, 20]247 list.insert(30)248 assert list == [10, 20, 30]250 list.insert(999, at=2)251 assert list == [10, 999, 20, 30]253 ```254 ## List.insert_all257 List.insert_all : func(list: @[T], items: [T], at: Int = 0 -> Void)258 ```260 Inserts a list of items at a specified position in the list.262 Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".264 Argument | Type | Description | Default265 ---------|------|-------------|---------273 **Example:**275 list := &[10, 20]276 list.insert_all([30, 40])277 assert list == [10, 20, 30, 40]279 list.insert_all([99, 100], at=2)280 assert list == [10, 99, 100, 20, 30, 40]282 ```283 ## List.pop286 List.pop : func(list: &[T], index: Int = -1 -> T?)287 ```289 Removes and returns an item from the list. If the given index is present in the list, the item at that index will be removed and the list will become one element shorter.291 Since negative indices are counted from the back, the default behavior is to pop the last value.293 Argument | Type | Description | Default294 ---------|------|-------------|---------298 **Return:** `none` if the list is empty or the given index does not exist in the list, otherwise the item at the given index.301 **Example:**303 list := &[10, 20, 30, 40]305 assert list.pop() == 40306 assert list[] == [10, 20, 30]308 assert list.pop(index=2) == 20309 assert list[] == [10, 30]311 ```312 ## List.random315 List.random : func(list: [T], random: func(min,max:Int64->Int64)? = none -> T?)316 ```318 Selects a random element from the list.320 Argument | Type | Description | Default321 ---------|------|-------------|---------323 random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`328 **Example:**330 nums := [10, 20, 30]331 pick := nums.random()!332 assert nums.has(pick)333 empty : [Int]334 assert empty.random() == none336 ```337 ## List.remove_at340 List.remove_at : func(list: @[T], at: Int = -1, count: Int = 1 -> Void)341 ```343 Removes elements from the list starting at a specified index.345 Since negative indices are counted from the back, the default behavior is to remove the last item.347 Argument | Type | Description | Default348 ---------|------|-------------|---------356 **Example:**358 list := &[10, 20, 30, 40, 50]359 list.remove_at(2)360 assert list == [10, 30, 40, 50]362 list.remove_at(2, count=2)363 assert list == [10, 50]365 ```366 ## List.remove_item369 List.remove_item : func(list: @[T], item: T, max_count: Int = -1 -> Void)370 ```372 Removes all occurrences of a specified item from the list.376 Argument | Type | Description | Default377 ---------|------|-------------|---------385 **Example:**387 list := &[10, 20, 10, 20, 30]388 list.remove_item(10)389 assert list == [20, 20, 30]391 list.remove_item(20, max_count=1)392 assert list == [20, 30]394 ```395 ## List.reversed398 List.reversed : func(list: [T] -> [T])399 ```401 Returns a reversed slice of the list.403 Argument | Type | Description | Default404 ---------|------|-------------|---------410 **Example:**412 assert [10, 20, 30].reversed() == [30, 20, 10]414 ```415 ## List.sample418 List.sample : func(list: [T], count: Int, weights: [Num]? = none, random: func(->Num)? = none -> [T])419 ```421 Selects a sample of elements from the list, optionally with weighted probabilities.423 Errors will be raised if any of the following conditions occurs: - The given list has no elements and `count >= 1` - `count < 0` (negative count) - The number of weights provided doesn't match the length of the list. - Any weight in the weights list is negative, infinite, or `NaN` - The sum of the given weights is zero (zero probability for every element).425 Argument | Type | Description | Default426 ---------|------|-------------|---------429 weights | `[Num]?` | The probability weights for each element in the list. These values do not need to add up to any particular number, they are relative weights. If no weights are given, elements will be sampled with uniform probability. | `none`430 random | `func(->Num)?` | If provided, this function will be used to get random values for sampling the list. The provided function should return random numbers between `0.0` (inclusive) and `1.0` (exclusive). (Used for deterministic pseudorandom number generation) | `none`435 **Example:**437 _ := [10, 20, 30].sample(2, weights=[90%, 5%, 5%]) # E.g. [10, 10]439 ```440 ## List.shuffle443 List.shuffle : func(list: @[T], random: func(min,max:Int64->Int64)? = none -> Void)444 ```446 Shuffles the elements of the list in place.448 Argument | Type | Description | Default449 ---------|------|-------------|---------451 random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`456 **Example:**458 nums := &[10, 20, 30, 40]459 nums.shuffle()460 # E.g. [20, 40, 10, 30]462 ```463 ## List.shuffled466 List.shuffled : func(list: [T], random: func(min,max:Int64->Int64)? = none -> [T])467 ```469 Creates a new list with elements shuffled.471 Argument | Type | Description | Default472 ---------|------|-------------|---------474 random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`479 **Example:**481 nums := [10, 20, 30, 40]482 _ := nums.shuffled()483 # E.g. [20, 40, 10, 30]485 ```486 ## List.slice489 List.slice : func(list: [T], from: Int, to: Int -> [T])490 ```492 Returns a slice of the list spanning the given indices (inclusive).494 Argument | Type | Description | Default495 ---------|------|-------------|---------500 **Return:** A new list spanning the given indices. Note: negative indices are counted from the back of the list, so `-1` refers to the last element, `-2` the second-to-last, and so on.503 **Example:**505 assert [10, 20, 30, 40, 50].slice(2, 4) == [20, 30, 40]506 assert [10, 20, 30, 40, 50].slice(-3, -2) == [30, 40]508 ```509 ## List.sort512 List.sort : func(list: @[T], by = T.compare -> Void)513 ```515 Sorts the elements of the list in place in ascending order (small to large).517 Argument | Type | Description | Default518 ---------|------|-------------|---------520 by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`525 **Example:**527 list := &[40, 10, -30, 20]528 list.sort()529 assert list == [-30, 10, 20, 40]531 list.sort(func(a,b:&Int) a.abs() <> b.abs())532 assert list == [10, 20, -30, 40]534 ```535 ## List.sorted538 List.sorted : func(list: [T], by = T.compare -> [T])539 ```541 Creates a new list with elements sorted.543 Argument | Type | Description | Default544 ---------|------|-------------|---------546 by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`551 **Example:**553 assert [40, 10, -30, 20].sorted() == [-30, 10, 20, 40]554 assert [40, 10, -30, 20].sorted(555 func(a,b:&Int) a.abs() <> b.abs()556 ) == [10, 20, -30, 40]558 ```559 ## List.to562 List.to : func(list: [T], last: Int -> [T])563 ```565 Returns a slice of the list from the start of the original list up to a specified index (inclusive).567 Argument | Type | Description | Default568 ---------|------|-------------|---------575 **Example:**577 assert [10, 20, 30, 40, 50].to(3) == [10, 20, 30]578 assert [10, 20, 30, 40, 50].to(-2) == [10, 20, 30, 40]580 ```581 ## List.unique584 List.unique : func(list: [T] -> {T})585 ```587 Returns a set of the unique elements of the list.589 Argument | Type | Description | Default590 ---------|------|-------------|---------596 **Example:**598 assert [10, 20, 10, 10, 30].unique() == {10, 20, 30}600 ```601 ## List.where604 List.where : func(list: [T], predicate: func(item:&T -> Bool) -> Int)605 ```607 Find the index of the first item that matches a predicate function (if any).609 Argument | Type | Description | Default610 ---------|------|-------------|---------612 predicate | `func(item:&T -> Bool)` | A function that returns `yes` if the item's index should be returned or `no` if it should not. | -614 **Return:** Returns the index of the first item where the predicate is true or `none` if no item matches.617 **Example:**619 assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("A")) == 2620 assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("X")) == none622 ```