From 38a4f86db2877f271c7d7ea05cee832828a93f42 Mon Sep 17 00:00:00 2001 From: Valery Kharitonov Date: Sun, 4 Jan 2015 12:26:01 +0300 Subject: [PATCH] Implemented asIterable method for arrays. #KT-1313 Fixed --- libraries/stdlib/src/generated/_Arrays.kt | 81 +++++++++++++++++++ .../stdlib/test/collections/ArraysTest.kt | 27 ++++++- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 15 +++- 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 15748d5caa6..2957d6334c2 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -9,6 +9,87 @@ import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js +/** + * Returns the Iterable that wraps the original array + */ +public fun Array.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun BooleanArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun ByteArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun CharArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun DoubleArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun FloatArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun IntArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun LongArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + +/** + * Returns the Iterable that wraps the original array + */ +public fun ShortArray.asIterable(): Iterable { + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } +} + /** * Returns the range of valid indices for the array */ diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index db36cf55cf2..d7957ccb503 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -1,6 +1,9 @@ package test.collections -import kotlin.test.* +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue +import kotlin.test.expect import org.junit.Test as test fun checkContent(iter: Iterator, length: Int, value: (Int) -> T) { @@ -214,6 +217,28 @@ class ArraysTest { assertEquals(listOf("aab", "aba", "ac"), array("ac", "aab", "aba").toSortedList()) } + test fun asIterable() { + val arr1 = intArray(1, 2, 3, 4, 5) + val iter1 = arr1.asIterable() + assertEquals(arr1.toList(), iter1.toList()) + arr1[0] = 0 + assertEquals(arr1.toList(), iter1.toList()) + + val arr2 = array("one", "two", "three") + val iter2 = arr2.asIterable() + assertEquals(arr2.toList(), iter2.toList()) + arr2[0] = "" + assertEquals(arr2.toList(), iter2.toList()) + + val arr3 = IntArray(0) + val iter3 = arr3.asIterable() + assertEquals(iter3.toList(), emptyList()) + + val arr4 = Array(0, { "$it" }) + val iter4 = arr4.asIterable() + assertEquals(iter4.toList(), emptyList()) + } + /* TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 43d369b0a98..642e4f2761c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -23,6 +23,19 @@ fun arrays(): List { } } + templates add f("asIterable()") { + only(ArraysOfObjects, ArraysOfPrimitives) + doc { "Returns the Iterable that wraps the original array" } + returns("Iterable") + body { + """ + return object : Iterable { + override fun iterator(): Iterator = this@asIterable.iterator() + } + """ + } + } + templates add pval("lastIndex") { only(ArraysOfObjects, ArraysOfPrimitives) doc { "Returns the last valid index for the array" } @@ -42,4 +55,4 @@ fun arrays(): List { } return templates -} \ No newline at end of file +}