From bd00ca315cb1461c10319c403a88d929d30f8c78 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sun, 10 Nov 2013 10:16:05 -0500 Subject: [PATCH] lib: adds Discrete::times as a shortcut to Range MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/standard/collection/range.nit | 8 ++++++++ tests/sav/test_for_times.sav | 13 +++++++++++++ tests/test_for_times.nit | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 tests/sav/test_for_times.sav create mode 100644 tests/test_for_times.nit diff --git a/lib/standard/collection/range.nit b/lib/standard/collection/range.nit index f356c6f..1b4d5bb 100644 --- a/lib/standard/collection/range.nit +++ b/lib/standard/collection/range.nit @@ -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 index 0000000..75c6ff0 --- /dev/null +++ b/tests/sav/test_for_times.sav @@ -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 index 0000000..81c504c --- /dev/null +++ b/tests/test_for_times.nit @@ -0,0 +1,4 @@ +module test_for_times + +for i in 3.times do print "Cool" +for i in 10.times do print i -- 1.7.9.5