News for hackers. Stuff that matters.
Re:The unreasonable effectiveness of the Julia programming l... by ogogmad on Friday October 09, 2020 @02:11PM (#24732709)

  - Arrays start at one
Why is this such a big deal? You just change the range of your for-loops from 0<=i<n to 1<=i<=n. Are you using cyclic buffers?
Re:The unreasonable effectiveness of the Julia programming l... by Athas on Friday October 09, 2020 @02:19PM (#24732786)
It matters when doing multidimensional index arithmetic. The formulae for ranking and unranking multi-dimensional to flat indexes are neater with 0-indexing than with 1-indexing.
Re:The unreasonable effectiveness of the Julia programming l... by improbable22 on Friday October 09, 2020 @02:36PM (#24732956)
Julia has really nice abstractions for dealing with this. Things for which you might do such calculations in other languages can often be done in a way generic to the number of dimensions, as well as the initial index, using CartesianIndices & friends. And this should be zero cost.
Re:The unreasonable effectiveness of the Julia programming l... by Athas on Friday October 09, 2020 @04:31PM (#24734123)
Almost all languages above the C level has nice abstractions for this, but you still sometimes need to do it for various reasons. Basically, whenever the indexes don't matter, then 1-indexing and 0-indexing are equivalently good, but when they do matter, then 0-indexing leads to neater calculations.
Re:The unreasonable effectiveness of the Julia programming l... by fghorow on Friday October 09, 2020 @11:23PM (#24736489)
OffsetArrays.jl is your friend.

I'm working on a project where indexing naturally runs from -n to n. I kludged the usual index hackery and was so plagued with off-by-one errors that I gave up on it. Then I remembered Tim Holy had a blog post about OffsetArrays.jl and started using that.

Problem solved.

Seriously, the most annoying thing I find about Julia is that the various packages fairly often have "creative" names, that make it difficult discover that they might actually be useful.

YMMV.

Re:The unreasonable effectiveness of the Julia programming l... by porker on Saturday October 10, 2020 @06:02AM (#24737841)
> I'm working on a project where indexing naturally runs from -n to n. I kludged the usual index hackery and was so plagued with off-by-one errors that I gave up on it. Then I remembered Tim Holy had a blog post about OffsetArrays.jl and started using that.

Ah, thank you. I've been tinkering around with Julia, and was trying out 2D random walks. Not being able to use a 2 dimensional array and store the position [0, 0] confused me. I ended up starting at X,X (where X > total number of moves) and then got stuck with plotting it nicely, because I wanted the plot axes to show how far it had moved, not X +- how far it had moved.

Re:The unreasonable effectiveness of the Julia programming l... by 7thaccount on Friday October 09, 2020 @09:36PM (#24736089)
There are plenty of matrix uses where 1-indexing is more natural. I think the main reason Julia uses it is to be more consistent with Matlab.
Re:The unreasonable effectiveness of the Julia programming l... by ntoshev on Saturday October 10, 2020 @11:03AM (#24739438)
Abstraction discovery is a hard problem in general. Good names help, but there's probably more we can do. Tooling finding a common pattern that the abstraction eliminates, perhaps?
Re:The unreasonable effectiveness of the Julia programming l... by improbable22 on Saturday October 10, 2020 @02:28PM (#24740839)
I guess it could poke you every time you say `strides(A)`?

Here's a recent example of an upgrade, from code with stride calculations to a more abstracted version, which is generic to offsets & number of dimensions:

https://github.com/JuliaLang/julia/pull/37367/files#diff-29c...

Re:The unreasonable effectiveness of the Julia programming l... by ogogmad on Friday October 09, 2020 @02:21PM (#24732808)
Fair enough. But is this a common enough occurrence in the life of a programmer that it's worth being a religious extremist about?
Re:The unreasonable effectiveness of the Julia programming l... by leephillips on Friday October 09, 2020 @02:30PM (#24732889)
No need for an inquisition. In both Fortran and Julia you can set your index origin to be whatever works best for the problem you are expressing.
Re:The unreasonable effectiveness of the Julia programming l... by remram on Friday October 09, 2020 @04:22PM (#24734029)
It's in a list of annoyances, together with 16 others. I don't think anyone is being "a religious extremist about" it...
Re:The unreasonable effectiveness of the Julia programming l... by TheRealKing on Saturday October 10, 2020 @03:40PM (#24741313)
In Fortran, you can start the index from any number you want. A(-10:-3) is a completely valid slice or full array declaration, so are A(0:7) or A(1:8). and this feature is absolutely useful in scientific computing.
Re:The unreasonable effectiveness of the Julia programming l... by pasabagi on Saturday October 10, 2020 @04:27PM (#24741638)
I also didn't think it was a big deal - I write a lot of lua, and I love lua - but every time I have to do anything involving modulo maths and arrays it's a pain in the ass.
Re:The unreasonable effectiveness of the Julia programming l... by ddragon on Saturday October 10, 2020 @05:42PM (#24742083)
Julia makes it easier for example with functions that handle mod in the context of 1-indexed arrays (if you don't want to use OffsetArrays).

https://docs.julialang.org/en/v1/base/math/#Base.mod1

Re:The unreasonable effectiveness of the Julia programming l... by pasabagi on Saturday October 10, 2020 @06:11PM (#24742240)
Well, that seems cool, but isn't indexing from zero simpler?

I don't really remember if zero-indexing was a big deal when I was learning to program. Are there big advantages for non-programmers? I figure that's why Lua went the 1-index route.

Re:The unreasonable effectiveness of the Julia programming l... by ddragon on Saturday October 10, 2020 @06:48PM (#24742462)
>I don't really remember if zero-indexing was a big deal when I was learning to program

And neither is 1-indexing even though people are always complaining about it. I kinda like when indexing from the third element to the seventh element to be just 3:7, instead of 2:6. and the last element to be length(x) instead of length(x)-1. And that use case is much more frequent to me than creating circular buffers on an array. And in Julia you can just use begin:end as well, or any iterator, it's really overall a non-issue for me.

Not sure about Lua, but in Julia (and Matlab, and R) is just because they actually have vectors/matrices types that have a convention that predates even programming, while it makes sense for languages that an array is basically a memory offset (that maps to a pointer arithmetic logic) to start in 0.

Re:The unreasonable effectiveness of the Julia programming l... by TheRealKing on Saturday October 10, 2020 @03:42PM (#24741329)
Array should start from any index scientist wishes. That is how it is done in Fortran.
Re:The unreasonable effectiveness of the Julia programming l... by adolgert on Friday October 09, 2020 @02:17PM (#24732769)
Re:The unreasonable effectiveness of the Julia programming l... by goto11 on Friday October 09, 2020 @03:11PM (#24733330)
Dijkstra has a certain tone. He can make his opinions sound almost like a mathematical proof. But when you dig into it, he just says 0-based is "nicer".
Re:The unreasonable effectiveness of the Julia programming l... by ogogmad on Friday October 09, 2020 @02:20PM (#24732802)
The points he makes are so extremely minor... You could just as easily say that natural language and mathematical convention are index-1.
Re:The unreasonable effectiveness of the Julia programming l... by e12e on Friday October 09, 2020 @08:11PM (#24735674)
I never understood the fascination with preferring "the element at zero offset from the start" to "the first element" - and I don't think djiskarta makes a compelling argument.