Undeprecate find(predicate) and generate it for all collection-like types. Provide findLast(predicate).
Drop deprecated method findNot and extension properties first, last, head, tail #KT-5185 Fixed
This commit is contained in:
+9
-5
@@ -1,14 +1,18 @@
|
||||
package foo
|
||||
|
||||
public fun <T> List<T>.some(): T = this[0]
|
||||
public fun String.some(): Char = this[0]
|
||||
public val <T> List<T>.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"
|
||||
}
|
||||
@@ -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 <T> Array<out T>.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 <T> Iterable<T>.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 <T> Sequence<T>.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 <T> Array<out T>.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 <T> Iterable<T>.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 <T> List<T>.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 <T> Sequence<T>.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.
|
||||
|
||||
@@ -2,12 +2,6 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
deprecated("Use firstOrNull function instead.", ReplaceWith("firstOrNull(predicate)"))
|
||||
public inline fun <T> Array<out T>.find(predicate: (T) -> Boolean): T? = firstOrNull(predicate)
|
||||
|
||||
deprecated("Use firstOrNull function instead.", ReplaceWith("firstOrNull(predicate)"))
|
||||
public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? = firstOrNull(predicate)
|
||||
|
||||
deprecated("Use listOf(...) or arrayListOf(...) instead", ReplaceWith("arrayListOf(*values)"))
|
||||
public fun arrayList<T>(vararg values: T): ArrayList<T> = arrayListOf(*values)
|
||||
|
||||
@@ -27,27 +21,6 @@ public fun <K, V> linkedMap(vararg values: Pair<K, V>): LinkedHashMap<K, V> = li
|
||||
deprecated("Use toList() instead.", ReplaceWith("toList()"))
|
||||
public fun String.toCollection(): Collection<Char> = toCollection(ArrayList<Char>(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 <T : Any> 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 <T> List<T>.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 <T> List<T>.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 <T> List<T>.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 <T> List<T>.tail: List<T>
|
||||
get() {
|
||||
return drop(1)
|
||||
}
|
||||
|
||||
/** Returns true if this collection is empty */
|
||||
deprecated("Use isEmpty() function call instead", ReplaceWith("isEmpty()"))
|
||||
public val Collection<*>.empty: Boolean
|
||||
|
||||
@@ -398,6 +398,14 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
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<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
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." }
|
||||
|
||||
Reference in New Issue
Block a user