Introduce firstNotNullOf and firstNotNullOfOrNull #KT-12109
This commit is contained in:
@@ -1175,6 +1175,36 @@ public inline fun CharArray.first(predicate: (Char) -> Boolean): Char {
|
||||
throw NoSuchElementException("Array contains no element matching the predicate.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this array in iteration order,
|
||||
* or throws [NoSuchElementException] if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Array<out T>.firstNotNullOf(transform: (T) -> R?): R {
|
||||
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the array was transformed to a non-null value.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this array in iteration order,
|
||||
* or `null` if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Array<out T>.firstNotNullOfOrNull(transform: (T) -> R?): R? {
|
||||
for (element in this) {
|
||||
val result = transform(element)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element, or `null` if the array is empty.
|
||||
*/
|
||||
|
||||
@@ -222,6 +222,36 @@ public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
|
||||
throw NoSuchElementException("Collection contains no element matching the predicate.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this collection in iteration order,
|
||||
* or throws [NoSuchElementException] if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Iterable<T>.firstNotNullOf(transform: (T) -> R?): R {
|
||||
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the collection was transformed to a non-null value.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this collection in iteration order,
|
||||
* or `null` if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Iterable<T>.firstNotNullOfOrNull(transform: (T) -> R?): R? {
|
||||
for (element in this) {
|
||||
val result = transform(element)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element, or `null` if the collection is empty.
|
||||
*/
|
||||
|
||||
@@ -17,6 +17,36 @@ import kotlin.random.*
|
||||
import kotlin.ranges.contains
|
||||
import kotlin.ranges.reversed
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to entries of this map in iteration order,
|
||||
* or throws [NoSuchElementException] if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V, R : Any> Map<out K, V>.firstNotNullOf(transform: (Map.Entry<K, V>) -> R?): R {
|
||||
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the map was transformed to a non-null value.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to entries of this map in iteration order,
|
||||
* or `null` if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V, R : Any> Map<out K, V>.firstNotNullOfOrNull(transform: (Map.Entry<K, V>) -> R?): R? {
|
||||
for (element in this) {
|
||||
val result = transform(element)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] containing all key-value pairs.
|
||||
*/
|
||||
|
||||
@@ -123,6 +123,40 @@ public inline fun <T> Sequence<T>.first(predicate: (T) -> Boolean): T {
|
||||
throw NoSuchElementException("Sequence contains no element matching the predicate.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this sequence in iteration order,
|
||||
* or throws [NoSuchElementException] if no non-null value was produced.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Sequence<T>.firstNotNullOf(transform: (T) -> R?): R {
|
||||
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the sequence was transformed to a non-null value.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to elements of this sequence in iteration order,
|
||||
* or `null` if no non-null value was produced.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, R : Any> Sequence<T>.firstNotNullOfOrNull(transform: (T) -> R?): R? {
|
||||
for (element in this) {
|
||||
val result = transform(element)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element, or `null` if the sequence is empty.
|
||||
*
|
||||
|
||||
@@ -81,6 +81,36 @@ public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
|
||||
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to characters of this char sequence in iteration order,
|
||||
* or throws [NoSuchElementException] if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R : Any> CharSequence.firstNotNullOf(transform: (Char) -> R?): R {
|
||||
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the char sequence was transformed to a non-null value.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first non-null value produced by [transform] function being applied to characters of this char sequence in iteration order,
|
||||
* or `null` if no non-null value was produced.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.firstNotNullOf
|
||||
*/
|
||||
@SinceKotlin("1.5")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <R : Any> CharSequence.firstNotNullOfOrNull(transform: (Char) -> R?): R? {
|
||||
for (element in this) {
|
||||
val result = transform(element)
|
||||
if (result != null) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first character, or `null` if the char sequence is empty.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user