Case Range Statement ..

bearophile bearophileHUGS at lycos.com
Thu Jul 9 15:37:51 PDT 2009


Walter Bright:
> Having such a third operand, however, should mesh quite well with 
> Andrei's range library construct.

In normal usage slices are sometimes useful, but slicing is mostly useful when you work with rectangular matrices, so you can slice and dice them in many ways, across axes, etc. NumPy and Matlab show such usages well. Such operations can probably be done well enough by a refined D matrix library designed for "heavy" numerical processing.

An option is to not change the current design of slices, keeping them simple, with no stride. I can accept this.
(One thing I don't like much of it is that "out of bound" slices are an hazard, you can't slice past the real size of an array. Time ago I have shown here that Python is safer in such regard, and performs something similar to: slice(a,b) === slice(max(0, min(a, $)), max(0, min(b, $))). This is quite handy (and later I have asked to Python designers, it's not a wart, it's the way they have wanted it), but it's also slower at run-time, so the current less handy behavior of D slices may be acceptable).

It's also possible to replace the current a..b syntax with something like a..b:c or a:b:c or a..b..c, etc. This is a bit more elastic than the current semantics, but not much. Even if I have asked for it in the past, I don't feel so much need for it. There are other more important features I now like to have.

If D wants to appeal to programmers that have to write numerical computations (and I think this may become one of the purposes of the D language, this means to replace some of the programs today done with Fortran and Matlab), then it may add a more general scaffold, that probably later will be used by a numeric processing library.
The Chapel language is designed for such numerical computations, among other things it has Domains, they are "index spaces". You can define arithmetic Domains, that are just one or more arithmetic ranges, open and closed, with stride (so they define an n dimensional arithmetic range, for example [0..10, 0..10] defines the space of the indexes of a 2D matrix 10x10). It allows to slice domains, to create sub-domains, un-ordered domains, associative domains and more. Then you can use them to iterate on, to initialize arrays, matrices or associative arrays defined on such Domain of indexes. It may sound complex, but I've seen it's easy to use, you can write Chapel code that uses such things only after reading about them for 20-40 minutes. Such abstraction of the idea of index space gives safety (most loops become foreach, no many out of bound situations anymore, this also means more performance because the compiler can avoid many run-time array bound checks), possibilities for the compiler to optimize and/or parallelize their usage, etc.

Bye,
bearophile



More information about the Digitalmars-d mailing list