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.
*/
@@ -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].
*
@@ -665,6 +665,29 @@ class ArraysTest {
assertFailsWith<NoSuchElementException> { emptyArray<Any>().random() }
}
@Test fun randomOrNull() {
Array(100) { it }.let { array ->
val tosses = List(10) { array.randomOrNull() }
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
val seed = Random.nextInt()
val random1 = Random(seed)
val random2 = Random(seed)
val tosses1 = List(10) { array.randomOrNull(random1) }
val tosses2 = List(10) { array.randomOrNull(random2) }
assertEquals(tosses1, tosses2)
}
arrayOf("x").let { singletonArray ->
val tosses = List(10) { singletonArray.randomOrNull() }
assertEquals(singletonArray.toList(), tosses.distinct())
}
assertNull(emptyArray<Any>().randomOrNull())
}
@Test fun contains() {
assertTrue(arrayOf("1", "2", "3", "4").contains("2"))
@@ -663,6 +663,31 @@ class CollectionTest {
assertFailsWith<NoSuchElementException> { emptyList<Any>().random() }
}
@Test fun randomOrNull() {
val list = List(100) { it }
val set = list.toSet()
listOf(list, set).forEach { collection: Collection<Int> ->
val tosses = List(10) { collection.randomOrNull() }
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
val seed = Random.nextInt()
val random1 = Random(seed)
val random2 = Random(seed)
val tosses1 = List(10) { collection.randomOrNull(random1) }
val tosses2 = List(10) { collection.randomOrNull(random2) }
assertEquals(tosses1, tosses2)
}
listOf("x").let { singletonList ->
val tosses = List(10) { singletonList.randomOrNull() }
assertEquals(singletonList, tosses.distinct())
}
assertNull(emptyList<Any>().randomOrNull())
}
@Test fun subscript() {
val list = arrayListOf("foo", "bar")
assertEquals("foo", list[0])
@@ -423,4 +423,10 @@ public class RangeTest {
assertFailsWith<NoSuchElementException> { LongRange.EMPTY.random() }
assertFailsWith<NoSuchElementException> { CharRange.EMPTY.random() }
}
@Test fun randomOrNullInEmptyRange() {
assertNull(IntRange.EMPTY.randomOrNull())
assertNull(LongRange.EMPTY.randomOrNull())
assertNull(CharRange.EMPTY.randomOrNull())
}
}
+23
View File
@@ -1029,6 +1029,29 @@ class StringTest {
assertFailsWith<NoSuchElementException> { data("").random() }
}
@Test fun randomOrNull() = withOneCharSequenceArg { data ->
data("abcdefg").let { charSeq ->
val tosses = List(10) { charSeq.randomOrNull() }
assertTrue(tosses.distinct().size > 1, "Should be some distinct elements in $tosses")
val seed = Random.nextInt()
val random1 = Random(seed)
val random2 = Random(seed)
val tosses1 = List(10) { charSeq.randomOrNull(random1) }
val tosses2 = List(10) { charSeq.randomOrNull(random2) }
assertEquals(tosses1, tosses2)
}
data("x").let { singletonCharSeq ->
val tosses = List(10) { singletonCharSeq.randomOrNull() }
assertEquals(singletonCharSeq.toList(), tosses.distinct())
}
assertNull(data("").randomOrNull())
}
@Test fun partition() {
val data = "a1b2c3"
val pair = data.partition { it.isAsciiDigit() }