Hacker News new | ask | show | jobs
by jmde 3556 days ago
This could either be brilliant or a total nightmare:

"Support for arrays with indexing starting at values different from 1. The array types are expected to be defined in packages, but now Julia provides an API for writing generic algorithms for arbitrary indexing schemes (#16260)."

Originally I thought total nightmare but now I'm not sure.

3 comments

Many people think this was added for support of 0 offset arrays, but that's only a side effect really. This feature was requested by people who work with rather odd arrays (e.g. diagonal slices through high dimensional data). I have mentioned in previous threads on the subject that julia's 1-indexed arrays don't matter much in practice because the generic APIs hide that fact most of the time. This just adds the last bit of code to make that completely true and adds documentation/cleanup. For those seeking more information, I'd recommend Tim Holy's JuliaCon keynote on the subject: https://www.youtube.com/watch?v=fl0g9tHeghA
> I have mentioned in previous threads on the subject that julia's 1-indexed arrays don't matter much in practice because the generic APIs hide that fact most of the time.

My guess is that most people who prefer zero-based feel strongly about it, and most people that like one-based don't care that much. If it really doesn't matter that much, I think Julia should've gone with zero. FFTs, Vandermonde matrices (least squares), polynomials, and pretty much anything where the array subscript relates to the mathematics is cleaner with zero-based arrays.

> My guess is that most people who prefer zero-based feel strongly about it, and most people that like one-based don't care that much.

FWIW, as someone in the latter category (liking one-based indexing), I'd say you're partially correct: I'm not emotional attached to 1-based indexing the way some 0-based folks seem to be, but it's a practical nicety and one of the things I'm really glad Julia chose to do. Making 0-based the default would definitely reduce its appeal to me.

Call it emotional if you like, but I won't use a language that has an off by one error built into its specfication :-)
Pascal and Ada have had arbitrary indexing for decades. It's been fine.
And Fortran, where it works fine. Perl lets you change the array index offset but everyone warns you that your computer will reach out and slap you in the face if you do. As is often the case, the devil is in the details.
In perl is it APL style where it changes the index for the entire world (until changed again) or is it Fortran/Pascal style where you are expected to declare the lowest/highest index per array?
APL-style with a global index origin ($[ in Perl, à la ⎕IO in most APLs).
Pascal and Ada don't usually use their arrays as matrices in mathematical expressions. Julia is targeted at people using Matlab.

How do you multiply two matrices that use different indexing? Ignore the indexing differences?

It's also problematic when different libraries use different conventions (0-based vs 1-based). Can you use the output of one such library to feed another?

Ada has the notion of attributes applying to types and variables, to access related properties. Among those, there are attributes to get the starting and ending indexes of an array. So it's possible to program completely generic array processing, independent of the underlying choice or range for the array. All this from memory, and it's been a long time...
Eiffel too IIRC (same family I suppose)
Doesn't Fortran feature variable indices for quite a while now?

I think the varying indices are a great feature for porting old code over from other programming languages.