#slices
Xu 0.16.0 API
import: #slices
counts: 62 functions, 0 methods, 1 types, 0 constants, 0 method sets
Algorithms for searching, comparing, and rearranging slices.
Built-in slice methods · Xu 0.14.1
The methods below belong to mutable dynamic slices and are available since Xu 0.14.1. Call them directly on a mutable slice or through a writable borrow.
slice.reserve(n int)
Ensures capacity for at least n elements while preserving the length and existing values. n is the total requested capacity. Pass a nonnegative count.
slice.resize_zero(n int)
Sets the slice length to n. Growth adds zero values and preserves the existing prefix. Shrinking keeps the capacity and the prefix of length n. A negative n leaves the slice as it was. Available for integer, floating-point, and boolean element types. See the local inference guide for related APIs.
contains_int
pub fn contains_int(xs []int, v int) bool
Purpose
Check for an element equal to the target, without requiring sorted input.
Parameters
xs []int- Elements to process.
v int- Element to find or insert.
Returns
true if a match exists; false for no match or an empty slice.
index_of_int
pub fn index_of_int(xs []int, v int) int
Purpose
Return the first position equal to the target.
Parameters
xs []int- Elements to process.
v int- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
last_index_int
pub fn last_index_int(xs &[]int, v int) int
Purpose
Return the last position equal to the target.
Parameters
xs &[]int- Elements to process.
v int- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
last_index_str
pub fn last_index_str(xs &[]string, v string) int
Purpose
Return the last position equal to the target.
Parameters
xs &[]string- Elements to process.
v string- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
sum_int
pub fn sum_int(xs []int) int
Purpose
Add all integers in a slice.
Parameters
xs []int- Elements to process.
Returns
The sum, or 0 for an empty slice.
contains_str
pub fn contains_str(xs []string, v string) bool
Purpose
Check for an element equal to the target, without requiring sorted input.
Parameters
xs []string- Elements to process.
v string- Element to find or insert.
Returns
true if a match exists; false for no match or an empty slice.
index_of_str
pub fn index_of_str(xs []string, v string) int
Purpose
Return the first position equal to the target.
Parameters
xs []string- Elements to process.
v string- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
equal_ints
pub fn equal_ints(a &[]int, b &[]int) bool
Purpose
Compare slice lengths and corresponding elements in order.
Parameters
a &[]int- Slice to process.
b &[]int- Second slice.
Returns
true only when lengths and all corresponding elements are equal; two empty slices are equal.
equal_strs
pub fn equal_strs(a &[]string, b &[]string) bool
Purpose
Compare slice lengths and corresponding elements in order.
Parameters
a &[]string- Slice to process.
b &[]string- Second slice.
Returns
true only when lengths and all corresponding elements are equal; two empty slices are equal.
reverse_ints
pub fn reverse_ints(a $&[]int)
Purpose
Reverse element order in place without comparing or sorting values.
Parameters
a $&[]int- Slice to process.
Returns
No new slice is returned; the result is stored in a.
reverse_strs
pub fn reverse_strs(a $&[]string)
Purpose
Reverse element order in place without comparing or sorting values.
Parameters
a $&[]string- Slice to process.
Returns
No new slice is returned; the result is stored in a.
clone_ints
pub fn clone_ints(xs &[]int) []int
Purpose
Copy elements into an independent slice container.
Parameters
xs &[]int- Elements to process.
Returns
A new slice with the same contents and order, or an empty slice for empty input.
Usage notes
- Elements in the new slice can be changed independently of the original slice.
clone_strs
pub fn clone_strs(xs &[]string) []string
Purpose
Copy elements into an independent slice container.
Parameters
xs &[]string- Elements to process.
Returns
A new slice with the same contents and order, or an empty slice for empty input.
Usage notes
- Elements in the new slice can be changed independently of the original slice.
compare_ints
pub fn compare_ints(a &[]int, b &[]int) int
Purpose
Compare lexicographically, using the first differing element and then length when the common prefix is equal.
Parameters
a &[]int- Slice to process.
b &[]int- Second slice.
Returns
-1 when the left slice precedes the right, 0 when equal, and 1 when it follows.
compare_strs
pub fn compare_strs(a &[]string, b &[]string) int
Purpose
Compare lexicographically, using the first differing element and then length when the common prefix is equal.
Parameters
a &[]string- Slice to process.
b &[]string- Second slice.
Returns
-1 when the left slice precedes the right, 0 when equal, and 1 when it follows.
compact_ints
pub fn compact_ints(xs &[]int) []int
Purpose
Collapse adjacent equal elements, retaining one from each run; equal values separated by another value remain.
Parameters
xs &[]int- Elements to process.
Returns
A new slice preserving order, without modifying the original slice.
Usage notes
- For example,
[1, 1, 2, 1] becomes [1, 2, 1]; this is not global deduplication.
compact_strs
pub fn compact_strs(xs &[]string) []string
Purpose
Collapse adjacent equal elements, retaining one from each run; equal values separated by another value remain.
Parameters
xs &[]string- Elements to process.
Returns
A new slice preserving order, without modifying the original slice.
Usage notes
- For example,
[1, 1, 2, 1] becomes [1, 2, 1]; this is not global deduplication.
insert_int
pub fn insert_int(xs &[]int, i int, v int) []int
Purpose
Insert one element before the requested position, preserving other elements in order.
Parameters
xs &[]int- Elements to process.
i int- Starting position, using a zero-based element index.
v int- Element to find or insert.
Returns
A new slice with the inserted element; the original is unchanged.
Usage notes
- An index below zero inserts at the beginning; an index above
xs.len appends at the end.
insert_str
pub fn insert_str(xs &[]string, i int, v string) []string
Purpose
Insert one element before the requested position, preserving other elements in order.
Parameters
xs &[]string- Elements to process.
i int- Starting position, using a zero-based element index.
v string- Element to find or insert.
Returns
A new slice with the inserted element; the original is unchanged.
Usage notes
- An index below zero inserts at the beginning; an index above
xs.len appends at the end.
delete_ints
pub fn delete_ints(xs &[]int, i int, j int) []int
Purpose
Remove elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]int- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
delete_strs
pub fn delete_strs(xs &[]string, i int, j int) []string
Purpose
Remove elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]string- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
min_int
pub fn min_int(xs &[]int) int
Purpose
Find the minimum element without changing slice order.
Parameters
xs &[]int- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
max_int
pub fn max_int(xs &[]int) int
Purpose
Find the maximum element without changing slice order.
Parameters
xs &[]int- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
contains_f64
pub fn contains_f64(xs &[]f64, v f64) bool
Purpose
Check for an element equal to the target, without requiring sorted input.
Parameters
xs &[]f64- Elements to process.
v f64- Element to find or insert.
Returns
true if a match exists; false for no match or an empty slice.
index_of_f64
pub fn index_of_f64(xs &[]f64, v f64) int
Purpose
Return the first position equal to the target.
Parameters
xs &[]f64- Elements to process.
v f64- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
equal_f64s
pub fn equal_f64s(a &[]f64, b &[]f64) bool
Purpose
Compare slice lengths and corresponding elements in order.
Parameters
a &[]f64- Slice to process.
b &[]f64- Second slice.
Returns
true only when lengths and all corresponding elements are equal; two empty slices are equal.
clone_f64s
pub fn clone_f64s(xs &[]f64) []f64
Purpose
Copy elements into an independent slice container.
Parameters
xs &[]f64- Elements to process.
Returns
A new slice with the same contents and order, or an empty slice for empty input.
Usage notes
- Elements in the new slice can be changed independently of the original slice.
reverse_f64s
pub fn reverse_f64s(a $&[]f64)
Purpose
Reverse element order in place without comparing or sorting values.
Parameters
a $&[]f64- Slice to process.
Returns
No new slice is returned; the result is stored in a.
min_f64
pub fn min_f64(xs &[]f64) f64
Purpose
Find the minimum element without changing slice order.
Parameters
xs &[]f64- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
max_f64
pub fn max_f64(xs &[]f64) f64
Purpose
Find the maximum element without changing slice order.
Parameters
xs &[]f64- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
compare_f64s
pub fn compare_f64s(a &[]f64, b &[]f64) int
Purpose
Compare lexicographically, using the first differing element and then length when the common prefix is equal.
Parameters
a &[]f64- Slice to process.
b &[]f64- Second slice.
Returns
-1 when the left slice precedes the right, 0 when equal, and 1 when it follows.
compact_f64s
pub fn compact_f64s(xs &[]f64) []f64
Purpose
Collapse adjacent equal elements, retaining one from each run; equal values separated by another value remain.
Parameters
xs &[]f64- Elements to process.
Returns
A new slice preserving order, without modifying the original slice.
Usage notes
- For example,
[1, 1, 2, 1] becomes [1, 2, 1]; this is not global deduplication.
insert_f64
pub fn insert_f64(xs &[]f64, i int, v f64) []f64
Purpose
Insert one element before the requested position, preserving other elements in order.
Parameters
xs &[]f64- Elements to process.
i int- Starting position, using a zero-based element index.
v f64- Element to find or insert.
Returns
A new slice with the inserted element; the original is unchanged.
Usage notes
- An index below zero inserts at the beginning; an index above
xs.len appends at the end.
delete_f64s
pub fn delete_f64s(xs &[]f64, i int, j int) []f64
Purpose
Remove elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]f64- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
struct SearchResult
pub struct SearchResult {
pub index int
pub found bool
}
Purpose
Structure encapsulating index, found.
Public fields
index int- field (int)
found bool- field (bool)
binary_search_ints
pub fn binary_search_ints(xs &[]int, v int) SearchResult
Purpose
Binary-search an ascending slice, returning both a match flag and an insertion position.
Parameters
xs &[]int- Elements to process.
v int- Element to find or insert.
Returns
SearchResult.index is the first position not less than the target, in 0..xs.len. Treat it as an existing element only when found is true.
Usage notes
- Sort ascending with the same ordering first. An absent value does not return
-1; an empty slice yields index = 0, found = false.
Call example
Find a duplicate and a missing value
@main
#slices
fn main() {
values := [1, 3, 3, 7]
hit := slices.binary_search_ints(&values, 3)
println(hit.index)
println(hit.found)
absent := slices.binary_search_ints(&values, 4)
println(absent.index)
println(absent.found)
}
Expected output:
1
true
3
false
binary_search_strs
pub fn binary_search_strs(xs &[]string, v string) SearchResult
Purpose
Binary-search an ascending slice, returning both a match flag and an insertion position.
Parameters
xs &[]string- Elements to process.
v string- Element to find or insert.
Returns
SearchResult.index is the first position not less than the target, in 0..xs.len. Treat it as an existing element only when found is true.
Usage notes
- Sort ascending with the same ordering first. An absent value does not return
-1; an empty slice yields index = 0, found = false.
binary_search_f64s
pub fn binary_search_f64s(xs &[]f64, v f64) SearchResult
Purpose
Binary-search an ascending slice, returning both a match flag and an insertion position.
Parameters
xs &[]f64- Elements to process.
v f64- Element to find or insert.
Returns
SearchResult.index is the first position not less than the target, in 0..xs.len. Treat it as an existing element only when found is true.
Usage notes
- Sort ascending with the same ordering first. An absent value does not return
-1; an empty slice yields index = 0, found = false.
replace_ints
pub fn replace_ints(xs &[]int, i int, j int, add &[]int) []int
Purpose
Replace elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]int- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
add &[]int- Elements to insert in place of the selected range.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
replace_strs
pub fn replace_strs(xs &[]string, i int, j int, add &[]string) []string
Purpose
Replace elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]string- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
add &[]string- Elements to insert in place of the selected range.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
concat_ints
pub fn concat_ints(a &[]int, b &[]int) []int
Purpose
Concatenate two slices, placing all elements of a before all elements of b.
Parameters
a &[]int- Slice to process.
b &[]int- Second slice.
Returns
A new concatenated slice; equal elements are not merged.
repeat_ints
pub fn repeat_ints(xs &[]int, n int) []int
Purpose
Repeat the whole slice in order rather than repeating each element individually.
Parameters
xs &[]int- Elements to process.
n int- Repeat count, which must be zero or positive.
Returns
A new repeated slice, empty for n = 0 or empty input.
Error handling
- A negative
n throws slices: negative Repeat count.
repeat_strs
pub fn repeat_strs(xs &[]string, n int) []string
Purpose
Repeat the whole slice in order rather than repeating each element individually.
Parameters
xs &[]string- Elements to process.
n int- Repeat count, which must be zero or positive.
Returns
A new repeated slice, empty for n = 0 or empty input.
Error handling
- A negative
n throws slices: negative Repeat count.
index_func_ints
pub fn index_func_ints(xs &[]int, f fn(int) bool) int
Purpose
Return the first position accepted by the predicate.
Parameters
xs &[]int- Elements to process.
f fn(int) bool- Receives a slice element and returns
true to select it.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
contains_func_ints
pub fn contains_func_ints(xs &[]int, f fn(int) bool) bool
Purpose
Check for an element accepted by the predicate.
Parameters
xs &[]int- Elements to process.
f fn(int) bool- Receives a slice element and returns
true to select it.
Returns
true if a match exists; false for no match or an empty slice.
index_func_strs
pub fn index_func_strs(xs &[]string, f fn(string) bool) int
Purpose
Return the first position accepted by the predicate.
Parameters
xs &[]string- Elements to process.
f fn(string) bool- Receives a slice element and returns
true to select it.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
concat_strs
pub fn concat_strs(a &[]string, b &[]string) []string
Purpose
Concatenate two slices, placing all elements of a before all elements of b.
Parameters
a &[]string- Slice to process.
b &[]string- Second slice.
Returns
A new concatenated slice; equal elements are not merged.
sort_ints_func
pub fn sort_ints_func(a $&[]int, less fn(int, int) bool)
Purpose
Sort in place using the supplied comparison; clone the slice first when its original order must be retained.
Parameters
a $&[]int- Slice to process.
less fn(int, int) bool- Compare two elements; return
true when the first belongs before the second, and false for equal elements.
Returns
No return value; the sorted elements remain in a.
sort_strs_func
pub fn sort_strs_func(a $&[]string, less fn(string, string) bool)
Purpose
Sort in place using the supplied comparison; clone the slice first when its original order must be retained.
Parameters
a $&[]string- Slice to process.
less fn(string, string) bool- Compare two elements; return
true when the first belongs before the second, and false for equal elements.
Returns
No return value; the sorted elements remain in a.
last_index_func_ints
pub fn last_index_func_ints(xs &[]int, f fn(int) bool) int
Purpose
Return the last position accepted by the predicate.
Parameters
xs &[]int- Elements to process.
f fn(int) bool- Receives a slice element and returns
true to select it.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
last_index_func_strs
pub fn last_index_func_strs(xs &[]string, f fn(string) bool) int
Purpose
Return the last position accepted by the predicate.
Parameters
xs &[]string- Elements to process.
f fn(string) bool- Receives a slice element and returns
true to select it.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
min_str
pub fn min_str(xs &[]string) string
Purpose
Find the minimum element without changing slice order.
Parameters
xs &[]string- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
max_str
pub fn max_str(xs &[]string) string
Purpose
Find the maximum element without changing slice order.
Parameters
xs &[]string- Elements to process.
Returns
The selected element value.
Error handling
- An empty slice throws
slices: empty; check its length or handle the error.
is_sorted_ints
pub fn is_sorted_ints(xs &[]int) bool
Purpose
Check whether adjacent elements are already in nondecreasing order without modifying the slice.
Parameters
xs &[]int- Elements to process.
Returns
true when sorted; empty and single-element slices also return true.
is_sorted_strs
pub fn is_sorted_strs(xs &[]string) bool
Purpose
Check whether adjacent elements are already in nondecreasing order without modifying the slice.
Parameters
xs &[]string- Elements to process.
Returns
true when sorted; empty and single-element slices also return true.
last_index_f64
pub fn last_index_f64(xs &[]f64, v f64) int
Purpose
Return the last position equal to the target.
Parameters
xs &[]f64- Elements to process.
v f64- Element to find or insert.
Returns
Zero-based element index, or -1 if absent or the slice is empty.
concat_f64s
pub fn concat_f64s(a &[]f64, b &[]f64) []f64
Purpose
Concatenate two slices, placing all elements of a before all elements of b.
Parameters
a &[]f64- Slice to process.
b &[]f64- Second slice.
Returns
A new concatenated slice; equal elements are not merged.
replace_f64s
pub fn replace_f64s(xs &[]f64, i int, j int, add &[]f64) []f64
Purpose
Replace elements in the half-open range [i, j), preserving the remaining order.
Parameters
xs &[]f64- Elements to process.
i int- Starting position, using a zero-based element index.
j int- End position, excluding the element at that index.
add &[]f64- Elements to insert in place of the selected range.
Returns
A new slice; the original slice is unchanged.
Usage notes
- Pass
0 <= i <= j <= xs.len. This is not a complete bounds-normalization interface; for example, i > xs.len can still index out of bounds.
repeat_f64s
pub fn repeat_f64s(xs &[]f64, n int) []f64
Purpose
Repeat the whole slice in order rather than repeating each element individually.
Parameters
xs &[]f64- Elements to process.
n int- Repeat count, which must be zero or positive.
Returns
A new repeated slice, empty for n = 0 or empty input.
Error handling
- A negative
n throws slices: negative Repeat count.
is_sorted_f64s
pub fn is_sorted_f64s(xs &[]f64) bool
Purpose
Check whether adjacent elements are already in nondecreasing order without modifying the slice.
Parameters
xs &[]f64- Elements to process.
Returns
true when sorted; empty and single-element slices also return true.
contains_func_strs
pub fn contains_func_strs(xs &[]string, f fn(string) bool) bool
Purpose
Check for an element accepted by the predicate.
Parameters
xs &[]string- Elements to process.
f fn(string) bool- Receives a slice element and returns
true to select it.
Returns
true if a match exists; false for no match or an empty slice.
compact_func_ints
pub fn compact_func_ints(xs &[]int, eq fn(int, int) bool) []int
Purpose
Collapse adjacent equal elements, retaining one from each run; equal values separated by another value remain.
Parameters
xs &[]int- Elements to process.
eq fn(int, int) bool- Compare two elements, returning
true when they should be treated as equal.
Returns
A new slice preserving order, without modifying the original slice.
Usage notes
- For example,
[1, 1, 2, 1] becomes [1, 2, 1]; this is not global deduplication.
delete_func_ints
pub fn delete_func_ints(xs &[]int, f fn(int) bool) []int
Purpose
Remove elements for which the predicate returns true, preserving the order of the rest.
Parameters
xs &[]int- Elements to process.
f fn(int) bool- Receives a slice element and returns
true to select it.
Returns
A new filtered slice, empty when every element matches.