Created
January 24, 2018 08:14
-
-
Save Araq/701565f110abd65801fbaea03143c24d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
HalfStepUp[T] = object | |
upper, step: T | |
StepUp[T] = object | |
lower, upper, step: T | |
HalfStepDown[T] = object | |
upper, step: T | |
StepDown[T] = object | |
lower, upper, step: T | |
proc `|+`[T](upper, step: T): HalfStepUp[T] = | |
HalfStepUp[T](upper: upper, step: step) | |
proc `..`[T](lower: T; s: HalfStepUp[T]): StepUp[T] = | |
StepUp[T](lower: lower, upper: s.upper, step: s.step) | |
proc `|-`[T](upper, step: T): HalfStepDown[T] = | |
HalfStepDown[T](upper: upper, step: step) | |
proc `..`[T](lower: T; s: HalfStepDown[T]): StepDown[T] = | |
StepDown[T](lower: lower, upper: s.upper, step: s.step) | |
iterator items*[T](x: StepUp[T]): T = | |
var it = x.lower | |
while it <= x.upper: | |
yield it | |
inc it, x.step | |
iterator items*[T](x: StepDown[T]): T = | |
var it = x.lower | |
while it >= x.upper: | |
yield it | |
dec it, x.step | |
for i in 5 .. 15 |+ 3: | |
echo i | |
echo "--------------" | |
for i in 15 .. 5 |- 3: | |
echo i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment