Tuple
Tuples are immutable, ordered sequences of values. They are produced by
certain operations such as iterating over key-value pairs. The Tuple type
object is in the prelude, so Tuple(iterable) is available without an
explicit import.
Constructor
Tuple iterable
Builds a tuple from an iterable.
The lowercase tuple factory instead collects its positional arguments
verbatim.
Parameters
| Name | Type | Description |
|---|---|---|
iterable |
values to collect |
When inherited by a Do subclass, copy() calls the subclass constructor with
the source tuple as a single positional argument. Subclasses that inherit
copy() should accept that argument and usually forward it with
Tuple.(init) $self $source.
let tup = Tuple([1, 2, 3])
assert_eq $tup[1] 2
for k v = {name: "Alice", age: 30}
# each pair is a tuple
echo "$k: $v"
Fields
len
Returns the number of elements.
Type
Int
Methods
get index :default? :else?
Retrieves the value at the given index. Returns nil if out of bounds and no
alternative is provided. Negative indexes count from the end.
Parameters
| Name | Type | Description |
|---|---|---|
index |
Int |
the index to access |
default: |
value to return if out of bounds | |
else: |
callable to invoke if out of bounds |
Returns
The value, or the default/else result.
contains element
Tests whether the tuple contains the given element (by equality).
Parameters
| Name | Type | Description |
|---|---|---|
element |
the value to check |
Returns
Bool
copy
Returns a shallow copy of the tuple. Because tuples are immutable, the returned copy may be the same object.
Returns
tuple
pairs
Returns an iterator yielding [index, value] pairs.
Returns
iterator of [int, value] pairs
Operations
Indexing
let pair = {name: "Alice"}.pairs().next()
assert_eq $pair[0] :name:
assert_eq $pair[1] "Alice"
assert_eq $pair[-1] "Alice"
Tuples are immutable; indexed assignment is not supported.
Tuples also accept Range values for slicing:
let tup = tuple([0, 1, 2, 3])
assert_eq $tup[1..3] (tuple [1, 2])
assert_eq $tup[..2] (tuple [0, 1])
assert_eq $tup[2..] (tuple [2, 3])
assert_eq $tup[..] (tuple [0, 1, 2, 3])
assert_eq $tup[Range 0 4 2] (tuple [0, 2])
assert_eq $tup[Range nil nil -1] (tuple [3, 2, 1, 0])
Slice indexing returns a new tuple.
Omitted start means 0, omitted end means the tuple length, and negative
start and end values count from the end. Negative steps reverse the slice.