Specialize Progression first/last/OrNull functions #KT-42178

This commit is contained in:
Abduqodiri Qurbonzoda
2022-02-07 17:13:59 +03:00
committed by Space
parent cca2680c7f
commit 42c9a27b8a
16 changed files with 513 additions and 39 deletions
+1 -1
View File
@@ -3231,4 +3231,4 @@ public final annotation class UseExperimental : kotlin.Annotation {
public constructor UseExperimental(vararg markerClass: kotlin.reflect.KClass<out kotlin.Annotation>)
public final val markerClass: kotlin.Array<out kotlin.reflect.KClass<out kotlin.Annotation>> { get; }
}
}
@@ -327,6 +327,66 @@ public infix fun kotlin.ULong.downTo(to: kotlin.ULong): kotlin.ranges.ULongProgr
@kotlin.WasExperimental(markerClass = {kotlin.ExperimentalUnsignedTypes::class})
public infix fun kotlin.UShort.downTo(to: kotlin.UShort): kotlin.ranges.UIntProgression
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.first(): kotlin.Char
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.first(): kotlin.Int
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.first(): kotlin.Long
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.first(): kotlin.UInt
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.first(): kotlin.ULong
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.firstOrNull(): kotlin.Char?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.firstOrNull(): kotlin.Int?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.firstOrNull(): kotlin.Long?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.firstOrNull(): kotlin.UInt?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.firstOrNull(): kotlin.ULong?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.last(): kotlin.Char
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.last(): kotlin.Int
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.last(): kotlin.Long
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.last(): kotlin.UInt
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.last(): kotlin.ULong
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.lastOrNull(): kotlin.Char?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.lastOrNull(): kotlin.Int?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.lastOrNull(): kotlin.Long?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.lastOrNull(): kotlin.UInt?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.lastOrNull(): kotlin.ULong?
@kotlin.SinceKotlin(version = "1.3")
@kotlin.internal.InlineOnly
public inline fun kotlin.ranges.CharRange.random(): kotlin.Char
+1 -1
View File
@@ -3231,4 +3231,4 @@ public final annotation class UseExperimental : kotlin.Annotation {
public constructor UseExperimental(vararg markerClass: kotlin.reflect.KClass<out kotlin.Annotation>)
public final val markerClass: kotlin.Array<out kotlin.reflect.KClass<out kotlin.Annotation>> { get; }
}
}
+60
View File
@@ -327,6 +327,66 @@ public infix fun kotlin.ULong.downTo(to: kotlin.ULong): kotlin.ranges.ULongProgr
@kotlin.WasExperimental(markerClass = {kotlin.ExperimentalUnsignedTypes::class})
public infix fun kotlin.UShort.downTo(to: kotlin.UShort): kotlin.ranges.UIntProgression
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.first(): kotlin.Char
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.first(): kotlin.Int
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.first(): kotlin.Long
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.first(): kotlin.UInt
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.first(): kotlin.ULong
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.firstOrNull(): kotlin.Char?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.firstOrNull(): kotlin.Int?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.firstOrNull(): kotlin.Long?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.firstOrNull(): kotlin.UInt?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.firstOrNull(): kotlin.ULong?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.last(): kotlin.Char
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.last(): kotlin.Int
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.last(): kotlin.Long
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.last(): kotlin.UInt
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.last(): kotlin.ULong
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.CharProgression.lastOrNull(): kotlin.Char?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.IntProgression.lastOrNull(): kotlin.Int?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.LongProgression.lastOrNull(): kotlin.Long?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.UIntProgression.lastOrNull(): kotlin.UInt?
@kotlin.SinceKotlin(version = "1.7")
public fun kotlin.ranges.ULongProgression.lastOrNull(): kotlin.ULong?
@kotlin.SinceKotlin(version = "1.3")
@kotlin.internal.InlineOnly
public inline fun kotlin.ranges.CharRange.random(): kotlin.Char
@@ -1005,8 +1005,9 @@ public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun <T> Array<out T>.first(): T {
if (isEmpty())
@@ -1015,8 +1016,9 @@ public fun <T> Array<out T>.first(): T {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun ByteArray.first(): Byte {
if (isEmpty())
@@ -1025,8 +1027,9 @@ public fun ByteArray.first(): Byte {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun ShortArray.first(): Short {
if (isEmpty())
@@ -1035,8 +1038,9 @@ public fun ShortArray.first(): Short {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun IntArray.first(): Int {
if (isEmpty())
@@ -1045,8 +1049,9 @@ public fun IntArray.first(): Int {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun LongArray.first(): Long {
if (isEmpty())
@@ -1055,8 +1060,9 @@ public fun LongArray.first(): Long {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun FloatArray.first(): Float {
if (isEmpty())
@@ -1065,8 +1071,9 @@ public fun FloatArray.first(): Float {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun DoubleArray.first(): Double {
if (isEmpty())
@@ -1075,8 +1082,9 @@ public fun DoubleArray.first(): Double {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun BooleanArray.first(): Boolean {
if (isEmpty())
@@ -1085,8 +1093,9 @@ public fun BooleanArray.first(): Boolean {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
public fun CharArray.first(): Char {
if (isEmpty())
@@ -188,8 +188,9 @@ public inline fun <T> List<T>.findLast(predicate: (T) -> Boolean): T? {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the collection is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the collection is empty.
*/
public fun <T> Iterable<T>.first(): T {
when (this) {
@@ -204,8 +205,9 @@ public fun <T> Iterable<T>.first(): T {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the list is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the list is empty.
*/
public fun <T> List<T>.first(): T {
if (isEmpty())
@@ -15,6 +15,138 @@ package kotlin.ranges
import kotlin.random.*
/**
* Returns the first element.
*
* @throws NoSuchElementException if the progression is empty.
*/
@SinceKotlin("1.7")
public fun IntProgression.first(): Int {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
/**
* Returns the first element.
*
* @throws NoSuchElementException if the progression is empty.
*/
@SinceKotlin("1.7")
public fun LongProgression.first(): Long {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
/**
* Returns the first element.
*
* @throws NoSuchElementException if the progression is empty.
*/
@SinceKotlin("1.7")
public fun CharProgression.first(): Char {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
/**
* Returns the first element, or `null` if the progression is empty.
*/
@SinceKotlin("1.7")
public fun IntProgression.firstOrNull(): Int? {
return if (isEmpty()) null else this.first
}
/**
* Returns the first element, or `null` if the progression is empty.
*/
@SinceKotlin("1.7")
public fun LongProgression.firstOrNull(): Long? {
return if (isEmpty()) null else this.first
}
/**
* Returns the first element, or `null` if the progression is empty.
*/
@SinceKotlin("1.7")
public fun CharProgression.firstOrNull(): Char? {
return if (isEmpty()) null else this.first
}
/**
* Returns the last element.
*
* @throws NoSuchElementException if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun IntProgression.last(): Int {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
/**
* Returns the last element.
*
* @throws NoSuchElementException if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun LongProgression.last(): Long {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
/**
* Returns the last element.
*
* @throws NoSuchElementException if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun CharProgression.last(): Char {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
/**
* Returns the last element, or `null` if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun IntProgression.lastOrNull(): Int? {
return if (isEmpty()) null else this.last
}
/**
* Returns the last element, or `null` if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun LongProgression.lastOrNull(): Long? {
return if (isEmpty()) null else this.last
}
/**
* Returns the last element, or `null` if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun CharProgression.lastOrNull(): Char? {
return if (isEmpty()) null else this.last
}
/**
* Returns a random element from this range.
*
@@ -100,10 +100,11 @@ public inline fun <T> Sequence<T>.findLast(predicate: (T) -> Boolean): T? {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the sequence is empty.
* Returns the first element.
*
* The operation is _terminal_.
*
* @throws NoSuchElementException if the sequence is empty.
*/
public fun <T> Sequence<T>.first(): T {
val iterator = iterator()
@@ -63,8 +63,9 @@ public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
}
/**
* Returns first character.
* @throws [NoSuchElementException] if the char sequence is empty.
* Returns the first character.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
public fun CharSequence.first(): Char {
if (isEmpty())
@@ -507,8 +507,9 @@ public inline fun UShortArray.findLast(predicate: (UShort) -> Boolean): UShort?
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -518,8 +519,9 @@ public inline fun UIntArray.first(): UInt {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -529,8 +531,9 @@ public inline fun ULongArray.first(): ULong {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -540,8 +543,9 @@ public inline fun UByteArray.first(): UByte {
}
/**
* Returns first element.
* @throws [NoSuchElementException] if the array is empty.
* Returns the first element.
*
* @throws NoSuchElementException if the array is empty.
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -15,6 +15,94 @@ package kotlin.ranges
import kotlin.random.*
/**
* Returns the first element.
*
* @throws NoSuchElementException if the progression is empty.
*/
@SinceKotlin("1.7")
public fun UIntProgression.first(): UInt {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
/**
* Returns the first element.
*
* @throws NoSuchElementException if the progression is empty.
*/
@SinceKotlin("1.7")
public fun ULongProgression.first(): ULong {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
/**
* Returns the first element, or `null` if the progression is empty.
*/
@SinceKotlin("1.7")
public fun UIntProgression.firstOrNull(): UInt? {
return if (isEmpty()) null else this.first
}
/**
* Returns the first element, or `null` if the progression is empty.
*/
@SinceKotlin("1.7")
public fun ULongProgression.firstOrNull(): ULong? {
return if (isEmpty()) null else this.first
}
/**
* Returns the last element.
*
* @throws NoSuchElementException if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun UIntProgression.last(): UInt {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
/**
* Returns the last element.
*
* @throws NoSuchElementException if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun ULongProgression.last(): ULong {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
/**
* Returns the last element, or `null` if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun UIntProgression.lastOrNull(): UInt? {
return if (isEmpty()) null else this.last
}
/**
* Returns the last element, or `null` if the progression is empty.
*
* @sample samples.collections.Collections.Elements.last
*/
@SinceKotlin("1.7")
public fun ULongProgression.lastOrNull(): ULong? {
return if (isEmpty()) null else this.last
}
/**
* Returns a random element from this range.
*
+28
View File
@@ -399,4 +399,32 @@ public class RangeTest {
assertNull(LongRange.EMPTY.randomOrNull())
assertNull(CharRange.EMPTY.randomOrNull())
}
@Test fun firstInEmptyRange() {
assertFailsWith<NoSuchElementException> { IntRange.EMPTY.first() }
assertFailsWith<NoSuchElementException> { LongRange.EMPTY.first() }
assertFailsWith<NoSuchElementException> { CharRange.EMPTY.first() }
assertFailsWith<NoSuchElementException> { IntProgression.fromClosedRange(0, 3, -2).first() }
}
@Test fun firstOrNullInEmptyRange() {
assertNull(IntRange.EMPTY.firstOrNull())
assertNull(LongRange.EMPTY.firstOrNull())
assertNull(CharRange.EMPTY.firstOrNull())
assertNull(IntProgression.fromClosedRange(0, 3, -2).firstOrNull())
}
@Test fun lastInEmptyRange() {
assertFailsWith<NoSuchElementException> { IntRange.EMPTY.last() }
assertFailsWith<NoSuchElementException> { LongRange.EMPTY.last() }
assertFailsWith<NoSuchElementException> { CharRange.EMPTY.last() }
assertFailsWith<NoSuchElementException> { IntProgression.fromClosedRange(0, 3, -2).last() }
}
@Test fun lastOrNullInEmptyRange() {
assertNull(IntRange.EMPTY.lastOrNull())
assertNull(LongRange.EMPTY.lastOrNull())
assertNull(CharRange.EMPTY.lastOrNull())
assertNull(IntProgression.fromClosedRange(0, 3, -2).lastOrNull())
}
}
@@ -241,4 +241,28 @@ public class URangeTest {
assertFailsWith<NoSuchElementException> { UIntRange.EMPTY.random() }
assertFailsWith<NoSuchElementException> { ULongRange.EMPTY.random() }
}
@Test fun firstInEmptyRange() {
assertFailsWith<NoSuchElementException> { UIntRange.EMPTY.first() }
assertFailsWith<NoSuchElementException> { ULongRange.EMPTY.first() }
assertFailsWith<NoSuchElementException> { UIntProgression.fromClosedRange(0u, 3u, -2).first() }
}
@Test fun firstOrNullInEmptyRange() {
assertNull(UIntRange.EMPTY.firstOrNull())
assertNull(ULongRange.EMPTY.firstOrNull())
assertNull(UIntProgression.fromClosedRange(0u, 3u, -2).firstOrNull())
}
@Test fun lastInEmptyRange() {
assertFailsWith<NoSuchElementException> { UIntRange.EMPTY.last() }
assertFailsWith<NoSuchElementException> { ULongRange.EMPTY.last() }
assertFailsWith<NoSuchElementException> { UIntProgression.fromClosedRange(0u, 3u, -2).last() }
}
@Test fun lastOrNullInEmptyRange() {
assertNull(UIntRange.EMPTY.lastOrNull())
assertNull(ULongRange.EMPTY.lastOrNull())
assertNull(UIntProgression.fromClosedRange(0u, 3u, -2).lastOrNull())
}
}