Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Julia actually just includes a dedicated function for this case (mod1), and that covers the vast majority of places where 0 base is easier.

In my cases of numerical code 1 based indexing causes fewer ±1 than 0 based indexing. (Nitpick: In modulo arithmetic 0 = N so having the index run from 1 to N is a ring just as well. The problem is the conventional representation of the equivalence class -N,0,N,2N,3N… in the numbers being 0 (which arises from arithmetics definition of modulo))



sort by: page size:

Tough luck. Using 0-indexing would have lost Matlab users to the Julia community. So they consciously decided to rather lose you. :-)

I got used to switching between 0/1-indexing. The only place it actually hurts is when using modulo to remain within some range. And this is needed much much less in Julia than in C and C-like.


Mathematical notation is almost always 1 based. Let $v = 1 \dots n$, that sort of thing. So I find translating mathematics into code (which I seem to do quite frequently) much easier with 1 based indexing. (One exception being if you have a lot of modulo arithmetic.)

I've used both 0 based (python, C++), and 1 based (R, matlab, mathematica), but I really belive 1 based is the right choice for Julia. It isn't necessary, but it feels better.


I had been comfortably programming in C and python for years and found 0-based indexing to be perfectly sensible. For the last couple of years I've been programming a lot in Julia. Initially I was horrified by the idea of indexing from 1 but I got the hang of it remarkably fast. Now I feel it's totally natural either way, depending what language I'm in.

I think it's only ever been a matter of preference as you say. And Julia was otherwise such a pleasure to write code in that it was easy for me to pay the small price to learn that.


Julia supports 0 based indexes if desired, similar to Ada and other languages.

"All indexing in Julia is 1-based"

I once designed/implemented a language where the biggest mistake I made was using 1-based indexing. At first it looked like it was going to be easier to understand/use but 0-based indexing is actually much more convenient when dealing with indexes arithmetic.

PS: the "metaprogramming" stuff is fabulous.


For what it's worth, I've ran into this a couple of times. Maybe it's just because I'm more familiar with 0-based indexing, but I think it's not just that. In every case the 0-based indexing had fewer (+1)'s and (-1)'s than the 1-based code. For instance k%n returns a number in the range [0,n), or in Julia notation [0:n-1]. When you want to do array indexing with that, you need (k%n)+1 in Julia.

Btw, Matlab also uses 1-based indexing, no? I thought that Julia chose 1-based indexing to get brain backwards compatibility with Matlab.


Complaining about 1-based indexing in Julia is so... 4 years old! Just use OffsetArrays, and you can use 0 based, or whatever base floats your boat (start underwater with -5, for example!)

It shouldn't. One-based indexing is what all the research uses, so it is undeniably the better choice for Julia. But zero-based indexing isn't just a memory trick, it makes lots of math easier when you're concatenating ranges or slicing arrays or doing modular arithmetic anywhere near an array.

If only Julia didn't index arrays starting from 1, rather than the more natural 0.

Could also be that C is superior in other ways, there isn't really a big difference between 0 and 1 based indexing, and thus 0 based won on the back of Cs other strengths.

The Dijkstra discussion is only aesthetic preference, nothing more.

Edit:

In Julia the examples would also idiomatically be written in terms of the provided mod1 function:

    new_index = mod1(old_index + 1, N)
    new_index = mod1(old_index - 1, N)

In math when we write a sum, we say "sum from i = 1 to n", not "sum from i = 0 to n-1".

Zero-based indexing of arrays makes sense in a language like C where arrays are just pointers and offsets are determined with arithmetic on pointer addresses in memory. This is why we are trained to write code like "for (i = 0; i < n; i++)". But in Julia where one of the stated purposes is to be able to write code that looks like formulae, it is far more natural to use one-based indexing.


Actually, it is quite easy to implement 0 based indices or any other indexing scheme in julia, since all the array indexing code is implemented in julia itself.

https://github.com/JuliaLang/julia/blob/master/j/array.j#L15...

I personally would find multiple indexing schemes confusing both for usage as well as to develop and maintain. Given that 1 based indexing seems to be a popular choice among many similar languages, we just went ahead with that.


Math/Science focused languages have usually used 1-based indexing, historically. Fortran, MATLAB, R, etc. In the primary fields where Julia is expected to be used, 0-based indexing would be a differentiator, not the other way around.

For those who don't like one-based indexing, your problem is solved: there is now a package which provides two-based indexing https://github.com/simonster/TwoBasedIndexing.jl ;-)

Edit: For a history of 0-based indexing see http://exple.tive.org/blarg/2013/10/22/citation-needed/ My understanding is that with modern compilers, there's no need for 0-based indexing; you can use 0-base, 1-base, or 69-base indexing as you prefer. 0-base is applicable to people creating chips or writing hardware drivers. Most scientific programmers are not so closely tied to hardware; 1-based indexing is one of the things that helps make Julia more accessible to high-school programmers or PhD students who don't want to learn binary arithmetic.


I don't think 1 based indexing is that useful. A lot of mathematics formulae start from 0 too and 1-indexing makes that more awkward.

Although I agree that some languages like Julia and say MATLAB feel more fluent to express mathematics.


I'm not extremely familiarized with Julia, but it seems to me that it would let you switch to 0-based indexing as an option.

> all serious numerical languages have that. it's more natural

And almost all serious general-purpose languages use 0-based. It is more natural at least to me. You see, this is exactly why 1-based index of Julia is disliked by many.


You do realize that there are approximately just as many use cases where 1 based indexing is more natural and involves less operations, right? It’s highly problem and context dependant.

But I’m not trying it make the point that 0 based or 1 based is better or worse than the other. I’m just saying that it’s a borderline immaterial difference for most use-cases and Julia gives many many tools for getting around any problems that may arise when a certain indexing scheme is awkward.

The 0 or 1 based debate is one of the most boring and pedantic arguments one can have and I do my best to ridicule people when they try to start it.


Disclaimer: Not a Julia user.

1. Base-1 indexing is more natural and less verbose. My suspicion is that base-0 indexing grew out of language implementers wanting to reduce their mental overhead rather than a first-principles approach. AKA machine code leaking out to the higher level languages.

2. You have to have some way of structuring syntax. Whether you have "end", ")" or "}". It is not inconsistent to start with something else, like a function signature with the corresponding keyword. Again Julia opts for natural readability.

Both of these complaints are rather superficial anyways. Julia is a marvelous piece of tech and has an interesting story to tell about type inference, multiple dispatch, performance, data-structures, general Lisp-yness and the merits of tailoring a language for a purpose/domain (and its users) in contrast to trying to adhere to paradigms and programmer culture (cults?).

next

Legal | privacy