chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.79k stars 420 forks source link

Index-neutral programming: indices #15024

Closed bradcray closed 4 years ago

bradcray commented 4 years ago

[edited to remove .lowIdx and .highIdx from the proposal for simplicity. In the comments, it was argued that such queries can be made from .indices when applicable and that this keeps things simpler / more orthogonal; we can always reconsider later if compelling arguments for .lowIdx and .highIdx come up].

As a Chapel programmer, I'd like to see most standard collection types support the method~s~ .indices ~, .lowIdx, and .highIdx~ in order to support programming that's neutral w.r.t. whether 0- vs. 1-based vs. something-else-indexing is used by the type. For example, these would permit me to write

for i in data.indices do
  ...data(i)...  // or ...data[i]...

for many collection types.

As a starting point, it would be nice to support these for tuples, strings, bytes, lists, and arrays.

[edit: and enums?]

~Interestingly, I think the implementations and possible uses of these are likely to vary a bit between types.~ I would expect tuples, strings, bytes, and lists (edit: and enums) can return a range for indices whereas ~rectangular~ arrays would likely return their domains ~and associative arrays would implement indices as an iterator~.

~Similarly, lowIdx can be param for tuples, strings, bytes, and lists, but not arrays; and highIdx can be param only for tuples.~