Stupid little iota of an idea

bearophile bearophileHUGS at lycos.com
Sat Feb 12 14:51:18 PST 2011


Peter Alexander:

> As Michel pointed out, a..b:c is ambiguous.
> 
> auto foo = [ 1..10 : 2, 2..20 : 3 ];
> 
> Is foo an AA of ranges to ints, or an array of stepped ranges?

Lazy strided intervals as associative array keys is not a common need. But there are few other situations:
auto r = pred ? 1..10 : 2 : 2..20 : 3;

Where the range syntax is not usable you may use the function from Phobos as fall-back:
auto r = pred ? iota(1,10,2) : iota(2,20,3);

A possible alternative is to require parentheses where the syntax is ambiguous:
auto foo = [(1..10 : 2), (2..20 : 3)];
auto r = pred ? (1..10 : 2) : (2..20 : 3);

Another alternative is to use .. to separate the stride too:
auto foo = [1..10..2, 2..20..3];
auto r = pred ? 1..10..2 : 2..20..3;

In this post I have assumed that the interval syntax is usable on integral values only. For floating point ones you use iota again:
iota(1.0, 10.0, 0.5)

Bye,
bearophile


More information about the Digitalmars-d mailing list