diff --git a/libraries/stdlib/common/src/generated/_Ranges.kt b/libraries/stdlib/common/src/generated/_Ranges.kt index b7e7eedc369..ef3802aa0ee 100644 --- a/libraries/stdlib/common/src/generated/_Ranges.kt +++ b/libraries/stdlib/common/src/generated/_Ranges.kt @@ -961,6 +961,8 @@ public fun CharProgression.reversed(): CharProgression { /** * Returns a progression that goes over the same range with the given step. + * + * @sample samples.ranges.Ranges.stepInt */ public infix fun IntProgression.step(step: Int): IntProgression { checkStepIsPositive(step > 0, step) @@ -969,6 +971,8 @@ public infix fun IntProgression.step(step: Int): IntProgression { /** * Returns a progression that goes over the same range with the given step. + * + * @sample samples.ranges.Ranges.stepLong */ public infix fun LongProgression.step(step: Long): LongProgression { checkStepIsPositive(step > 0, step) @@ -977,6 +981,8 @@ public infix fun LongProgression.step(step: Long): LongProgression { /** * Returns a progression that goes over the same range with the given step. + * + * @sample samples.ranges.Ranges.stepChar */ public infix fun CharProgression.step(step: Int): CharProgression { checkStepIsPositive(step > 0, step) diff --git a/libraries/stdlib/common/src/generated/_URanges.kt b/libraries/stdlib/common/src/generated/_URanges.kt index be77271e835..2462ae78381 100644 --- a/libraries/stdlib/common/src/generated/_URanges.kt +++ b/libraries/stdlib/common/src/generated/_URanges.kt @@ -345,6 +345,8 @@ public fun ULongProgression.reversed(): ULongProgression { /** * Returns a progression that goes over the same range with the given step. + * + * @sample samples.ranges.Ranges.stepUInt */ @SinceKotlin("1.5") @WasExperimental(ExperimentalUnsignedTypes::class) @@ -355,6 +357,8 @@ public infix fun UIntProgression.step(step: Int): UIntProgression { /** * Returns a progression that goes over the same range with the given step. + * + * @sample samples.ranges.Ranges.stepULong */ @SinceKotlin("1.5") @WasExperimental(ExperimentalUnsignedTypes::class) diff --git a/libraries/stdlib/samples/test/samples/ranges/ranges.kt b/libraries/stdlib/samples/test/samples/ranges/ranges.kt index d382da48a63..b6e01a91395 100644 --- a/libraries/stdlib/samples/test/samples/ranges/ranges.kt +++ b/libraries/stdlib/samples/test/samples/ranges/ranges.kt @@ -52,4 +52,44 @@ class Ranges { assertTrue(3.14f in range) assertFalse(100.1f in range) } + + @Sample + fun stepInt() { + val ascendingProgression = 1..6 step 2 + val descendingProgression = 6 downTo 1 step 2 + assertPrints(ascendingProgression.toList(), "[1, 3, 5]") + assertPrints(descendingProgression.toList(), "[6, 4, 2]") + } + + @Sample + fun stepUInt() { + val ascendingProgression = 1u..6u step 2 + val descendingProgression = 6u downTo 1u step 2 + assertPrints(ascendingProgression.toList(), "[1, 3, 5]") + assertPrints(descendingProgression.toList(), "[6, 4, 2]") + } + + @Sample + fun stepLong() { + val ascendingProgression = 1L..6L step 2L + val descendingProgression = 6L downTo 1L step 2L + assertPrints(ascendingProgression.toList(), "[1, 3, 5]") + assertPrints(descendingProgression.toList(), "[6, 4, 2]") + } + + @Sample + fun stepULong() { + val ascendingProgression = 1UL..6UL step 2L + val descendingProgression = 6UL downTo 1UL step 2L + assertPrints(ascendingProgression.toList(), "[1, 3, 5]") + assertPrints(descendingProgression.toList(), "[6, 4, 2]") + } + + @Sample + fun stepChar() { + val ascendingProgression = 'a'..'f' step 2 + val descendingProgression = 'f' downTo 'a' step 2 + assertPrints(ascendingProgression.toList(), "[a, c, e]") + assertPrints(descendingProgression.toList(), "[f, d, b]") + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt index c611aaa2797..435774ecb45 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt @@ -68,6 +68,7 @@ object RangeOps : TemplateGroupBase() { } builder { infix(true) doc { "Returns a progression that goes over the same range with the given step." } + sample("samples.ranges.Ranges.step$primitive") signature("step(step: ${primitive!!.stepType})", notForSorting = true) returns("TProgression") body {