Hacker News new | ask | show | jobs
by harshreality 756 days ago
Julia, R, Mathematica, Matlab, Fortran, Smalltalk, Lua...

Julia being a mostly functional non-systems-programming language, where memory addressing is usually in the background, I think you'd find you rarely use numeric-literal indexing and you were getting upset over nothing.

The typical looping over i from 1 to length(a) is not idiomatic in Julia, where looping over eachindex() or pairs() abstracts away any concern over index type (OffsetArray can have an arbitrary first index). There's firstindex() and lastindex(); bracket indexing syntax has begin and end special keywords. Multidimensional arrays have axes().

"But what about modular arithmetic to calculate 1-based indexes?"

Julia has mod1(), fld1(), etc., which also provide a hint to anyone reading the code that it might be computing things to be used as index-like objects (rather than pure arithmetic or offset computation).

The strict way to handle indexing, I suppose, would be to have distinct 1-indexed (e.g. Ind1) and 0-indexed (e.g. Ind0) Int-based types, and refuse to accept regular Ints as indexes. That would eliminate ambiguity, at the cost of Int-to-Ind[01] conversions all over the place (if one insists on using lots of numeric literals as indexes). And how would one abstract away modular arithmetic differences between the Ind0 and Ind1 types? By redefining regular mod and div for Ind1? That would be hella confusing, so that abstraction probably isn't viable.

1 comments

> The strict way to handle indexing, I suppose, would be to have distinct 1-indexed (e.g. Ind1) and 0-indexed (e.g. Ind0) Int-based types, and refuse to accept regular Ints as indexes.

I would be happy if unsigned integer UInt would start indexing from 0, whereas for signed integer it would start from 1. Also a module level parameter where preference on whether a literal integer shall be interpreted as Int or UInt would alleviate many usability issues there.