Support componentN for arrays and lists #KT-5538 Fixed
This commit is contained in:
@@ -7,6 +7,356 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> Array<out T>.component1(): T {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun BooleanArray.component1(): Boolean {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun ByteArray.component1(): Byte {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun CharArray.component1(): Char {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun DoubleArray.component1(): Double {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun FloatArray.component1(): Float {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun IntArray.component1(): Int {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun LongArray.component1(): Long {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun ShortArray.component1(): Short {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 1st *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> List<T>.component1(): T {
|
||||
return get(0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> Array<out T>.component2(): T {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun BooleanArray.component2(): Boolean {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun ByteArray.component2(): Byte {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun CharArray.component2(): Char {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun DoubleArray.component2(): Double {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun FloatArray.component2(): Float {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun IntArray.component2(): Int {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun LongArray.component2(): Long {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun ShortArray.component2(): Short {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 2nd *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> List<T>.component2(): T {
|
||||
return get(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> Array<out T>.component3(): T {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun BooleanArray.component3(): Boolean {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun ByteArray.component3(): Byte {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun CharArray.component3(): Char {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun DoubleArray.component3(): Double {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun FloatArray.component3(): Float {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun IntArray.component3(): Int {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun LongArray.component3(): Long {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun ShortArray.component3(): Short {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 3rd *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> List<T>.component3(): T {
|
||||
return get(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> Array<out T>.component4(): T {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun BooleanArray.component4(): Boolean {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun ByteArray.component4(): Byte {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun CharArray.component4(): Char {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun DoubleArray.component4(): Double {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun FloatArray.component4(): Float {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun IntArray.component4(): Int {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun LongArray.component4(): Long {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun ShortArray.component4(): Short {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 4th *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> List<T>.component4(): T {
|
||||
return get(3)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> Array<out T>.component5(): T {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun BooleanArray.component5(): Boolean {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun ByteArray.component5(): Byte {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun CharArray.component5(): Char {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun DoubleArray.component5(): Double {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun FloatArray.component5(): Float {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun IntArray.component5(): Int {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun LongArray.component5(): Long {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun ShortArray.component5(): Short {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 5th *element* from the collection.
|
||||
*/
|
||||
public inline fun <T> List<T>.component5(): T {
|
||||
return get(4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if *element* is found in the collection
|
||||
*/
|
||||
|
||||
@@ -471,4 +471,42 @@ class CollectionTest {
|
||||
expect(arrayListOf(1, 2, 3)) { list.sort() }
|
||||
expect(arrayListOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun decomposeFirst() {
|
||||
val (first) = listOf(1, 2)
|
||||
assertEquals(first, 1)
|
||||
}
|
||||
|
||||
test fun decomposeSplit() {
|
||||
val (key, value) = "key = value".split('=').map { it.trim() }
|
||||
assertEquals(key, "key")
|
||||
assertEquals(value, "value")
|
||||
}
|
||||
|
||||
test fun decomposeList() {
|
||||
val (a, b, c, d, e) = listOf(1, 2, 3, 4, 5)
|
||||
assertEquals(a, 1)
|
||||
assertEquals(b, 2)
|
||||
assertEquals(c, 3)
|
||||
assertEquals(d, 4)
|
||||
assertEquals(e, 5)
|
||||
}
|
||||
|
||||
test fun decomposeArray() {
|
||||
val (a, b, c, d, e) = array(1, 2, 3, 4, 5)
|
||||
assertEquals(a, 1)
|
||||
assertEquals(b, 2)
|
||||
assertEquals(c, 3)
|
||||
assertEquals(d, 4)
|
||||
assertEquals(e, 5)
|
||||
}
|
||||
|
||||
test fun decomposeIntArray() {
|
||||
val (a, b, c, d, e) = intArray(1, 2, 3, 4, 5)
|
||||
assertEquals(a, 1)
|
||||
assertEquals(b, 2)
|
||||
assertEquals(c, 3)
|
||||
assertEquals(d, 4)
|
||||
assertEquals(e, 5)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,5 +434,41 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("component1()") {
|
||||
inline(true)
|
||||
doc { "Returns 1st *element* from the collection." }
|
||||
returns("T")
|
||||
body { "return get(0)" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
}
|
||||
templates add f("component2()") {
|
||||
inline(true)
|
||||
doc { "Returns 2nd *element* from the collection." }
|
||||
returns("T")
|
||||
body { "return get(1)" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
}
|
||||
templates add f("component3()") {
|
||||
inline(true)
|
||||
doc { "Returns 3rd *element* from the collection." }
|
||||
returns("T")
|
||||
body { "return get(2)" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
}
|
||||
templates add f("component4()") {
|
||||
inline(true)
|
||||
doc { "Returns 4th *element* from the collection." }
|
||||
returns("T")
|
||||
body { "return get(3)" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
}
|
||||
templates add f("component5()") {
|
||||
inline(true)
|
||||
doc { "Returns 5th *element* from the collection." }
|
||||
returns("T")
|
||||
body { "return get(4)" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user