lib: adds Discrete::times as a shortcut to Range
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 10 Nov 2013 15:16:05 +0000 (10:16 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sun, 10 Nov 2013 15:16:05 +0000 (10:16 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/collection/range.nit
tests/sav/test_for_times.sav [new file with mode: 0644]
tests/test_for_times.nit [new file with mode: 0644]

index f356c6f..1b4d5bb 100644 (file)
@@ -100,3 +100,11 @@ class IteratorRange[E: Discrete]
                _item = r.first
        end
 end
+
+redef class Discrete
+       # Returns the range from 0 to `self-1`, is used to do:
+       #
+       #    for i in 3.times do print "Cool"
+       #    for i in 100.times do print "{i}/100"
+       fun times: Range[OTHER] do return new Range[OTHER](0, self-1)
+end
diff --git a/tests/sav/test_for_times.sav b/tests/sav/test_for_times.sav
new file mode 100644 (file)
index 0000000..75c6ff0
--- /dev/null
@@ -0,0 +1,13 @@
+Cool
+Cool
+Cool
+0
+1
+2
+3
+4
+5
+6
+7
+8
+9
diff --git a/tests/test_for_times.nit b/tests/test_for_times.nit
new file mode 100644 (file)
index 0000000..81c504c
--- /dev/null
@@ -0,0 +1,4 @@
+module test_for_times
+
+for i in 3.times do print "Cool"
+for i in 10.times do print i