First class lazy Interval

Don nospam at nospam.com
Fri Feb 27 06:37:26 PST 2009


Denis Koroskin wrote:
> On Fri, 27 Feb 2009 16:44:31 +0300, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Michel Fortin wrote:
>>> On 2009-02-27 04:43:46 -0500, bearophile <bearophileHUGS at lycos.com> 
>>> said:
>>>
>>>> D2 supports the interval syntax in the foreach:
>>>> foreach (i; 1..1000) {...}
>>>>
>>>> Such intervals are useful in a very large number of situations. So, 
>>>> with the new Range support, it may be useful to allow the interval 
>>>> syntax to be used in other contexts as well.
>>>> So x..y may become a first-class lazy interval from x to y-1, that 
>>>> can be passed to functions too, etc, and not just used into foreach 
>>>> (the compiler can recognize it, and often optimize it away in many 
>>>> situations, replacing it with a normal for() loop).
>>>  I agree that having first-class intervals in the language would make 
>>> it better, especially when you want to pass intervals as function 
>>> arguments.
>>
>> I'm having trouble understanding what's wrong with the good old data 
>> types and functions.
>>
>> Andrei
> 
> The syntax. One may want to reuse 0..100 syntax to generate random number:
> auto x = random(0..100); // gimme a random value in [0, 100)
> 
> or check if a value belongs to an interval:
> 
> T opIndex(size_t index)
> {
>    assert(index in 0.._size);
>    // ...
> }

Particularly, multi-dimensional slices. We have opSlice as a hacky 
special case of opIndex. So desirable syntax like

auto y = x[2..$, 4, 3..8];

is impossible. You can only do something like:

auto y = x[Range(2,$), 4, Range(3,8)];
or perhaps
auto y = x[Range[2..$], 4, Range[3..8]];



More information about the Digitalmars-d mailing list