Tuple[*Ts]
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.
Inherits from: Iterable[Union[...Ts]]
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 |
Example
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"
Methods
(index) index
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 endpoints count from the end. Negative
steps reverse the slice.
(unpack)()
Unpacks tuple elements.
contains element -> Bool
Tests whether the tuple contains the given element (by equality).
Parameters
| Name | Type | Description |
|---|---|---|
element |
the value to check |
copy() -> Tuple[...Ts]
Returns a shallow copy of the tuple. Because tuples are immutable, the returned copy may be the same object.
get[D = nil] index … -> (Union[...Ts] | D)
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? |
D |
|
:else? |
(() -> D) |
len() -> Int
Returns the number of elements.
pairs() -> Iter[Tuple[Int, Value]]
Returns an iterator yielding [index, value] pairs.