A tuple is a typed linked list. Each instance of
Tuple
represents the value and type of a single link.
The attributes first
and rest
allow us to retrieve a
value from the list without losing its static type
information.
value point = Tuple(0.0, Tuple(0.0, Tuple("origin", []))); Float x = point.first; Float y = point.rest.first; String label = point.rest.rest.first;
Usually, we abbreviate code involving tuples.
[Float,Float,String] point = [0.0, 0.0, "origin"]; Float x = point[0]; Float y = point[1]; String label = point[2];
A list of types enclosed in brackets is an abbreviated
tuple type. An instance of Tuple
may be constructed by
surrounding a value list in brackets:
[String,String] words = ["hello", "world"];
The index operator with a literal integer argument is a
shortcut for a chain of evaluations of rest
and
first
. For example, point[1]
means point.rest.first
.
A terminated tuple type is a tuple where the type of
the last link in the chain is Empty
. An unterminated
tuple type is a tuple where the type of the last link
in the chain is Sequence
or Sequential
. Thus, a
terminated tuple type has a length that is known
statically. For an unterminated tuple type only a lower
bound on its length is known statically.
Here, point
is an unterminated tuple:
String[] labels = ... ; [Float,Float,String*] point = [0.0, 0.0, *labels]; Float x = point[0]; Float y = point[1]; String? firstLabel = point[2]; String[] allLabels = point[2...];
no subtypes hierarchy
Initializer |
Tuple(First first, Rest rest) Parameters:
|
Attributes | |
first | Source Codeshared actual First first The first element of this tuple. (The head of the linked list.) |
last | Source Codeshared actual Element last The last element of this tuple. |
lastIndex | Source Codeshared actual Integer lastIndex The index of the last element of the list, or |
rest | Source Codeshared actual Rest rest A tuple with the elements of this tuple, except for the first element. (The tail of the linked list.) |
size | Source Codeshared actual Integer size The number of elements returned by the |
Inherited Attributes |
Attributes inherited from: Object |
Attributes inherited from: Collection<Element> |
Attributes inherited from: Correspondence<Key,Item> |
Attributes inherited from: Iterable<Element,Absent> |
Attributes inherited from: List<Element> |
Attributes inherited from: [Element+] |
Attributes inherited from: Element[] |
Methods | |
append | Source Codeshared actual [First, <Element|Other>*] append<Other>(Other[] elements) Return a tuple containing the elements of this
tuple, followed by the given Parameters:
|
clone | Source Codeshared actual Tuple<Element,First,Rest> clone() This tuple. |
contains | Source Codeshared actual Boolean contains(Object element) Determine if the given value is an element of this tuple. |
getFromFirst | Source Codeshared actual Element? getFromFirst(Integer index) The |
iterator | Source Codeshared actual Iterator<Element> iterator() An iterator for the elements belonging to this stream. If this is a nonempty stream with type |
measure | Source Codeshared actual Element[] measure(Integer from, Integer length) Obtain a measure containing the mapped values starting
from the given starting index, with the given
For any ranged stream r[from:length] The measure should contain the given number
of elements of this stream, starting from the element
at the given starting index, in the same order
as they are produced by the When the given index does not belong to this ranged object, the behavior is implementation dependent. |
span | Source Codeshared actual Element[] span(Integer from, Integer end) Obtain a span containing the elements between the two given indices. For any ranged stream r[from..to] The span should contain elements of this stream,
starting from the element at the given starting
index, and ending with the element at the given
ending index, in the same order as they are
produced by the When one or both of the given indices does not belong to this ranged stream, the behavior is implementation dependent. |
spanFrom | Source Codeshared actual Element[] spanFrom(Integer from) Obtain a span containing the elements between the given starting index and the last index of this ranged object. For any ranged stream r[from...] The span should contain elements of this stream,
starting from the element at the given starting
index, in the same order as they are produced by
the When the given index does not belong to this ranged stream, the behavior is implementation dependent. |
spanTo | Source Codeshared actual Element[] spanTo(Integer to) Obtain a span containing the elements between the first index of this ranged stream and given end index. For any ranged stream r[...to] The span should contain elements of this stream, up to
the element at the given ending index, in the
same order as they are produced by the When the given index does not belong to this ranged stream, the behavior is implementation dependent. |
tuple | Source Codeshared actual Tuple<Element,First,Rest> tuple() A This operation makes it possible to narrow this sequence to a given static length, for example: assert (is String[3] bits = string.split('/'.equals) .sequence() .tuple); value [prefix, middle, postfix] = bits; Since 1.3.3 |
withLeading | Source Codeshared actual Tuple<Element|Other,Other,Tuple<Element,First,Rest>> withLeading<Other>(Other element) Return a new tuple that starts with the specified
Parameters:
|
withTrailing | Source Codeshared actual [First, <Element|Other>+] withTrailing<Other>(Other element) Return a new tuple containing the elements of this
tuple, followed by the given Parameters:
|