4 ms·
That's not so obvious, the abstractions for multi-dimensional indexing are better than (I think) you're imagining. Here's a nice recent example: a new reverse!
by improbable22 6y ago
That's not so obvious, the abstractions for multi-dimensional indexing are better than (I think) you're imagining.
Here's a nice recent example: a new reverse!(A; dims) function, which is fast and works on arrays with any number of dimensions. And is completely indifferent to where the indices start:
https://github.com/JuliaLang/julia/pull/37367/files#diff-eb90365c420e16fa79cd86366d0e85c42474047528efe605b7168d22d0fe6fdfR105 https://github.com/JuliaLang/julia/pull/37367/files#diff-eb9...
- ZephyrBlu 6y agoI'm not 100% sure what I'm looking at, but that seems to be related to reversing arrays/matrices, not accessing arrays through indexes.
- improbable22 6y agoMaybe less readable than I remembered, sorry! But these `CartesianIndex` things are how you index multi-dimensional arrays. If by indexing you only mean adding offsets to pointers with your bare hands, this is possible but discouraged. Because you'll produce a buggier and less general version of exactly what already exists. In this case, reversing an N-dimensional array, along any M of its axes, with arbitrary offsets -- there are a lot of ways to get that wrong.
- ZephyrBlu 6y agoWhat I mean is something like this: # one dimensional a = [1, 2, 3] # prints the number 1 @ index 0 print(a[0]) # multi-dimensional b = [[1, 2, 3], [4, 5, 6]] # prints the number 4 @ index (1, 0) print(b[1][0]) In Julia these indexes would start from 1. It doesn't seem like `CartesianIndex` solves this, since looking at some examples you still have to specify array indexes (Which start from 1): https://docs.julialang.org/en/v1/base/arrays/#Base.IteratorsMD.CartesianIndex https://docs.julialang.org/en/v1/base/arrays/#Base.Iterators...
- improbable22 6y agoOK, I thought you were advocating zero based on complicated index calculations for some algorithm, with mod & div, which is what I was trying to say could be abstracted away. The literal syntax `a=[1,2,3]` makes a one-dimensional `Array{Int,1}` whose first element is indeed `a[1]`. (`b` is an array of arrays, while `c = [1 2 3; 4 5 6]` is an honest 2-dimensional array.)