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]]
Implements: Spread[Ts], Unpack[Ts, Rest: BaseSeq[Ts], Pos: Tuple[...Ts]], Index[Ts], Seq[Union[...Ts]]
Constructor
Tuple[T] iterable
Builds a tuple from an iterable.
The lowercase tuple factory instead collects its positional arguments
verbatim.
Parameters
| Name | Type | Description |
|---|---|---|
self |
Tuple[...T] |
Receiver. |
iterable |
Iterable[T] |
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"
Fields
len @ Int[get]Returns the number of elements.
Methods
(index)[K] key -> IndexItem[Ts, K] overload 1 of 2
Returns an element, or a new tuple for a slice.
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.
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)
Omitted start means 0, omitted end means the tuple length, and negative
endpoints count from the end. Negative steps reverse the slice.
Parameters
| Name | Type | Description |
|---|---|---|
key |
K |
(index) range -> Tuple[...Union[...Ts]] overload 2 of 2
Returns an element, or a new tuple for a slice.
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.
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)
Omitted start means 0, omitted end means the tuple length, and negative
endpoints count from the end. Negative steps reverse the slice.
Parameters
| Name | Type | Description |
|---|---|---|
range |
Range[Int] |
(iter)() -> UnpackIter[Union[...Ts]]
Returns an iterator with read-only unpacking.
(spread)() -> Spread[Ts]
Spreads the items.
(unpack)() -> Tuple[...Ts]
Unpacks tuple elements.
contains element -> Bool
Tests whether the tuple contains the given element (by equality).
Parameters
| Name | Type | Description |
|---|---|---|
element |
Value |
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) |
iter() -> UnpackIter[Union[...Ts]]
Returns an iterator with read-only unpacking.
pairs() -> UnpackIter[Tuple[Int, Value]]
Returns an iterator yielding (index, value) pairs.