Standard Library
struct Pair<A, B>
A pair of two values, possibly of different types.
since 0.1.0-alpha.1linuxwindowswasm
Overview
A Pair is a value type: assigning or passing one copies it, and changing a field of the copy does not change the original. Two pairs are equal with == when both fields are equal. Create one with Pair::Of(first, second). Pairs are what iterating a Map and relational joins produce, and they are a light way to return two values from a function.
Description
Pair<A, B> holds two values, first of type A and second of type B. Build one with
Pair::Of(a, b); the type arguments are inferred. A Pair is a struct, so it is a value:
assigning it copies it, and changing a field of the copy never changes the original. Two pairs
are equal with == when both fields are equal, and toString() gives the text (first, second).
Pairs are what the library hands back when it needs to return two things at once. Array.zip,
Array.join, Array.groupJoin and Array.withIndex produce arrays of pairs, Map.entries()
returns an Array<Pair<K, V>>, and a for loop over a Map yields one Pair per entry.
Building, copying and comparing pairs
Pair<string, int> best = Pair::Of("ann", 30);
console.writeln(best.first);
console.writeln(best.second);
console.writeln(best.toString());
Pair<string, int> copy = best;
copy.second = 31;
console.writeln(best.second);
console.writeln(copy.second);
console.writeln(best == Pair::Of("ann", 30));
console.writeln(best == copy);
ann
30
(ann, 30)
30
31
true
false
Rules
Pair::Of(a, b)is the constructor;firstandsecondare readable and assignable fields.- A
Pairis copied on assignment and on passing; it has no identity. ==compares the two fields in turn, each with its own==.- To print a pair, call
toString()and print the text.
Examples
Pairs from a Map loop and from zip
Map<string, int> scores = Map();
scores = scores.with("ann", 30).with("bob", 25);
for (Pair e in scores) { console.writeln(e.toString()); }
Array<Pair<int, string>> zipped = [1, 2].zip(["a", "b"]);
console.writeln(zipped[1].toString());
console.writeln(zipped[1].first);
(ann, 30)
(bob, 25)
(2, b)
2
Notes
For three values use Triple<A, B, C>; for more, declare a struct with named fields.
Examples
Typical use
Pair<string, int> entry = Pair::Of("apples", 3);
console.writeln("${entry.first}: ${entry.second}");
console.writeln(entry.toString());
Pair<string, int> copy = entry;
copy.second = 10;
console.writeln(entry.second);
console.writeln(copy.second);
console.writeln(entry == Pair::Of("apples", 3));
Array<Pair<string, int>> stock = [Pair::Of("a", 1), Pair::Of("b", 2)];
for (Pair<string, int> p in stock) {
console.writeln("${p.first}=${p.second}");
}
apples: 3
(apples, 3)
3
10
true
a=1
b=2
Constructors
Of
Pair::Of(A a, B b)Create a pair from its two values.
Written Pair::Of(a, b); the type arguments are inferred from the declared type of the variable it is assigned to.
Parameters
- a
- The value for
first. - b
- The value for
second.
Examples
Pair<int, string> p = Pair::Of(1, "one");
console.writeln(p.first);
console.writeln(p.second);
1
one
Fields
first
A firstThe first value of the pair.
second
B secondThe second value of the pair.
Methods
toString
toString() -> stringThe pair as text, in the form (first, second).
This is what ${} interpolation of a pair produces. The fields are joined with +, so a field that is itself a struct is not expanded.
Returns
The text (first, second).
Examples
Pair<int, string> p = Pair::Of(1, "one");
console.writeln(p.toString());
console.writeln("pair: ${p}");
Pair<float, bool> q = Pair::Of(1.5, true);
console.writeln(q.toString());
(1, one)
pair: (1, one)
(1.500000, true)
Operators
==
==(Pair<A, B> other) -> boolCompare two pairs field by field.
The pairs are equal when first equals first and second equals second, using the equality of the field types.
Parameters
- other
- The pair to compare with.
Returns
true if both fields are equal.
Examples
Pair<int, string> a = Pair::Of(1, "one");
Pair<int, string> b = Pair::Of(1, "one");
console.writeln(a == b);
console.writeln(a == Pair::Of(2, "one"));
console.writeln(a != b);
true
false
false