diff --git a/js/js.translator/testData/nameClashes/cases/extensionFunctionAndProperty.kt b/js/js.translator/testData/nameClashes/cases/extensionFunctionAndProperty.kt index 42fcaa4b366..06e670b4d8c 100644 --- a/js/js.translator/testData/nameClashes/cases/extensionFunctionAndProperty.kt +++ b/js/js.translator/testData/nameClashes/cases/extensionFunctionAndProperty.kt @@ -1,14 +1,18 @@ package foo +public fun List.some(): T = this[0] +public fun String.some(): Char = this[0] +public val List.some: T get() = this[1] +public val String.some: Char get() = this[1] + fun box(): String { val data = listOf("foo", "bar") - assertEquals("bar", data.last()) - assertEquals("bar", data.last) - - assertEquals("foo", data.first) - assertEquals("foo", data.first()) + assertEquals("foo", data.some()) + assertEquals("bar", data.some) + assertEquals('f', "foo".some()) + assertEquals('a', "bar".some) return "OK" } \ No newline at end of file diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index e308581f8e1..01f77d2540e 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -806,6 +806,181 @@ public fun String.elementAtOrNull(index: Int): Char? { return if (index >= 0 && index <= lastIndex) get(index) else null } +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Array.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Iterable.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the first element matching the given [predicate], or `null` if element was not found. + */ +public inline fun Sequence.find(predicate: (T) -> Boolean): T? { + return firstOrNull(predicate) +} + +/** + * Returns the first character matching the given [predicate], or `null` if character was not found. + */ +public inline fun String.find(predicate: (Char) -> Boolean): Char? { + return firstOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Array.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Iterable.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun List.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns the last element matching the given [predicate], or `null` if no such element was found. + */ +public inline fun Sequence.findLast(predicate: (T) -> Boolean): T? { + return lastOrNull(predicate) +} + +/** + * Returns the last character matching the given [predicate], or `null` if no such character was found. + */ +public inline fun String.findLast(predicate: (Char) -> Boolean): Char? { + return lastOrNull(predicate) +} + /** * Returns first element. * @throws [NoSuchElementException] if the collection is empty. diff --git a/libraries/stdlib/src/kotlin/deprecated/Deprecated.kt b/libraries/stdlib/src/kotlin/deprecated/Deprecated.kt index d6d8ffe42db..9a80da5e253 100644 --- a/libraries/stdlib/src/kotlin/deprecated/Deprecated.kt +++ b/libraries/stdlib/src/kotlin/deprecated/Deprecated.kt @@ -2,12 +2,6 @@ package kotlin import java.util.* -deprecated("Use firstOrNull function instead.", ReplaceWith("firstOrNull(predicate)")) -public inline fun Array.find(predicate: (T) -> Boolean): T? = firstOrNull(predicate) - -deprecated("Use firstOrNull function instead.", ReplaceWith("firstOrNull(predicate)")) -public inline fun Iterable.find(predicate: (T) -> Boolean): T? = firstOrNull(predicate) - deprecated("Use listOf(...) or arrayListOf(...) instead", ReplaceWith("arrayListOf(*values)")) public fun arrayList(vararg values: T): ArrayList = arrayListOf(*values) @@ -27,27 +21,6 @@ public fun linkedMap(vararg values: Pair): LinkedHashMap = li deprecated("Use toList() instead.", ReplaceWith("toList()")) public fun String.toCollection(): Collection = toCollection(ArrayList(this.length())) -/** - * Returns the first character which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/text/StringTest.kt find - */ -deprecated("Use firstOrNull instead", ReplaceWith("firstOrNull(predicate)")) -public inline fun String.find(predicate: (Char) -> Boolean): Char? { - for (c in this) if (predicate(c)) return c - return null -} - -/** - * Returns the first character which does not match the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/text/StringTest.kt findNot - */ -deprecated("Use firstOrNull instead with negated predicate") -public inline fun String.findNot(predicate: (Char) -> Boolean): Char? { - for (c in this) if (!predicate(c)) return c - return null -} /** * A helper method for creating a [[Runnable]] from a function @@ -101,49 +74,6 @@ deprecated("Use compareValuesBy() instead", ReplaceWith("compareValuesBy(a, b, * public fun compareBy(a: T?, b: T?, vararg functions: (T) -> Comparable<*>?): Int = compareValuesBy(a, b, *functions) -/** - * Returns the first item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt first - */ -deprecated("Use firstOrNull() function instead", ReplaceWith("this.firstOrNull()")) -public val List.first: T? - get() = this.firstOrNull() - - -/** - * Returns the last item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt last - */ -deprecated("Use lastOrNull() function instead", ReplaceWith("this.lastOrNull()")) -public val List.last: T? - get() { - val s = this.size() - return if (s > 0) this[s - 1] else null - } - - -/** - * Returns the first item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt head - */ -deprecated("Use firstOrNull() function instead", ReplaceWith("firstOrNull()")) -public val List.head: T? - get() = firstOrNull() - -/** - * Returns all elements in this collection apart from the first one - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt tail - */ -deprecated("Use drop(1) function call instead", ReplaceWith("drop(1)")) -public val List.tail: List - get() { - return drop(1) - } - /** Returns true if this collection is empty */ deprecated("Use isEmpty() function call instead", ReplaceWith("isEmpty()")) public val Collection<*>.empty: Boolean diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 00a5bf4519b..e2224eed193 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -398,6 +398,14 @@ fun elements(): List { } } + templates add f("find(predicate: (T) -> Boolean)") { + inline(true) + doc { "Returns the first element matching the given [predicate], or `null` if element was not found." } + doc(Strings) { "Returns the first character matching the given [predicate], or `null` if character was not found." } + returns("T?") + body { "return firstOrNull(predicate)"} + } + templates add f("last()") { doc { """Returns the last element. @throws [NoSuchElementException] if the collection is empty.""" } @@ -584,6 +592,15 @@ fun elements(): List { } } + templates add f("findLast(predicate: (T) -> Boolean)") { + inline(true) + include(Lists) + doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." } + doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." } + returns("T?") + body { "return lastOrNull(predicate)"} + } + templates add f("single()") { doc { "Returns the single element, or throws an exception if the collection is empty or has more than one element." } doc(Strings) { "Returns the single character, or throws an exception if the string is empty or has more than one character." }