Add samples for mapNotNull, find, getOrNull functions

This commit is contained in:
Elijah Verdoorn
2020-04-26 18:03:11 -07:00
committed by Abduqodiri Qurbonzoda
parent 62df2b3195
commit 41d5615608
10 changed files with 154 additions and 0 deletions
@@ -826,6 +826,8 @@ public inline fun CharArray.elementAtOrNull(index: Int): Char? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.find(predicate: (T) -> Boolean): T? {
@@ -834,6 +836,8 @@ public inline fun <T> Array<out T>.find(predicate: (T) -> Boolean): T? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? {
@@ -842,6 +846,8 @@ public inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? {
@@ -850,6 +856,8 @@ public inline fun ShortArray.find(predicate: (Short) -> Boolean): Short? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? {
@@ -858,6 +866,8 @@ public inline fun IntArray.find(predicate: (Int) -> Boolean): Int? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? {
@@ -866,6 +876,8 @@ public inline fun LongArray.find(predicate: (Long) -> Boolean): Long? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? {
@@ -874,6 +886,8 @@ public inline fun FloatArray.find(predicate: (Float) -> Boolean): Float? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? {
@@ -882,6 +896,8 @@ public inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? {
@@ -890,6 +906,8 @@ public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? {
@@ -898,6 +916,8 @@ public inline fun CharArray.find(predicate: (Char) -> Boolean): Char? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.findLast(predicate: (T) -> Boolean): T? {
@@ -906,6 +926,8 @@ public inline fun <T> Array<out T>.findLast(predicate: (T) -> Boolean): T? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? {
@@ -914,6 +936,8 @@ public inline fun ByteArray.findLast(predicate: (Byte) -> Boolean): Byte? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? {
@@ -922,6 +946,8 @@ public inline fun ShortArray.findLast(predicate: (Short) -> Boolean): Short? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? {
@@ -930,6 +956,8 @@ public inline fun IntArray.findLast(predicate: (Int) -> Boolean): Int? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? {
@@ -938,6 +966,8 @@ public inline fun LongArray.findLast(predicate: (Long) -> Boolean): Long? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? {
@@ -946,6 +976,8 @@ public inline fun FloatArray.findLast(predicate: (Float) -> Boolean): Float? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double? {
@@ -954,6 +986,8 @@ public inline fun DoubleArray.findLast(predicate: (Double) -> Boolean): Double?
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolean? {
@@ -962,6 +996,8 @@ public inline fun BooleanArray.findLast(predicate: (Boolean) -> Boolean): Boolea
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? {
@@ -1348,6 +1384,8 @@ public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char):
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun <T> Array<out T>.getOrNull(index: Int): T? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1355,6 +1393,8 @@ public fun <T> Array<out T>.getOrNull(index: Int): T? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun ByteArray.getOrNull(index: Int): Byte? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1362,6 +1402,8 @@ public fun ByteArray.getOrNull(index: Int): Byte? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun ShortArray.getOrNull(index: Int): Short? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1369,6 +1411,8 @@ public fun ShortArray.getOrNull(index: Int): Short? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun IntArray.getOrNull(index: Int): Int? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1376,6 +1420,8 @@ public fun IntArray.getOrNull(index: Int): Int? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun LongArray.getOrNull(index: Int): Long? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1383,6 +1429,8 @@ public fun LongArray.getOrNull(index: Int): Long? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun FloatArray.getOrNull(index: Int): Float? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1390,6 +1438,8 @@ public fun FloatArray.getOrNull(index: Int): Float? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun DoubleArray.getOrNull(index: Int): Double? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1397,6 +1447,8 @@ public fun DoubleArray.getOrNull(index: Int): Double? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun BooleanArray.getOrNull(index: Int): Boolean? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1404,6 +1456,8 @@ public fun BooleanArray.getOrNull(index: Int): Boolean? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun CharArray.getOrNull(index: Int): Char? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -11465,6 +11519,8 @@ public inline fun <R, C : MutableCollection<in R>> CharArray.mapIndexedTo(destin
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each element in the original array.
*
* @sample samples.collections.Collections.Transformations.mapNotNull
*/
public inline fun <T, R : Any> Array<out T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
@@ -159,6 +159,8 @@ public inline fun <T> List<T>.elementAtOrNull(index: Int): T? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? {
@@ -167,6 +169,8 @@ public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.findLast(predicate: (T) -> Boolean): T? {
@@ -175,6 +179,8 @@ public inline fun <T> Iterable<T>.findLast(predicate: (T) -> Boolean): T? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> List<T>.findLast(predicate: (T) -> Boolean): T? {
@@ -261,6 +267,8 @@ public inline fun <T> List<T>.getOrElse(index: Int, defaultValue: (Int) -> T): T
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this list.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun <T> List<T>.getOrNull(index: Int): T? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -1536,6 +1544,8 @@ public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapIndexedTo(d
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each element in the original collection.
*
* @sample samples.collections.Collections.Transformations.mapNotNull
*/
public inline fun <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
@@ -98,6 +98,8 @@ public inline fun <K, V, R> Map<out K, V>.map(transform: (Map.Entry<K, V>) -> R)
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each entry in the original map.
*
* @sample samples.collections.Maps.Transformations.mapNotNull
*/
public inline fun <K, V, R : Any> Map<out K, V>.mapNotNull(transform: (Map.Entry<K, V>) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
@@ -79,6 +79,8 @@ public fun <T> Sequence<T>.elementAtOrNull(index: Int): T? {
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.find(predicate: (T) -> Boolean): T? {
@@ -89,6 +91,8 @@ public inline fun <T> Sequence<T>.find(predicate: (T) -> Boolean): T? {
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun <T> Sequence<T>.findLast(predicate: (T) -> Boolean): T? {
@@ -1060,6 +1064,8 @@ public inline fun <T, R, C : MutableCollection<in R>> Sequence<T>.mapIndexedTo(d
* to each element in the original sequence.
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Transformations.mapNotNull
*/
public fun <T, R : Any> Sequence<T>.mapNotNull(transform: (T) -> R?): Sequence<R> {
return TransformingSequence(this, transform).filterNotNull()
@@ -44,6 +44,8 @@ public inline fun CharSequence.elementAtOrNull(index: Int): Char? {
/**
* Returns the first character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
@@ -52,6 +54,8 @@ public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
/**
* Returns the last character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
@@ -102,6 +106,8 @@ public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char
/**
* Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun CharSequence.getOrNull(index: Int): Char? {
return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -970,6 +976,8 @@ public inline fun <R, C : MutableCollection<in R>> CharSequence.mapIndexedTo(des
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each character in the original char sequence.
*
* @sample samples.collections.Collections.Transformations.mapNotNull
*/
public inline fun <R : Any> CharSequence.mapNotNull(transform: (Char) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
@@ -412,6 +412,8 @@ public inline fun UShortArray.elementAtOrNull(index: Int): UShort? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -422,6 +424,8 @@ public inline fun UIntArray.find(predicate: (UInt) -> Boolean): UInt? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -432,6 +436,8 @@ public inline fun ULongArray.find(predicate: (ULong) -> Boolean): ULong? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -442,6 +448,8 @@ public inline fun UByteArray.find(predicate: (UByte) -> Boolean): UByte? {
/**
* Returns the first element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -452,6 +460,8 @@ public inline fun UShortArray.find(predicate: (UShort) -> Boolean): UShort? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -462,6 +472,8 @@ public inline fun UIntArray.findLast(predicate: (UInt) -> Boolean): UInt? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -472,6 +484,8 @@ public inline fun ULongArray.findLast(predicate: (ULong) -> Boolean): ULong? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -482,6 +496,8 @@ public inline fun UByteArray.findLast(predicate: (UByte) -> Boolean): UByte? {
/**
* Returns the last element matching the given [predicate], or `null` if no such element was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -704,6 +720,8 @@ public inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShor
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -713,6 +731,8 @@ public fun UIntArray.getOrNull(index: Int): UInt? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -722,6 +742,8 @@ public fun ULongArray.getOrNull(index: Int): ULong? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -731,6 +753,8 @@ public fun UByteArray.getOrNull(index: Int): UByte? {
/**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes