indexing operator for multidimensional lists
See original GitHub issueCeylon doesn’t have true multidimensional lists, but it is possible to work around that using a list of lists as in Java. This works out just perfectly for multidimensional tuples:
value tup2d = [[1, 2], [2, 3], [0,1]];
Integer elem = tup2d[1][0];
However, for other multidimensional lists (including multidimensional arrays) the stacked indexing operator doesn’t work:
value tup2d = [ for (i in 0..n-1) [ for (j in 0..n-1) i==j then 1 else 0 ]];
Integer elem = tup2d[1][0]; //compile error!!
I can see several possible solutions to this:
- Make the
x[i]
operator accept aCorrespondence?
as its LHS instead of onlyCorrespondence
as it does today. - Introduce a multidimensional lookup operator of form
x[i,j]
for things of typeCorrespondence<Correspondence<...>>
, wherex[i,j]
means whatx[i][j]
would mean if it were well-typed. - Figure out a way to tag an
Array
with its dimensions, for example,Array.ofDimensions(2, 3)(0.0)
and introduce a multidimensional lookup operator of formx[i,j]
wherex[i,j]
meansx[j+3*i]
.
Option 1 is of course the simplest change.
Options 1 and 2 work for any kind of Correspondence
and support the current pattern of using lists of lists (of lists, etc).
Option 3 recognizes that lists of lists probably aren’t really a very efficient way to lay out multidimensional arrays of numbers, but only really works for Array
s. Here, a “2D” array of Float
s would be represented as an Array<Float>
, but its elements could still be accessed as if they were laid out in two dimensions using arr[i, j]
.
Thoughts?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:10 (5 by maintainers)
Top GitHub Comments
There are two cases. Either the model is a list of lists, or the model is a rectangle (or hyperrectangle). The programmer knows which. As to gifting syntax to the first case, I am reminded of that line from Ceylon’s motivational preamble, what was it? “Rather clarity than a sea of argot ASCII” ? So I want to advocate for what Ceylon already has in this example (i.e. the type rules on the
x[i]
operator).The want for “clear, concise” syntax, for indexing into a list-of-lists that you know might be an OoB, trades off with the mission to be explicit about those possible
Null
s. This is where I like Ceylon for making some things “difficult”. I would say “conscious”. I also think this is where the solutions are for the application to provide. Again, this list of lists is not rectangular; the application’s purpose would decide how much of a fuss should be made over its handling of an OoB, not to mention the semantics of doing so. So let them work through Ceylon’s highly regular, strongly typed, rich system to obtain their objective - and they’ll get a Ceylon source file for it.In the hyperrectangle case, I’m imagining a language module artifact that would have the conveniences, maybe even optimization. It’s different.
I hope I haven’t made myself look too foolish.
If the
[]
operator worked as requested in #4517, one could make a multidimensional-array class whose[]
operator always returned a Correspondence, just one that at the final dimension would always returnnull
if any earlier dimension was out-of-bounds. (Though that wouldn’t help with the comprehension-based example above.)