commonMain.extensions.ClosedRange.kt Maven / Gradle / Ivy
package io.fluidsonic.stdlib
import kotlin.jvm.*
operator fun > ClosedRange.component1() =
start
operator fun > ClosedRange.component2() =
endInclusive
fun > ClosedRange.flipped() =
endInclusive .. start
fun , R : Comparable> ClosedRange.mapBounds(transform: (Bound) -> R) =
transform(start) .. transform(endInclusive)
@JvmName("mapBoundsToDouble")
fun > ClosedRange.mapBounds(transform: (Bound) -> Double) =
transform(start) .. transform(endInclusive)
@JvmName("mapBoundsToFloat")
fun > ClosedRange.mapBounds(transform: (Bound) -> Float) =
transform(start) .. transform(endInclusive)
fun > ClosedRange.mapBounds(transform: (Bound) -> Int) =
transform(start) .. transform(endInclusive)
fun > ClosedRange.mapBounds(transform: (Bound) -> Long) =
transform(start) .. transform(endInclusive)
@ExperimentalUnsignedTypes
fun > ClosedRange.mapBounds(transform: (Bound) -> UInt) =
transform(start) .. transform(endInclusive)
@ExperimentalUnsignedTypes
fun > ClosedRange.mapBounds(transform: (Bound) -> ULong) =
transform(start) .. transform(endInclusive)
fun > ClosedRange.intersection(other: ClosedRange) =
overlaps(other).thenTake { maxOf(start, other.start) rangeToExcluding minOf(endInclusive, other.endInclusive) }
fun > ClosedRange.overlaps(other: ClosedRange) =
contains(other.start) || other.contains(start)
fun > ClosedRange.toSequence(next: (Bound) -> Bound?) =
when {
isEmpty() -> emptySequence()
else -> generateSequence(start) { start ->
next(start)?.takeIf { value -> contains(value) }
}
}