types.Array.slice
Method on every
arrayCopies the elements from start up to, but not including, end.
The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing
... from the prose below, not from the signature.let squad = ["scout", "medic", "sniper", "engineer"];
let frontline = squad.slice(1, 3);
println("frontline =", ", ".join(frontline));Output:
frontline = medic, sniperParameters
start | number | first index to copy; 0 when left out optional |
end | number | index to stop before; len() when left out optional |
... | any | repeats |
Return value
A new array holding the copied elements, empty if the range is empty.
Notes
Despite the trailing ... in the signature above, slice is not variadic: it takes zero, one or two real arguments, and a third is silently ignored rather than erroring, unlike findvalue. A negative start or end counts back from the end (-1 is the last element), and the range is clamped into [0, len()] rather than throwing when it runs past either end.
Example
let a = [0, 1, 2, 3, 4];
println("a.slice().len() =", a.slice().len()); // no arguments: a full copy
println("a.slice(2).len() =", a.slice(2).len()); // one argument: from start to the end
println("a.slice(-2, -1) =", ", ".join(a.slice(-2, -1).map(@(v) v.tostring()))); // negative indices count from the end
println("a.slice(1, 3, 999).len() =", a.slice(1, 3, 999).len()); // a third argument is silently ignoredOutput:
a.slice().len() = 5
a.slice(2).len() = 3
a.slice(-2, -1) = 3
a.slice(1, 3, 999).len() = 2See also
resize | Grows or shrinks the array to size elements. |
slice | Returns the bytes of str from start up to, but not including, end. |
| array | class index |