Package kotlin.ranges
Ranges, Progressions and related top-level and extension functions.
Types
CharProgression
A progression of values of type Char.
open class CharProgression : Iterable<Char>CharRange
A range of values of type Char.
class CharRange : CharProgression, ClosedRange<Char>ClosedFloatingPointRange
Represents a range of floating point numbers. Extends ClosedRange interface providing custom operation lessThanOrEquals for comparing values of range domain type.
interface ClosedFloatingPointRange<T : Comparable<T>> :
ClosedRange<T>ClosedRange
Represents a range of values (for example, numbers or characters). See the Kotlin language documentation for more information.
interface ClosedRange<T : Comparable<T>>IntProgression
A progression of values of type Int.
open class IntProgression : Iterable<Int>IntRange
A range of values of type Int.
class IntRange : IntProgression, ClosedRange<Int>LongProgression
A progression of values of type Long.
open class LongProgression : Iterable<Long>LongRange
A range of values of type Long.
class LongRange : LongProgression, ClosedRange<Long>UIntRange
A range of values of type UInt.
class UIntRange : UIntProgression, ClosedRange<UInt>ULongRange
A range of values of type ULong.
class ULongRange : ULongProgression, ClosedRange<ULong>Functions
coerceAtLeast
Ensures that this value is not less than the specified minimumValue.
fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): Tfun Byte.coerceAtLeast(minimumValue: Byte): Bytefun Short.coerceAtLeast(minimumValue: Short): Shortfun Int.coerceAtLeast(minimumValue: Int): Intfun Long.coerceAtLeast(minimumValue: Long): Longfun Float.coerceAtLeast(minimumValue: Float): Floatfun Double.coerceAtLeast(minimumValue: Double): DoublecoerceAtMost
Ensures that this value is not greater than the specified maximumValue.
fun <T : Comparable<T>> T.coerceAtMost(maximumValue: T): Tfun Byte.coerceAtMost(maximumValue: Byte): Bytefun Short.coerceAtMost(maximumValue: Short): Shortfun Int.coerceAtMost(maximumValue: Int): Intfun Long.coerceAtMost(maximumValue: Long): Longfun Float.coerceAtMost(maximumValue: Float): Floatfun Double.coerceAtMost(maximumValue: Double): DoublecoerceIn
Ensures that this value lies in the specified range minimumValue..maximumValue.
fun <T : Comparable<T>> T.coerceIn(
minimumValue: T?,
maximumValue: T?
): Tfun Byte.coerceIn(
minimumValue: Byte,
maximumValue: Byte
): Bytefun Short.coerceIn(
minimumValue: Short,
maximumValue: Short
): Shortfun Int.coerceIn(minimumValue: Int, maximumValue: Int): Intfun Long.coerceIn(
minimumValue: Long,
maximumValue: Long
): Longfun Float.coerceIn(
minimumValue: Float,
maximumValue: Float
): Floatfun Double.coerceIn(
minimumValue: Double,
maximumValue: Double
): DoubleEnsures that this value lies in the specified range.
fun <T : Comparable<T>> T.coerceIn(
range: ClosedFloatingPointRange<T>
): Tfun <T : Comparable<T>> T.coerceIn(range: ClosedRange<T>): Tfun Int.coerceIn(range: ClosedRange<Int>): Intfun Long.coerceIn(range: ClosedRange<Long>): Longfun UInt.coerceIn(range: ClosedRange<UInt>): UIntfun ULong.coerceIn(range: ClosedRange<ULong>): ULongcontains
Returns true if this range contains the specified element.
operator fun IntRange.contains(element: Int?): Booleanoperator fun LongRange.contains(element: Long?): Booleanoperator fun CharRange.contains(element: Char?): Booleanoperator fun ULongRange.contains(element: ULong?): BooleanChecks if the specified value belongs to this range.
operator fun ClosedRange<Int>.contains(value: Byte): Booleanoperator fun ClosedRange<Long>.contains(value: Byte): Booleanoperator fun ClosedRange<Short>.contains(
value: Byte
): Booleanoperator fun ClosedRange<Double>.contains(
value: Byte
): Booleanoperator fun ClosedRange<Float>.contains(
value: Byte
): Booleanoperator fun ClosedRange<Int>.contains(
value: Double
): Booleanoperator fun ClosedRange<Long>.contains(
value: Double
): Booleanoperator fun ClosedRange<Byte>.contains(
value: Double
): Booleanoperator fun ClosedRange<Short>.contains(
value: Double
): Booleanoperator fun ClosedRange<Float>.contains(
value: Double
): Booleanoperator fun ClosedRange<Int>.contains(value: Float): Booleanoperator fun ClosedRange<Long>.contains(
value: Float
): Booleanoperator fun ClosedRange<Byte>.contains(
value: Float
): Booleanoperator fun ClosedRange<Short>.contains(
value: Float
): Booleanoperator fun ClosedRange<Double>.contains(
value: Float
): Booleanoperator fun ClosedRange<Long>.contains(value: Int): Booleanoperator fun ClosedRange<Byte>.contains(value: Int): Booleanoperator fun ClosedRange<Short>.contains(value: Int): Booleanoperator fun ClosedRange<Double>.contains(
value: Int
): Booleanoperator fun ClosedRange<Float>.contains(value: Int): Booleanoperator fun ClosedRange<Int>.contains(value: Long): Booleanoperator fun ClosedRange<Byte>.contains(value: Long): Booleanoperator fun ClosedRange<Short>.contains(
value: Long
): Booleanoperator fun ClosedRange<Double>.contains(
value: Long
): Booleanoperator fun ClosedRange<Float>.contains(
value: Long
): Booleanoperator fun ClosedRange<Int>.contains(value: Short): Booleanoperator fun ClosedRange<Long>.contains(
value: Short
): Booleanoperator fun ClosedRange<Byte>.contains(
value: Short
): Booleanoperator fun ClosedRange<Double>.contains(
value: Short
): Booleanoperator fun ClosedRange<Float>.contains(
value: Short
): Booleanoperator fun ULongRange.contains(value: UByte): Booleanoperator fun ULongRange.contains(value: UInt): Booleanoperator fun ULongRange.contains(value: UShort): BooleanReturns true if this iterable range contains the specified element.
operator fun <T : Any, R> R.contains(
element: T?
): Boolean where R : Iterable<T>, R : ClosedRange<T>downTo
Returns a progression from this value down to the specified to value with the step -1.
infix fun Int.downTo(to: Byte): IntProgressioninfix fun Long.downTo(to: Byte): LongProgressioninfix fun Byte.downTo(to: Byte): IntProgressioninfix fun Short.downTo(to: Byte): IntProgressioninfix fun Char.downTo(to: Char): CharProgressioninfix fun Int.downTo(to: Int): IntProgressioninfix fun Long.downTo(to: Int): LongProgressioninfix fun Byte.downTo(to: Int): IntProgressioninfix fun Short.downTo(to: Int): IntProgressioninfix fun Int.downTo(to: Long): LongProgressioninfix fun Long.downTo(to: Long): LongProgressioninfix fun Byte.downTo(to: Long): LongProgressioninfix fun Short.downTo(to: Long): LongProgressioninfix fun Int.downTo(to: Short): IntProgressioninfix fun Long.downTo(to: Short): LongProgressioninfix fun Byte.downTo(to: Short): IntProgressioninfix fun Short.downTo(to: Short): IntProgressioninfix fun UByte.downTo(to: UByte): UIntProgressioninfix fun UInt.downTo(to: UInt): UIntProgressioninfix fun ULong.downTo(to: ULong): ULongProgressioninfix fun UShort.downTo(to: UShort): UIntProgressionrandom
Returns a random element from this range.
fun IntRange.random(): Intfun LongRange.random(): Longfun CharRange.random(): Charfun ULongRange.random(): ULongrandomOrNull
Returns a random element from this range, or null if this range is empty.
fun IntRange.randomOrNull(): Int?fun LongRange.randomOrNull(): Long?fun CharRange.randomOrNull(): Char?fun ULongRange.randomOrNull(): ULong?Returns a random element from this range using the specified source of randomness, or null if this range is empty.
fun ULongRange.randomOrNull(random: Random): ULong?rangeTo
Creates a range from this Comparable value to the specified that value.
operator fun <T : Comparable<T>> T.rangeTo(
that: T
): ClosedRange<T>Creates a range from this Double value to the specified that value.
operator fun Double.rangeTo(
that: Double
): ClosedFloatingPointRange<Double>Creates a range from this Float value to the specified that value.
operator fun Float.rangeTo(
that: Float
): ClosedFloatingPointRange<Float>reversed
Returns a progression that goes over the same range in the opposite direction with the same step.
fun IntProgression.reversed(): IntProgressionfun LongProgression.reversed(): LongProgressionfun CharProgression.reversed(): CharProgressionfun UIntProgression.reversed(): UIntProgressionfun ULongProgression.reversed(): ULongProgressionstep
Returns a progression that goes over the same range with the given step.
infix fun IntProgression.step(step: Int): IntProgressioninfix fun LongProgression.step(step: Long): LongProgressioninfix fun CharProgression.step(step: Int): CharProgressioninfix fun UIntProgression.step(step: Int): UIntProgressioninfix fun ULongProgression.step(step: Long): ULongProgressionuntil
Returns a range from this value up to but excluding the specified to value.
infix fun Int.until(to: Byte): IntRangeinfix fun Long.until(to: Byte): LongRangeinfix fun Byte.until(to: Byte): IntRangeinfix fun Short.until(to: Byte): IntRangeinfix fun Char.until(to: Char): CharRangeinfix fun Int.until(to: Int): IntRangeinfix fun Long.until(to: Int): LongRangeinfix fun Byte.until(to: Int): IntRangeinfix fun Short.until(to: Int): IntRangeinfix fun Int.until(to: Long): LongRangeinfix fun Long.until(to: Long): LongRangeinfix fun Byte.until(to: Long): LongRangeinfix fun Short.until(to: Long): LongRangeinfix fun Int.until(to: Short): IntRangeinfix fun Long.until(to: Short): LongRangeinfix fun Byte.until(to: Short): IntRangeinfix fun Short.until(to: Short): IntRangeinfix fun ULong.until(to: ULong): ULongRange