4 ms·
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/TwoBa
by chrispeel 11y ago
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 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/ 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.
- masklinn 11y ago> 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. It's never been needed, Pascal had arbitrary array bounds (including negative) 50 years ago. > 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. What does binary arithmetic have to do with anything? And have there been any study pertaining to the accessibility of 0-indexed versus 1-indexed arrays to high-scool programmers which would warrant such an assertion?
- JulianMorrison 11y agoZero based indexing counts offsets. Literally in C, you are saying "add n times sizeof(T) to the base pointer and dereference it". It rather implies arrays are contiguous in storage and lack any "holes". It's a crude but close-to-the-metal approach. C arrays are not general, they are one specific structure. An array could be implemented as a tree, but C doesn't have built-in support for that. One based indexing counts, or labels boxes. Counting implies no "holes", but abandons any implication about how they are laid out in storage. Labelling allows treating numerical arrays as sparse hash tables, with as many "holes" as needed. It's a high level view, suited to mathy ways of thinking about data.
- gct 11y agoYou can "label" by just agreeing that zero is in the set of counting numbers and labelling with those. Just because it's zero based doesn't mean it has to be an offset.
- gct 11y agoOne-based is just wrong. I've never been in a zero-based language and wanted one-based, but I go the other way all the time. Arrays beginning with zero are isomorphic to the ring formed by Z modulo N, and you can use a bit mask for modulo with a power-of-two array size. It's not an "implementation detail" that arrays start with zero it's a "zero is important to mathematics" thing. If I want to build a hash table, a power of two size for the underlying array is a nice performance optimization since you can use a bitmask instead of a modulo operation, and it makes a big performance difference, what does one-based give you other than some nebulous "it just _seems_ simpler to think about" kind of nonsense? I love how we expect people to grok the type system but we're afraid zero-based arrays will scare mathematicians off. See: https://en.wikipedia.org/wiki/Modular_arithmetic#Integers_modulo_n https://en.wikipedia.org/wiki/Modular_arithmetic#Integers_mo...