Tuples
A tuple, tup
, is an immutable array of values.
-
$tup() -> tup
$tup(arg1: any, arg2: any, ...) -> tup
-
Creates a new
tup
object. The arguments provide the tuple's values.
Note that trailing commas are allowed in function calls, e.g
var tup = $tup( "foo", "bar", "baz", ); assert tup == $tup("foo", "bar", "baz");
Equality
Tuples compare as equal using the ==
operator if they have the same length and their elements are equal, e.g.
var foo = $tup("foo", 123); var bar = $tup("foo", 123); assert foo == bar;
Comparison
You can compare tuples using the comparison operators, <
, <=
, >
, >=
, e.g.
assert $tup(1, 2, 3) < $tup(1, 2, 4);
Tuples are compared lexicographically by element. A comparison will panic if the elements are not comparable.
Indexing
You can index into a tuple to get (but not set) entries, e.g.
var tup = $tup("foo", "bar"); assert tup[0] == "foo"; assert tup[1] == "bar";
Indices are zero-based. Indexing is equivalent to using the tuple's :get()
method, e.g.
var tup = $tup("foo", "bar"); assert tup:get(0) == "foo"; assert tup:get(1) == "bar";
Iterating
Tuples are iterable, e.g.
for item in $tup(123, 456, 789) { echo item; }
Containment
You can check if a tuple contains an item using the in
operator, e.g.
if 123 in $tup(123, 456, 789) { echo "found"; }
This is equivalent to calling the tuple's :contains()
method.
Methods
-
:contains(value: any) -> bool
-
Returns
true
if the tuple contains an item equal tovalue
, otherwisefalse
. -
:count() -> i64
-
Returns the number of items in the tuple.
-
:get(index: i64) -> any
-
Returns the value at
index
. Will panic ifindex
is out of range or not an integer. -
:iter() -> iter
-
Returns an
iter
object wrapping the tuple. -
:slice(start_index: i64) -> tup
:slice(start_index: i64, length: i64) -> tup
-
Copies a slice of the source tuple and returns it as a new tuple.
If
start_index
is negative, counts backwards from the end of the tuple — i.e. astart_index
of-1
refers to to the last item in the tuple.If
length
is omitted, copies to the end of the source tuple.Panics if either argument is out of range.