diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index aa42608fd01..d7b13651452 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -8880,72 +8880,63 @@ public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { * Returns `true` if array has at least one element. */ public fun Array.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun ByteArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun ShortArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun IntArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun LongArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun FloatArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun DoubleArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun BooleanArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun CharArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** @@ -10634,72 +10625,63 @@ public fun CharArray.minWith(comparator: Comparator): Char? { * Returns `true` if the array has no elements. */ public fun Array.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun ByteArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun ShortArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun IntArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun LongArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun FloatArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun DoubleArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun BooleanArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun CharArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** diff --git a/js/js.libraries/src/core/generated/_CollectionsJs.kt b/js/js.libraries/src/core/generated/_CollectionsJs.kt index 7f55d417a78..047194d88eb 100644 --- a/js/js.libraries/src/core/generated/_CollectionsJs.kt +++ b/js/js.libraries/src/core/generated/_CollectionsJs.kt @@ -1361,6 +1361,7 @@ public infix fun Iterable.union(other: Iterable): Set { * Returns `true` if all elements match the given [predicate]. */ public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true for (element in this) if (!predicate(element)) return false return true } @@ -1369,14 +1370,15 @@ public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { * Returns `true` if collection has at least one element. */ public fun Iterable.any(): Boolean { - for (element in this) return true - return false + if (this is Collection) return !isEmpty() + return iterator().hasNext() } /** * Returns `true` if at least one element matches the given [predicate]. */ public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return false for (element in this) if (predicate(element)) return true return false } @@ -1385,6 +1387,7 @@ public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { * Returns the number of elements in this collection. */ public fun Iterable.count(): Int { + if (this is Collection) return size var count = 0 for (element in this) count++ return count @@ -1402,6 +1405,7 @@ public inline fun Collection.count(): Int { * Returns the number of elements matching the given [predicate]. */ public inline fun Iterable.count(predicate: (T) -> Boolean): Int { + if (this is Collection && isEmpty()) return 0 var count = 0 for (element in this) if (predicate(element)) count++ return count @@ -1653,14 +1657,15 @@ public fun Iterable.minWith(comparator: Comparator): T? { * Returns `true` if the collection has no elements. */ public fun Iterable.none(): Boolean { - for (element in this) return false - return true + if (this is Collection) return isEmpty() + return !iterator().hasNext() } /** * Returns `true` if no elements match the given [predicate]. */ public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true for (element in this) if (predicate(element)) return false return true } diff --git a/js/js.libraries/src/core/generated/_MapsJs.kt b/js/js.libraries/src/core/generated/_MapsJs.kt index 02da5b759f7..29690bd3bd6 100644 --- a/js/js.libraries/src/core/generated/_MapsJs.kt +++ b/js/js.libraries/src/core/generated/_MapsJs.kt @@ -88,6 +88,7 @@ public inline fun > Map.mapTo(des * Returns `true` if all entries match the given [predicate]. */ public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true for (element in this) if (!predicate(element)) return false return true } @@ -96,14 +97,14 @@ public inline fun Map.all(predicate: (Map.Entry) -> Boole * Returns `true` if map has at least one entry. */ public fun Map.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if at least one entry matches the given [predicate]. */ public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return false for (element in this) if (predicate(element)) return true return false } @@ -120,6 +121,7 @@ public inline fun Map.count(): Int { * Returns the number of entries matching the given [predicate]. */ public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { + if (isEmpty()) return 0 var count = 0 for (element in this) if (predicate(element)) count++ return count @@ -167,14 +169,14 @@ public fun Map.minWith(comparator: Comparator Map.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if no entries match the given [predicate]. */ public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true for (element in this) if (predicate(element)) return false return true } diff --git a/js/js.libraries/src/core/generated/_SequencesJs.kt b/js/js.libraries/src/core/generated/_SequencesJs.kt index 061ab04feb4..31d3bb14bb7 100644 --- a/js/js.libraries/src/core/generated/_SequencesJs.kt +++ b/js/js.libraries/src/core/generated/_SequencesJs.kt @@ -936,8 +936,7 @@ public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { * The operation is _terminal_. */ public fun Sequence.any(): Boolean { - for (element in this) return true - return false + return iterator().hasNext() } /** @@ -1215,8 +1214,7 @@ public fun Sequence.minWith(comparator: Comparator): T? { * The operation is _terminal_. */ public fun Sequence.none(): Boolean { - for (element in this) return false - return true + return !iterator().hasNext() } /** diff --git a/js/js.libraries/src/core/generated/_StringsJs.kt b/js/js.libraries/src/core/generated/_StringsJs.kt index 4cc6b649cd0..356ad697cde 100644 --- a/js/js.libraries/src/core/generated/_StringsJs.kt +++ b/js/js.libraries/src/core/generated/_StringsJs.kt @@ -815,8 +815,7 @@ public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean { * Returns `true` if char sequence has at least one character. */ public fun CharSequence.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** @@ -1003,8 +1002,7 @@ public fun CharSequence.minWith(comparator: Comparator): Char? { * Returns `true` if the char sequence has no characters. */ public fun CharSequence.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index e3a4869d71b..ae33efda44f 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -8950,72 +8950,63 @@ public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { * Returns `true` if array has at least one element. */ public fun Array.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun ByteArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun ShortArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun IntArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun LongArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun FloatArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun DoubleArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun BooleanArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if array has at least one element. */ public fun CharArray.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** @@ -10704,72 +10695,63 @@ public fun CharArray.minWith(comparator: Comparator): Char? { * Returns `true` if the array has no elements. */ public fun Array.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun ByteArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun ShortArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun IntArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun LongArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun FloatArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun DoubleArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun BooleanArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if the array has no elements. */ public fun CharArray.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index d622d6315ca..63f0fed44e6 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1371,6 +1371,7 @@ public infix fun Iterable.union(other: Iterable): Set { * Returns `true` if all elements match the given [predicate]. */ public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true for (element in this) if (!predicate(element)) return false return true } @@ -1379,14 +1380,15 @@ public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { * Returns `true` if collection has at least one element. */ public fun Iterable.any(): Boolean { - for (element in this) return true - return false + if (this is Collection) return !isEmpty() + return iterator().hasNext() } /** * Returns `true` if at least one element matches the given [predicate]. */ public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return false for (element in this) if (predicate(element)) return true return false } @@ -1395,6 +1397,7 @@ public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean { * Returns the number of elements in this collection. */ public fun Iterable.count(): Int { + if (this is Collection) return size var count = 0 for (element in this) count++ return count @@ -1412,6 +1415,7 @@ public inline fun Collection.count(): Int { * Returns the number of elements matching the given [predicate]. */ public inline fun Iterable.count(predicate: (T) -> Boolean): Int { + if (this is Collection && isEmpty()) return 0 var count = 0 for (element in this) if (predicate(element)) count++ return count @@ -1663,14 +1667,15 @@ public fun Iterable.minWith(comparator: Comparator): T? { * Returns `true` if the collection has no elements. */ public fun Iterable.none(): Boolean { - for (element in this) return false - return true + if (this is Collection) return isEmpty() + return !iterator().hasNext() } /** * Returns `true` if no elements match the given [predicate]. */ public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { + if (this is Collection && isEmpty()) return true for (element in this) if (predicate(element)) return false return true } diff --git a/libraries/stdlib/src/generated/_Maps.kt b/libraries/stdlib/src/generated/_Maps.kt index a8f932d3dda..d11c03b317a 100644 --- a/libraries/stdlib/src/generated/_Maps.kt +++ b/libraries/stdlib/src/generated/_Maps.kt @@ -88,6 +88,7 @@ public inline fun > Map.mapTo(des * Returns `true` if all entries match the given [predicate]. */ public inline fun Map.all(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true for (element in this) if (!predicate(element)) return false return true } @@ -96,14 +97,14 @@ public inline fun Map.all(predicate: (Map.Entry) -> Boole * Returns `true` if map has at least one entry. */ public fun Map.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** * Returns `true` if at least one entry matches the given [predicate]. */ public inline fun Map.any(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return false for (element in this) if (predicate(element)) return true return false } @@ -120,6 +121,7 @@ public inline fun Map.count(): Int { * Returns the number of entries matching the given [predicate]. */ public inline fun Map.count(predicate: (Map.Entry) -> Boolean): Int { + if (isEmpty()) return 0 var count = 0 for (element in this) if (predicate(element)) count++ return count @@ -167,14 +169,14 @@ public fun Map.minWith(comparator: Comparator Map.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** * Returns `true` if no entries match the given [predicate]. */ public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { + if (isEmpty()) return true for (element in this) if (predicate(element)) return false return true } diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 3afc3105f85..246947e234d 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -958,8 +958,7 @@ public inline fun Sequence.all(predicate: (T) -> Boolean): Boolean { * The operation is _terminal_. */ public fun Sequence.any(): Boolean { - for (element in this) return true - return false + return iterator().hasNext() } /** @@ -1237,8 +1236,7 @@ public fun Sequence.minWith(comparator: Comparator): T? { * The operation is _terminal_. */ public fun Sequence.none(): Boolean { - for (element in this) return false - return true + return !iterator().hasNext() } /** diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index ec9305b2bfb..b2adb7cf962 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -823,8 +823,7 @@ public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean { * Returns `true` if char sequence has at least one character. */ public fun CharSequence.any(): Boolean { - for (element in this) return true - return false + return !isEmpty() } /** @@ -1011,8 +1010,7 @@ public fun CharSequence.minWith(comparator: Comparator): Char? { * Returns `true` if the char sequence has no characters. */ public fun CharSequence.none(): Boolean { - for (element in this) return false - return true + return isEmpty() } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 23fa64a8cec..539b4a4d93c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -10,8 +10,13 @@ fun aggregates(): List { inline(true) doc { f -> "Returns `true` if all ${f.element.pluralize()} match the given [predicate]." } returns("Boolean") - body { + body { f -> """ + ${when (f) { + Iterables -> "if (this is Collection && isEmpty()) return true" + Maps -> "if (isEmpty()) return true" + else -> "" + }} for (element in this) if (!predicate(element)) return false return true """ @@ -24,8 +29,13 @@ fun aggregates(): List { doc { f -> "Returns `true` if no ${f.element.pluralize()} match the given [predicate]." } returns("Boolean") - body { + body { f -> """ + ${when (f) { + Iterables -> "if (this is Collection && isEmpty()) return true" + Maps -> "if (isEmpty()) return true" + else -> "" + }} for (element in this) if (predicate(element)) return false return true """ @@ -37,11 +47,17 @@ fun aggregates(): List { doc { f -> "Returns `true` if the ${f.collection} has no ${f.element.pluralize()}." } returns("Boolean") body { + "return !iterator().hasNext()" + } + body(Iterables) { """ - for (element in this) return false - return true + if (this is Collection) return isEmpty() + return !iterator().hasNext() """ } + body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) { + "return isEmpty()" + } include(Maps, CharSequences) } @@ -50,8 +66,13 @@ fun aggregates(): List { doc { f -> "Returns `true` if at least one ${f.element} matches the given [predicate]." } returns("Boolean") - body { + body { f -> """ + ${when (f) { + Iterables -> "if (this is Collection && isEmpty()) return false" + Maps -> "if (isEmpty()) return false" + else -> "" + }} for (element in this) if (predicate(element)) return true return false """ @@ -63,11 +84,17 @@ fun aggregates(): List { doc { f -> "Returns `true` if ${f.collection} has at least one ${f.element}." } returns("Boolean") body { + "return iterator().hasNext()" + } + body(Iterables) { """ - for (element in this) return true - return false + if (this is Collection) return !isEmpty() + return iterator().hasNext() """ } + body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) { + "return !isEmpty()" + } include(Maps, CharSequences) } @@ -76,8 +103,13 @@ fun aggregates(): List { doc { f -> "Returns the number of ${f.element.pluralize()} matching the given [predicate]." } returns("Int") - body { + body { f -> """ + ${when (f) { + Iterables -> "if (this is Collection && isEmpty()) return 0" + Maps -> "if (isEmpty()) return 0" + else -> "" + }} var count = 0 for (element in this) if (predicate(element)) count++ return count @@ -89,8 +121,9 @@ fun aggregates(): List { templates add f("count()") { doc { f -> "Returns the number of ${f.element.pluralize()} in this ${f.collection}." } returns("Int") - body { + body { f -> """ + ${if (f == Iterables) "if (this is Collection) return size" else "" } var count = 0 for (element in this) count++ return count