-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Tuples.kt
74 lines (66 loc) · 2.3 KB
/
Tuples.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@file:kotlin.jvm.JvmName("TuplesKt")
package kotlin
/**
* Represents a generic pair of two values.
*
* There is no meaning attached to values in this class, it can be used for any purpose.
* Pair exhibits value semantics, i.e. two pairs are equal if both components are equal.
*
* An example of decomposing it into values:
* @sample samples.misc.Tuples.pairDestructuring
*
* @param A type of the first value.
* @param B type of the second value.
* @property first First value.
* @property second Second value.
* @constructor Creates a new instance of Pair.
*/
public data class Pair<out A, out B>(
public val first: A,
public val second: B
) : Serializable {
/**
* Returns string representation of the [Pair] including its [first] and [second] values.
*/
public override fun toString(): String = "($first, $second)"
}
/**
* Creates a tuple of type [Pair] from this and [that].
*
* This can be useful for creating [Map] literals with less noise, for example:
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
/**
* Converts this pair into a list.
*/
public fun <T> Pair<T, T>.toList(): List<T> = listOf(first, second)
/**
* Represents a triad of values
*
* There is no meaning attached to values in this class, it can be used for any purpose.
* Triple exhibits value semantics, i.e. two triples are equal if all three components are equal.
* An example of decomposing it into values:
* @sample samples.misc.Tuples.tripleDestructuring
*
* @param A type of the first value.
* @param B type of the second value.
* @param C type of the third value.
* @property first First value.
* @property second Second value.
* @property third Third value.
*/
public data class Triple<out A, out B, out C>(
public val first: A,
public val second: B,
public val third: C
) : Serializable {
/**
* Returns string representation of the [Triple] including its [first], [second] and [third] values.
*/
public override fun toString(): String = "($first, $second, $third)"
}
/**
* Converts this triple into a list.
*/
public fun <T> Triple<T, T, T>.toList(): List<T> = listOf(first, second, third)