Table of Contents
Concurnas has native support for numerical ranges. For example:
range = 0 to 10 //integer range from 1 to 10 inclusive
Above, range is now of type
IntSequence. In Concurnas, sequences implement the
java.util.Iterable interface, meaning that they are able to be used within for loops and anywhere else where an iterator is appropriate. Let's extract the values of the above range:
result = x for x in range //result == [0, 1, 2, 34, 5,6 7, 8, 9, 10] //note that the range is inclusive of the start and finishing items specified
The above example denotes a integer sequence. A long sequence is created when either of the range bounds specified are of type long:
range LongSequence = 0L to 10
Sequences can be created with specific increments via the step method:
stepped = 0 to 10 step 2 result = x for x in stepped //result == [0, 2, 4, 6, 8, 10]
So far we have only explored ascending sequences, we can create descending sequences by inverting the boundary arguments:
descending = 10 to 0 step 2 result = x for x in descending //result == [10, 8, 6, 4, 2, 0]
As an alternative to decrementing sequences, a reversed sequence can be created as follows:
norm = 0 to 4 rev = norm reversed //norm == [0, 1, 2, 3, 4] //rev == [4, 3, 2, 1, 0]
Infinite sequences can be created simply by omitting a to argument:
infi = 0 to //infi => 0, 1, 2, 3, ...
And they can be stepped as follows:
infi = 0 to step 10 //infi == 0, 10, 20, 30,...
Note that adding a step also enables us to create infinitely decreasing sequences:
infi = 0 to step -1 //infi == 0, -1, -2, -3,...
Infinite sequences cannot be reversed.
Sequences have direct support for the in operator (without requiring calculation of the entire contents of the range). For example:
range = 0 to 5 cont1 = 4 in range //cont1 resolves to true as 4 is in the range cont2 = 88 not in range//con2 resolves to true as 88 is not in the range
Char, double, float sequences?
Concurnas doesn't have direct support for non
int/long sequences, however the effects can be easily achieved. For example, a char sequence:
chars = x as char for x in 65 to 70 //chars == [A, B, C, D, E, F]
Under the hood?
Ranges are implemented via a clever use of both extension functions,expression lists, operator overloading and auto importing of the relevant extension functions and sequence classes. See:
com/concurnas/lang/ranges.conc for more details.