Create method Collection.randomOrNull() #KT-35347

This commit is contained in:
Abduqodiri Qurbonzoda
2020-01-15 22:16:47 +03:00
parent 80d5723a07
commit 3cad1bbb51
12 changed files with 589 additions and 0 deletions
@@ -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.
*/