From d908678dcfd493c24df51b6696945c86dd3a876e Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Mon, 22 Dec 2014 23:34:46 +0300 Subject: [PATCH] Take into account collection sizes when possible #KT-6180 --- libraries/stdlib/src/generated/_Filtering.kt | 20 ++--- libraries/stdlib/src/generated/_Generators.kt | 82 +++++-------------- libraries/stdlib/src/generated/_Mapping.kt | 22 ++--- libraries/stdlib/src/generated/_Snapshots.kt | 12 +-- .../stdlib/src/kotlin/collections/JUtil.kt | 3 + .../src/templates/Filtering.kt | 2 +- .../src/templates/Generators.kt | 37 +++++++-- .../src/templates/Mapping.kt | 9 +- .../src/templates/Snapshots.kt | 33 ++------ 9 files changed, 97 insertions(+), 123 deletions(-) diff --git a/libraries/stdlib/src/generated/_Filtering.kt b/libraries/stdlib/src/generated/_Filtering.kt index 9674752548c..9deca84e2ed 100644 --- a/libraries/stdlib/src/generated/_Filtering.kt +++ b/libraries/stdlib/src/generated/_Filtering.kt @@ -12,7 +12,7 @@ import java.util.* */ public fun Array.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -26,7 +26,7 @@ public fun Array.drop(n: Int): List { */ public fun BooleanArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -40,7 +40,7 @@ public fun BooleanArray.drop(n: Int): List { */ public fun ByteArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -54,7 +54,7 @@ public fun ByteArray.drop(n: Int): List { */ public fun CharArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -68,7 +68,7 @@ public fun CharArray.drop(n: Int): List { */ public fun DoubleArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -82,7 +82,7 @@ public fun DoubleArray.drop(n: Int): List { */ public fun FloatArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -96,7 +96,7 @@ public fun FloatArray.drop(n: Int): List { */ public fun IntArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -110,7 +110,7 @@ public fun IntArray.drop(n: Int): List { */ public fun LongArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -124,7 +124,7 @@ public fun LongArray.drop(n: Int): List { */ public fun ShortArray.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { @@ -138,7 +138,7 @@ public fun ShortArray.drop(n: Int): List { */ public fun Collection.drop(n: Int): List { if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) for (item in this) { diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 21b8cc77c28..2cc7d676928 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -13,7 +13,7 @@ import java.util.* public inline fun Array.merge(array: Array, transform: (T, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -26,7 +26,7 @@ public inline fun Array.merge(array: Array, transform: ( public inline fun BooleanArray.merge(array: Array, transform: (Boolean, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -39,7 +39,7 @@ public inline fun BooleanArray.merge(array: Array, transform: (Boo public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -52,7 +52,7 @@ public inline fun ByteArray.merge(array: Array, transform: (Byte, public inline fun CharArray.merge(array: Array, transform: (Char, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -65,7 +65,7 @@ public inline fun CharArray.merge(array: Array, transform: (Char, public inline fun DoubleArray.merge(array: Array, transform: (Double, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -78,7 +78,7 @@ public inline fun DoubleArray.merge(array: Array, transform: (Doub public inline fun FloatArray.merge(array: Array, transform: (Float, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -91,7 +91,7 @@ public inline fun FloatArray.merge(array: Array, transform: (Float public inline fun IntArray.merge(array: Array, transform: (Int, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -104,7 +104,7 @@ public inline fun IntArray.merge(array: Array, transform: (Int, R) public inline fun LongArray.merge(array: Array, transform: (Long, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -117,7 +117,7 @@ public inline fun LongArray.merge(array: Array, transform: (Long, public inline fun ShortArray.merge(array: Array, transform: (Short, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -130,20 +130,7 @@ public inline fun ShortArray.merge(array: Array, transform: (Short public inline fun Iterable.merge(array: Array, transform: (T, R) -> V): List { val first = iterator() val second = array.iterator() - val list = arrayListOf() - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. - */ -public inline fun String.merge(array: Array, transform: (Char, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(collectionSizeOrDefault(10)) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -156,7 +143,7 @@ public inline fun String.merge(array: Array, transform: (Char, R) public inline fun Array.merge(other: Iterable, transform: (T, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -169,7 +156,7 @@ public inline fun Array.merge(other: Iterable, transform: (T public inline fun BooleanArray.merge(other: Iterable, transform: (Boolean, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -182,7 +169,7 @@ public inline fun BooleanArray.merge(other: Iterable, transform: (Bool public inline fun ByteArray.merge(other: Iterable, transform: (Byte, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -195,7 +182,7 @@ public inline fun ByteArray.merge(other: Iterable, transform: (Byte, R public inline fun CharArray.merge(other: Iterable, transform: (Char, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -208,7 +195,7 @@ public inline fun CharArray.merge(other: Iterable, transform: (Char, R public inline fun DoubleArray.merge(other: Iterable, transform: (Double, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -221,7 +208,7 @@ public inline fun DoubleArray.merge(other: Iterable, transform: (Doubl public inline fun FloatArray.merge(other: Iterable, transform: (Float, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -234,7 +221,7 @@ public inline fun FloatArray.merge(other: Iterable, transform: (Float, public inline fun IntArray.merge(other: Iterable, transform: (Int, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -247,7 +234,7 @@ public inline fun IntArray.merge(other: Iterable, transform: (Int, R) public inline fun LongArray.merge(other: Iterable, transform: (Long, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -260,7 +247,7 @@ public inline fun LongArray.merge(other: Iterable, transform: (Long, R public inline fun ShortArray.merge(other: Iterable, transform: (Short, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -273,20 +260,7 @@ public inline fun ShortArray.merge(other: Iterable, transform: (Short, public inline fun Iterable.merge(other: Iterable, transform: (T, R) -> V): List { val first = iterator() val second = other.iterator() - val list = arrayListOf() - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) - } - return list -} - -/** - * Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. - */ -public inline fun String.merge(other: Iterable, transform: (Char, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(collectionSizeOrDefault(10)) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -877,13 +851,6 @@ public fun Iterable.zip(array: Array): List> { return merge(array) { (t1, t2) -> t1 to t2 } } -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public fun String.zip(array: Array): List> { - return merge(array) { (t1, t2) -> t1 to t2 } -} - /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ @@ -954,20 +921,13 @@ public fun Iterable.zip(other: Iterable): List> { return merge(other) { (t1, t2) -> t1 to t2 } } -/** - * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. - */ -public fun String.zip(other: Iterable): List> { - return merge(other) { (t1, t2) -> t1 to t2 } -} - /** * Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection. */ public fun String.zip(other: String): List> { val first = iterator() val second = other.iterator() - val list = ArrayList>() + val list = ArrayList>(length()) while (first.hasNext() && second.hasNext()) { list.add(first.next() to second.next()) } diff --git a/libraries/stdlib/src/generated/_Mapping.kt b/libraries/stdlib/src/generated/_Mapping.kt index 894b7d7c0ef..776c4f457f5 100644 --- a/libraries/stdlib/src/generated/_Mapping.kt +++ b/libraries/stdlib/src/generated/_Mapping.kt @@ -564,70 +564,70 @@ public inline fun String.map(transform: (Char) -> R): List { * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun Array.mapIndexed(transform: (Int, T) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun BooleanArray.mapIndexed(transform: (Int, Boolean) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun ByteArray.mapIndexed(transform: (Int, Byte) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun CharArray.mapIndexed(transform: (Int, Char) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun DoubleArray.mapIndexed(transform: (Int, Double) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun FloatArray.mapIndexed(transform: (Int, Float) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun IntArray.mapIndexed(transform: (Int, Int) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun LongArray.mapIndexed(transform: (Int, Long) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun ShortArray.mapIndexed(transform: (Int, Short) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(size()), transform) } /** * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun Iterable.mapIndexed(transform: (Int, T) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform) } /** @@ -641,7 +641,7 @@ public fun Stream.mapIndexed(transform: (Int, T) -> R): Stream { * Returns a list containing the results of applying the given *transform* function to each element and its index of the original collection */ public inline fun String.mapIndexed(transform: (Int, Char) -> R): List { - return mapIndexedTo(ArrayList(), transform) + return mapIndexedTo(ArrayList(length()), transform) } /** diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index 3f17c9fe558..1ffc5bba317 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -92,7 +92,7 @@ public fun ShortArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun Iterable.toArrayList(): ArrayList { - return toCollection(ArrayList()) + return toCollection(ArrayList(collectionSizeOrDefault(10))) } /** @@ -106,7 +106,7 @@ public fun Stream.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun String.toArrayList(): ArrayList { - return toCollection(ArrayList()) + return toCollection(ArrayList(length())) } /** @@ -411,7 +411,9 @@ public fun Map.toList(): List> { * Returns a List containing all elements */ public fun Array.toList(): List { - return toCollection(ArrayList()) + val list = ArrayList(size()) + for (item in this) list.add(item) + return list } /** @@ -490,7 +492,7 @@ public fun ShortArray.toList(): List { * Returns a List containing all elements */ public fun Iterable.toList(): List { - return toCollection(ArrayList()) + return toCollection(ArrayList(collectionSizeOrDefault(10))) } /** @@ -504,7 +506,7 @@ public fun Stream.toList(): List { * Returns a List containing all elements */ public fun String.toList(): List { - return toCollection(ArrayList()) + return toCollection(ArrayList(length())) } /** diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index b1b9a57a97f..d292f6cfa16 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -86,3 +86,6 @@ public fun List?.orEmpty(): List = this ?: emptyList() /** Returns the List if its not null otherwise returns the empty list */ public fun Set?.orEmpty(): Set = this ?: emptySet() + +public fun Iterable.collectionSizeOrNull(): Int? = if (this is Collection<*>) size() else null +public fun Iterable.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) size() else default \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index 94be3ef24b3..b484c886bab 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -33,7 +33,7 @@ fun filtering(): List { body(Collections, ArraysOfObjects, ArraysOfPrimitives) { """ if (n >= size()) - return ArrayList() + return emptyList() var count = 0 val list = ArrayList(size() - n) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 2fa21cb6394..967b3a017b9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -119,7 +119,7 @@ fun generators(): List { } templates add f("merge(other: Iterable, transform: (T, R) -> V)") { - exclude(Streams) + exclude(Streams, Strings) doc { """ Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. @@ -133,7 +133,18 @@ fun generators(): List { """ val first = iterator() val second = other.iterator() - val list = arrayListOf() + val list = ArrayList(collectionSizeOrDefault(10)) + while (first.hasNext() && second.hasNext()) { + list.add(transform(first.next(), second.next())) + } + return list + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val first = iterator() + val second = other.iterator() + val list = ArrayList(size()) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -143,7 +154,7 @@ fun generators(): List { } templates add f("merge(array: Array, transform: (T, R) -> V)") { - exclude(Streams) + exclude(Streams, Strings) doc { """ Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. @@ -157,13 +168,25 @@ fun generators(): List { """ val first = iterator() val second = array.iterator() - val list = arrayListOf() + val list = ArrayList(collectionSizeOrDefault(10)) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } return list """ } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val first = iterator() + val second = array.iterator() + val list = ArrayList(size()) + while (first.hasNext() && second.hasNext()) { + list.add(transform(first.next(), second.next())) + } + return list + """ + } + } @@ -186,7 +209,7 @@ fun generators(): List { templates add f("zip(other: Iterable)") { - exclude(Streams) + exclude(Streams, Strings) doc { """ Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. @@ -213,7 +236,7 @@ fun generators(): List { """ val first = iterator() val second = other.iterator() - val list = ArrayList>() + val list = ArrayList>(length()) while (first.hasNext() && second.hasNext()) { list.add(first.next() to second.next()) } @@ -223,7 +246,7 @@ fun generators(): List { } templates add f("zip(array: Array)") { - exclude(Streams) + exclude(Streams, Strings) doc { """ Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 1e14918f48c..266815fa57c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -51,9 +51,14 @@ fun mapping(): List { typeParam("R") returns("List") body { - "return mapIndexedTo(ArrayList(), transform)" + "return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform)" + } + body(ArraysOfObjects, ArraysOfPrimitives) { + "return mapIndexedTo(ArrayList(size()), transform)" + } + body(Strings) { + "return mapIndexedTo(ArrayList(length()), transform)" } - inline(false, Streams) returns(Streams) { "Stream" } doc(Streams) { "Returns a stream containing the results of applying the given *transform* function to each element and its index of the original stream" } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index f9da0307009..05b85b4787c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -40,16 +40,9 @@ fun snapshots(): List { templates add f("toArrayList()") { doc { "Returns an ArrayList of all elements" } returns("ArrayList") - body { "return toCollection(ArrayList())" } - - // ISSUE: JavaScript can't perform this operation - /* - body(Collections) { - """ - return ArrayList(this) - """ - } - */ + body { "return toCollection(ArrayList(collectionSizeOrDefault(10)))" } + body(Streams) { "return toCollection(ArrayList())" } + body(Strings) { "return toCollection(ArrayList(length()))" } body(ArraysOfObjects, ArraysOfPrimitives) { """ val list = ArrayList(size()) @@ -76,22 +69,10 @@ fun snapshots(): List { templates add f("toList()") { doc { "Returns a List containing all elements" } returns("List") - body { "return toCollection(ArrayList())" } - - // ISSUE: JavaScript can't perform this operations - /* - body(Collections) { - """ - return ArrayList(this) - """ - } - body(ArraysOfObjects) { - """ - return ArrayList(Arrays.asList(*this)) - """ - } - */ - body(ArraysOfPrimitives) { + body { "return toCollection(ArrayList(collectionSizeOrDefault(10)))" } + body(Streams) { "return toCollection(ArrayList())" } + body(Strings) { "return toCollection(ArrayList(length()))" } + body(ArraysOfObjects, ArraysOfPrimitives) { """ val list = ArrayList(size()) for (item in this) list.add(item)