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
@@ -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.
*/