Moved *Range.step() functions to stdlib.

This commit is contained in:
Evgeny Gerashchenko
2013-01-23 20:05:35 +04:00
parent 9b866b2e12
commit 0ff3589bb9
13 changed files with 85 additions and 74 deletions
+77 -1
View File
@@ -23,7 +23,7 @@ public fun FloatSequence.reversed(): FloatSequence {
public fun LongSequence.reversed(): LongSequence {
return LongSequence(end, start, -increment)
}
public fun DoubleSequence.reversed(): DoubleSequence {
return DoubleSequence(end, start, -increment)
}
@@ -57,3 +57,79 @@ public fun DoubleRange.reversed(): DoubleSequence {
return DoubleSequence(end, start, -1.0)
}
public fun IntSequence.step(step: Int): IntSequence {
checkStepIsPositive(step > 0, step)
return IntSequence(start, end, if (increment > 0) step else -step)
}
public fun CharacterSequence.step(step: Int): CharacterSequence {
checkStepIsPositive(step > 0, step)
return CharacterSequence(start, end, if (increment > 0) step else -step)
}
public fun ByteSequence.step(step: Int): ByteSequence {
checkStepIsPositive(step > 0, step)
return ByteSequence(start, end, if (increment > 0) step else -step)
}
public fun ShortSequence.step(step: Int): ShortSequence {
checkStepIsPositive(step > 0, step)
return ShortSequence(start, end, if (increment > 0) step else -step)
}
public fun LongSequence.step(step: Long): LongSequence {
checkStepIsPositive(step > 0, step)
return LongSequence(start, end, if (increment > 0) step else -step)
}
public fun FloatSequence.step(step: Float): FloatSequence {
checkStepIsPositive(step > 0, step)
return FloatSequence(start, end, if (increment > 0) step else -step)
}
public fun DoubleSequence.step(step: Double): DoubleSequence {
checkStepIsPositive(step > 0, step)
return DoubleSequence(start, end, if (increment > 0) step else -step)
}
public fun IntRange.step(step: Int): IntSequence {
checkStepIsPositive(step > 0, step)
return IntSequence(start, end, step)
}
public fun CharRange.step(step: Int): CharacterSequence {
checkStepIsPositive(step > 0, step)
return CharacterSequence(start, end, step)
}
public fun ByteRange.step(step: Int): ByteSequence {
checkStepIsPositive(step > 0, step)
return ByteSequence(start, end, step)
}
public fun ShortRange.step(step: Int): ShortSequence {
checkStepIsPositive(step > 0, step)
return ShortSequence(start, end, step)
}
public fun LongRange.step(step: Long): LongSequence {
checkStepIsPositive(step > 0, step)
return LongSequence(start, end, step)
}
public fun FloatRange.step(step: Float): FloatSequence {
checkStepIsPositive(step > 0, step)
return FloatSequence(start, end, step)
}
public fun DoubleRange.step(step: Double): DoubleSequence {
checkStepIsPositive(step > 0, step)
return DoubleSequence(start, end, step)
}
private fun checkStepIsPositive(isPositive: Boolean, step: Number) {
if (!isPositive) throw IllegalArgumentException("Step must be positive, was: $step")
}