Sequential
Associated Functions for sequential collections
Built-in associated functions for Sequential Collections.
These associated functions are available on all sequential collection types including Vec, arrays, and slice views.
They may be semantically different, but they all share the memory layout:
| capacity | length | elements …. |
Indexing
Sequential collections support index-based element access using the subscript operator [].
Reading
value := sequential_instance[index]
Access an element at the specified index (zero-based).
Example
numbers := [10, 20, 30, 40]
second := numbers[1] // 20
Writing
sequential_instance[index] = value
Assign a new value to the element at the specified index.
Example
mut numbers := [10, 20, 30, 40]
numbers[2] = 99 // [10, 20, 99, 40]
Basic Operations
len
fn len(self) -> Int
Returns the number of elements in the collection.
is_empty
fn is_empty(self) -> Bool
Returns true if the collection contains no elements.
capacity
fn capacity(self) -> Int
Returns the maximum number of elements the collection can hold.
clear
fn clear(mut self)
Sets the length to zero
Element Access
first
fn first(self) -> T
Returns the first element in the collection. Traps if empty.
last
fn last(self) -> T
Returns the last element in the collection. Traps if empty.
Reordering
swap
fn swap(self, index_a: Int, index_b: Int)
Swaps the elements at the two specified indices.
swap_remove
fn swap_remove(mut self, index: Int)
Removes an element by swapping it with the last element and then removing it.
rev
fn rev(self) -> [T]
Returns a reversed view of the collection. The return collection must be a storage.
sort
fn sort(mut self)
Sorts the elements in ascending order (for comparable types).
Note: Not implemented yet.
Example
mut numbers := [3, 1, 4, 1, 5]
numbers.sort() // [1, 1, 3, 4, 5]
sort_by
fn sort_by(mut self, lambda: (T, T) -> Int)
Sorts the elements using a custom comparison function. The lambda should return:
- Negative value if first < second
- Zero if first == second
- Positive value if first > second
Note: Not implemented yet.
Example
mut numbers := [3, 1, 4, 1, 5]
numbers.sort_by(|a, b| b - a) // [5, 4, 3, 1, 1] (descending)
shuffle_index
fn shuffle_index(mut self, seed: Int, count: Int)
Shuffles indices in the collection (only available for Int slices).
Iteration
for
fn for(self, lambda: (T) -> ())
fn for(self, lambda: (Int, T) -> ())
Iterates over elements, calling the lambda for each element.
while
fn while(self, lambda: (T) -> Bool)
fn while(self, lambda: (Int, T) -> Bool)
Iterates while the lambda returns true.
Transforming
map
fn map<U>(self, lambda: (T) -> U) -> [U]
Aggregation
fold
fn fold<U>(self, initial: U, lambda: (U, T) -> U) -> U
Combines all elements into a single value using an accumulator function. Also known as reduce.
Note: Not implemented yet.
Example
numbers := [1, 2, 3, 4]
sum := numbers.fold(0, |acc, x| acc + x) // 10
words := ["hello", "world"]
sentence := words.fold("", |acc, word| acc + " " + word) // " hello world"
Transforms each element to a new value using the provided lambda function.
Note: Not implemented yet.
Example
numbers := [1, 2, 3, 4]
doubled := numbers.map(|x| x * 2) // [2, 4, 6, 8]
Filtering & Searching
filter
fn filter(self, lambda: (T) -> Bool) -> [T]
Returns a new slice containing only elements that match the predicate.
filter_mut
fn filter_mut(mut self, lambda: (T) -> Bool)
Removes elements that don’t match the predicate in-place.
find
fn find(self, lambda: (T) -> Bool) -> T?
Returns the first element that matches the predicate.
contains
fn contains(self, value: T) -> Bool
Returns true if the collection contains the specified value.
Note: Not implemented yet.
Example
numbers := [1, 2, 3, 4]
has_three := numbers.contains(3) // true
has_five := numbers.contains(5) // false
all
fn all(self, lambda: (T) -> Bool) -> Bool
Returns true if all elements match the predicate.
any
fn any(self, lambda: (T) -> Bool) -> Bool
Returns true if any element matches the predicate.
Index Operations
copy_from_index
fn copy_from_index(self, indices: [Int]) -> [T]
Creates a new slice by copying elements at the specified indices.