Create method Collection.randomOrNull() #KT-35347
This commit is contained in:
@@ -2418,6 +2418,195 @@ public fun CharArray.random(random: Random): Char {
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Array<out T>.randomOrNull(): T? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.randomOrNull(): Byte? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.randomOrNull(): Short? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.randomOrNull(): Int? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.randomOrNull(): Long? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.randomOrNull(): Float? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.randomOrNull(): Double? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.randomOrNull(): Boolean? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.randomOrNull(): Char? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun <T> Array<out T>.randomOrNull(random: Random): T? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun ByteArray.randomOrNull(random: Random): Byte? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun ShortArray.randomOrNull(random: Random): Short? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun IntArray.randomOrNull(random: Random): Int? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun LongArray.randomOrNull(random: Random): Long? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun FloatArray.randomOrNull(random: Random): Float? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun DoubleArray.randomOrNull(random: Random): Double? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun BooleanArray.randomOrNull(random: Random): Boolean? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun CharArray.randomOrNull(random: Random): Char? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the single element, or throws an exception if the array is empty or has more than one element.
|
||||
*/
|
||||
|
||||
@@ -501,6 +501,27 @@ public fun <T> Collection<T>.random(random: Random): T {
|
||||
return elementAt(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this collection, or `null` if this collection is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Collection<T>.randomOrNull(): T? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this collection using the specified source of randomness, or `null` if this collection is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun <T> Collection<T>.randomOrNull(random: Random): T? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return elementAt(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the single element, or throws an exception if the collection is empty or has more than one element.
|
||||
*/
|
||||
|
||||
@@ -90,6 +90,69 @@ public fun CharRange.random(random: Random): Char {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntRange.randomOrNull(): Int? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongRange.randomOrNull(): Long? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharRange.randomOrNull(): Char? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range using the specified source of randomness, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun IntRange.randomOrNull(random: Random): Int? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return random.nextInt(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range using the specified source of randomness, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun LongRange.randomOrNull(random: Random): Long? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return random.nextLong(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range using the specified source of randomness, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun CharRange.randomOrNull(random: Random): Char? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return random.nextInt(first.toInt(), last.toInt() + 1).toChar()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if this range contains the specified [element].
|
||||
*
|
||||
|
||||
@@ -194,6 +194,27 @@ public fun CharSequence.random(random: Random): Char {
|
||||
return get(random.nextInt(length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random character from this char sequence, or `null` if this char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence.randomOrNull(): Char? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random character from this char sequence using the specified source of randomness, or `null` if this char sequence is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
public fun CharSequence.randomOrNull(random: Random): Char? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the single character, or throws an exception if the char sequence is empty or has more than one character.
|
||||
*/
|
||||
|
||||
@@ -1194,6 +1194,98 @@ public fun UShortArray.random(random: Random): UShort {
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.randomOrNull(): UInt? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.randomOrNull(): ULong? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.randomOrNull(): UByte? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.randomOrNull(): UShort? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntArray.randomOrNull(random: Random): UInt? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongArray.randomOrNull(random: Random): ULong? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UByteArray.randomOrNull(random: Random): UByte? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this array using the specified source of randomness, or `null` if this array is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UShortArray.randomOrNull(random: Random): UShort? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return get(random.nextInt(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the single element, or throws an exception if the array is empty or has more than one element.
|
||||
*/
|
||||
|
||||
@@ -69,6 +69,52 @@ public fun ULongRange.random(random: Random): ULong {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntRange.randomOrNull(): UInt? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongRange.randomOrNull(): ULong? {
|
||||
return randomOrNull(Random)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range using the specified source of randomness, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntRange.randomOrNull(random: Random): UInt? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return random.nextUInt(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random element from this range using the specified source of randomness, or `null` if this range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalStdlibApi
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongRange.randomOrNull(random: Random): ULong? {
|
||||
if (isEmpty())
|
||||
return null
|
||||
return random.nextULong(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if this range contains the specified [element].
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user