question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

indexing operator for multidimensional lists

See original GitHub issue

Ceylon 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:

  1. Make the x[i] operator accept a Correspondence? as its LHS instead of only Correspondence as it does today.
  2. Introduce a multidimensional lookup operator of form x[i,j] for things of type Correspondence<Correspondence<...>>, where x[i,j] means what x[i][j] would mean if it were well-typed.
  3. 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 form x[i,j] where x[i,j] means x[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 Arrays. Here, a “2D” array of Floats 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Mandritticommented, Mar 31, 2018

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 Nulls. 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.

1reaction
kingjon3377commented, Mar 30, 2018

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 return null if any earlier dimension was out-of-bounds. (Though that wouldn’t help with the comprehension-based example above.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-dimensional lists in Python - GeeksforGeeks
Multi-dimensional lists in Python · 1. append(): Adds an element at the end of the list. Example: # Adding a sublist · 2....
Read more >
C++ multidimensional array operator - Stack Overflow
You can have operator[] return an array of a smaller dimension (For a 3D array, it will return a 2D array, for a...
Read more >
Multidimensional subscript operator - open-std.org
C++ preferentially uses operator[] for one-dimensional array access. Standard C++ provides several different single-dimensional array types: ...
Read more >
Multi-dimensional array basics - Aptech
Indexing multiple rows and/or columns with the colon operator ... After working through the examples above and doing some experimental indexing of your...
Read more >
Indexing on ndarrays — NumPy v1.25.dev0 Manual
ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection. There are different kinds...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found