The class of Range
shows a range between begin and end. The object is created by new Range
as below.
new Range(begin, end, excludingEnd)
The instance of class Range
is also created by the dot style syntax as below.
There are 2 styles of ..
and ...
.
The style of ..
means to include the end, but the style of ...
means to exclude the end.
var a = 2..10; // new Range(2, 10)
var b = 2...10; // new Range(2, 10, true)
The class Range
have following methods.
Name | Outline |
---|---|
begin() |
The value of the beginning point. |
end() |
The value of the end point. If you put nothing, returns null . This do not care about the excludeEnd flag. |
next() |
Returns the next value. If it is called a first time, the beginning value will be returned. |
isEndExcluded() |
true if the Range excludes the end. |
each([func]) |
Iterates the range with callback to func . |
Note that in each(func)
with no end point, it will be an infinite loop.
Range
object can be used as an index for Array, String, Binary, or Range like this.
a[2..3] // The variable `a` can be Array, String, Binary, or Range.
// This means to return a set of elements from 2nd element to 3rd element.
The object used at the begin and end point of the class Range
must have followings.
- The operator
<=>
.- It is used when comparing with the begin and end point.
- The method named as
next()
.- It is used when iterating the object.
The style of omitting the end point like n..
is available.
But currently you can NOT omit the beginning value because it causes a conflict with the spread/rest operator of ...
.
If you need this, you can use the workaround with null
as below.
a = null..10; // no beginning point of Range.
System.println((2..10).toArray());
[2, 3, 4, 5, 6, 7, 8, 9, 10]
You can use a variable or expression.
function makeRange(begin, len) {
return begin..(begin+len);
}
System.println(makeRange(100, 2).end()); // => 102
102
var a = "a".."z"; // new Range("a", "z")
var b = "ab"..."ax"; // new Range("ab", "ax", true)
System.println(a.toArray());
System.println(b.toArray());
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
["ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw"]
using DateTime;
for (var dt in DateTime(2000, 1, 1)..DateTime(2000, 1, 10)) {
System.println(dt);
}
2000/01/01 00:00:00
2000/01/02 00:00:00
2000/01/03 00:00:00
2000/01/04 00:00:00
2000/01/05 00:00:00
2000/01/06 00:00:00
2000/01/07 00:00:00
2000/01/08 00:00:00
2000/01/09 00:00:00
2000/01/10 00:00:00
for (var i = 0; i <= 10; ++i) {
switch (i) {
case 1..4:
System.println("okay 1 (%{i})");
break;
case 7...9:
System.println("okay 2 (%{i})");
break;
default:
System.println("out of range (%{i})");
break;
}
}
out of range (0)
okay 1 (1)
okay 1 (2)
okay 1 (3)
okay 1 (4)
out of range (5)
out of range (6)
okay 2 (7)
okay 2 (8)
out of range (9)
out of range (10)
for (var i in 'o'..'af') {
switch (i) {
case 'ac'..'ae':
System.println("okay 1 (%{i})");
break;
case 'p'...'w':
System.println("okay 2 (%{i})");
break;
default:
System.println("out of range (%{i})");
break;
}
}
out of range (o)
okay 2 (p)
okay 2 (q)
okay 2 (r)
okay 2 (s)
okay 2 (t)
okay 2 (u)
okay 2 (v)
out of range (w)
out of range (x)
out of range (y)
out of range (z)
out of range (aa)
out of range (ab)
okay 1 (ac)
okay 1 (ad)
okay 1 (ae)
out of range (af)
Range for String means to return a part of string between the start and the end of Range
.
It is like String#subString()
but note that String#subString()
requires a length.
var str = "abcdefghijklmnopqrstuvwxyz";
System.println(str[2..25]); // "cdefghijklmnopqrstuvwxyz"
System.println(str[2...25]); // "cdefghijklmnopqrstuvwxy"
System.println(str.subString(2, 23)); // "cdefghijklmnopqrstuvwxy"
cdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxy
cdefghijklmnopqrstuvwxy
Range for Array means to return a part of array between the start and the end of Range
.
It is like Array#subArray()
but note that Array#subArray()
requires a length.
var ary = 16.times();
System.println(ary[2..12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
System.println(ary[2...12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
System.println(ary.subArray(2, 10)); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Range for Binary means to return a part of binary between the start and the end of Range
.
It is like Binary#subBinary()
but note that Binary#subBinary()
requires a length.
var bin = <0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f>;
System.println(bin[2..12]); // <0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c>
System.println(bin[2...12]); // <0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b>
System.println(bin.subBinary(2, 10)); // <0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b>
<0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c>
<0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b>
<0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b>
Range for Range means to return parts between the start and the end by Range
at the index.
var range = 0..16;
System.println(range[2..12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
System.println(range[2...12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Range for Range means to return parts between the start and the end by Range
at the index.
var range = 0..;
System.println(range[2..12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
System.println(range[2...12]); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Range for Range means to return parts between the start and the end by Range
at the index.
var range = 1..;
System.println(range[2]); // 3
System.println(range[22]); // 23
System.println(range[1050]); // 1051
3
23
1051
Range for Range means to return parts between the start and the end by Range
at the index.
System.println((1..10)[2..]); // 3..10
System.println((1...10)[2..]); // 3...10
System.println((100..)[2..]); // 102..
Range(3, 10, false)
Range(3, 10, true)
Range(102, null, false)
Range for Range means to return parts between the start and the end by Range
at the index.
var range = (1...10)[2..];
for (e in range) {
System.println(e);
}
3
4
5
6
7
8
9