Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.17.7

Tuples



A tuple, tup, is an immutable array of values.

$tup(*args: any) -> tup

Creates a new tuple. The arguments provide the tuple's values.

Tuple Literals

You can create a tuple using literal syntax, e.g.

var tup = ("foo", "bar", "baz");

Trailing commas are allowed, e.g.

var tup = (
    "foo",
    "bar",
    "baz",
);

The empty literal () will create an empty tuple.

Note that parentheses containing a single expression will be parsed as grouping parentheses, not as a tuple literal, e.g.

assert (123) == 123;
assert (1 + 2) == 3;

To create a single-valued tuple, either use the $tup() function or add a trailing comma after the expression, e.g.

var tup1 = $tup(123);
var tup2 = (123,);
var tup3 = (1 + 2,);

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;

Comparisons

You can compare tuples using the comparison operators, <, <=, >, >=, e.g.

assert (1, 2, 3) < (1, 2, 4);

Tuples are compared lexicographically by element, e.g.

assert (1, 1) < (1, 1, 1);
assert (1, 1, 1) < (1, 1, 1, 1)

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 = ("foo", "bar", "baz");
assert tup[0] == "foo";
assert tup[1] == "bar";
assert tup[2] == "baz";

Indices are zero-based. A negative index counts backwards from the end of the vector, e.g.

var tup = ("foo", "bar", "baz");
assert tup[-1] == "baz";
assert tup[-2] == "bar";
assert tup[-3] == "foo";

Iterating

Tuples are iterable, e.g.

for item in (123, 456, 789) {
    echo item;
}

Containment

You can check if a tuple contains an item using the in operator, e.g.

if 123 in (123, 456, 789) {
    echo "found";
}

This is equivalent to calling the tuple's :contains() method.

Concatenation

You can concatenate tuples using the + operator, e.g.

var tup = ("abc", "def") + ("ghi", "jkl");

The result is a new tuple containing the combined entries from the input tuples.

Methods

:contains(value: any) -> bool

Returns true if the tuple contains an item equal to value, otherwise false.

:count() -> i64

Returns the number of items in the tuple.

:get(index: i64) -> any

Returns the value at index. Will panic if index is out of range or not an integer.

A negative index counts backwards from the end of 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. a start_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.

:values() -> iter

Returns an iterator over the tuple's values.