Don:
>How do you specify a uint range that includes uint.max?<
The interval I was talking about may generate integers only (it may be possible
to generate longs and chars too if you give it long/char extrema, but I am not
sure this is a good idea).
>This is a perfect example of what Andrei recently posted: proposal of a
>language change for a pathetically limited special case. Your stride is not
>powerful enough to be worthwhile.<
It's limited on purpose, but such intervals are useful in many situations.
And note such interval is less limited than the current syntax of D2, that
allows such interval as foreach argument only.
This post of mine was just an idea, surely most of it can be improved :-)
---------------------
Daniel Keep:
>[a,b] is inclusive, (a,b) is exclusive and [a..b] is lower inclusive, upper
>exclusive (to match slicing syntax).<
I'll probably not be able to remember that. What I'm looking for is something
simple that covers the most common cases, not all of them introducing excessive
burden to the memory. In designing such things the 80/20 rule is good.
---------------------
Don:
> So desirable syntax like auto y = x[2..$, 4, 3..8]; is impossible.<
I see.
In Haskell you can use the syntax [x..] to denote the half interval of integers
from x to infinite.
So in D2 x..$ may be used to denote an interval infinte on the right... or
(contextual-wise) one interval extended to the max possible value (max int, or
max length of the iterable/data structure?). I am not sure such double meaning
is a good thing.
---------------------
Lutger:
>About the stride, in ruby this is done as (0..10).step(2), which is very
>readable imho.<
It's more readable than
0 .. 10 .. 2
but I think you can get used to 0..10..2 too, because you probably end using
such syntax often in programs.
---------------------
Sean Reque:
>but Ruby also supports instantiating them using perl's syntax, which
>underneath the hood is just creating a Regexp object. The advantage of the
>perl-like syntax is that editors and IDEs can aid a user with syntax
>highlighting, whereas when creating a regular expression with a string no such
>help can be given.<
Languages are designed for different purposes and with different ideas in mind.
Syntax-wise Python is simpler, less tricky (*) and more uniform than Ruby. This
makes Python less complex to learn, but less able to create custom-purpose
sub-languages, etc.
(* = see functions called as foo 5)
>Doing things similarly in D, one could implement intervals in a library form
>in a similar manner as Daniel did, and then add support in the language for
>the syntax bearophile described.<
Few weeks ago I have suggested to adopt a similar strategy in D too, for
example to implement complex numbers, multi-precision numbers, and few other
things. The compiler can implement just the syntax and a default "bridge" from
such syntax and the logic that is contained into a normal D module of the std
lib. So for example you can write:
Bigint a = 123_456_789_123_456_789_123_456_789;
Bigint b = 987_654_321_987_654_321_987_654_321;
c = a * b;
Where the compiler has a built-in support such natural syntax for Bigints
literals (or complex number literals), but the operations like the
multiplication among such Bigints are implemented in D into the std.bigints
module and not into the compiler. I don't actually know if such idea can be
implemented in a D compiler, no one has answered me about this so far :-)
---------------------
A possible small problem:
Floating-point linear ranges (as used in matlab and numPy, etc) are often
useful, but the 0..10 and 0..10..2 syntax doesn't look much good if you want to
re-use it for floats (or even for interval arithmetic) because all those points
may be too much confusing, even if you add spaces:
0.0 .. 10.5 .. 0.5
0. .. 10. .. .5
So intervals may be kept for integral values only.
As an alternative syntax may be:
foreach (i; 1:10) {...}
foreach (i; 10 : 1 : -1) {...}
foreach (i; 0.0 : 10.5 : 0.5) {...}
Bye,
bearophile