From 720c0605313cba897677ab60dbb9d0aa2e333c68 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 28 Feb 2017 19:22:44 +0300 Subject: [PATCH] Add step() infix functions. --- .../src/main/kotlin/kotlin/ranges/Ranges.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt b/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt index ddd16121b9a..2e88dd173fa 100644 --- a/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt +++ b/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt @@ -326,12 +326,40 @@ public fun Long.coerceIn(range: ClosedRange): Long { } +internal fun checkStepIsPositive(isPositive: Boolean, step: Number) { + if (!isPositive) throw IllegalArgumentException("Step must be positive, was: $step.") +} + // This part is from generated _Ranges.kt. /** * Returns a progression that goes over the same range in the opposite direction with the same step. */ public fun IntProgression.reversed() = IntProgression.fromClosedRange(last, first, -step) +/** + * Returns a progression that goes over the same range with the given step. + */ +public infix fun IntProgression.step(step: Int): IntProgression { + checkStepIsPositive(step > 0, step) + return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step. + */ +public infix fun LongProgression.step(step: Long): LongProgression { + checkStepIsPositive(step > 0, step) + return LongProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} + +/** + * Returns a progression that goes over the same range with the given step. + */ +public infix fun CharProgression.step(step: Int): CharProgression { + checkStepIsPositive(step > 0, step) + return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step) +} + /** * Returns a progression from this value down to the specified [to] value with the step -1. *