1-based indexing in Lua

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
58 messages Options
123
Reply | Threaded
Open this post in threaded view
|

1-based indexing in Lua

Dirk Laurie-2
2018-03-29 7:48 GMT+02:00 Charles Heywood <[hidden email]>:

> Lua is the most commonly used, though. ColdFusion also gets some nitpicks
> about it but there's so many other reasons to pick on CF.
>
>
> On Thu, Mar 29, 2018, 12:47 AM David Crayford <[hidden email]> wrote:
>>
>> I've never understood why Lua attracts so much opprobrium for 1 based
>> indexing. I started my career working on mainframes and all the
>> languages had 1 based indexing (COBOL, PL1, Fortran, REXX).
>> It wasn't until I started using C that I had ever used a language with 0
>> based indexing which makes sense for a language where arrays decay to
>> pointers. IMO, the Lua authors got it right.

Agreed.

Moreover, it is not strictly correct to say that Lua has 1-based indexing.

Even to say that the Lua standard library and the default length
function have 1-based indexing, is only partially correct. When
working with bytes in files, the io library has 0-based indexing.

The actual range of indices in a table can be anything the user wants.
Negative indices are useful; take for example the 'arg' array. I much
prefer the Lua way, where t[-1] means t[-1], to the Python way, where
t[-1] means t[#t-1], and to the C way, where t[-1] means "let's hope
the pointer t is pointing to something that won't make the program
crash".

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Axel Kittenberger
> Moreover, it is not strictly correct to say that Lua has 1-based indexing.

Mixed 1-based and 0-based indexing is the worst possible state of affairs regarding this for a programming system.

As I replied in that survey discussion recently, where remarkably many 1-based languages are rated in the "most hated" section.
If you have to mix it is bad for one's brain and since this question likely revolved around "programming languages I have to use beside my main language" this is the result of the majority of currently popular programming languages are 0-based. Not to argue it is better by itself, but there is definitely a bandwagon effect.

On Thu, Mar 29, 2018 at 10:36 AM, Dirk Laurie <[hidden email]> wrote:
2018-03-29 7:48 GMT+02:00 Charles Heywood <[hidden email]>:
> Lua is the most commonly used, though. ColdFusion also gets some nitpicks
> about it but there's so many other reasons to pick on CF.
>
>
> On Thu, Mar 29, 2018, 12:47 AM David Crayford <[hidden email]> wrote:
>>
>> I've never understood why Lua attracts so much opprobrium for 1 based
>> indexing. I started my career working on mainframes and all the
>> languages had 1 based indexing (COBOL, PL1, Fortran, REXX).
>> It wasn't until I started using C that I had ever used a language with 0
>> based indexing which makes sense for a language where arrays decay to
>> pointers. IMO, the Lua authors got it right.

Agreed.

Moreover, it is not strictly correct to say that Lua has 1-based indexing.

Even to say that the Lua standard library and the default length
function have 1-based indexing, is only partially correct. When
working with bytes in files, the io library has 0-based indexing.

The actual range of indices in a table can be anything the user wants.
Negative indices are useful; take for example the 'arg' array. I much
prefer the Lua way, where t[-1] means t[-1], to the Python way, where
t[-1] means t[#t-1], and to the C way, where t[-1] means "let's hope
the pointer t is pointing to something that won't make the program
crash".


Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Andrew Gierth
In reply to this post by Dirk Laurie-2
>>>>> "Dirk" == Dirk Laurie <[hidden email]> writes:

 >> Lua is the most commonly used [language with 1-based indexing]

Not so; even if you disregard COBOL (which is still pretty widely used),
I would bet that SQL (which also uses 1-based array indexes) is much
more commonly used than Lua.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Dirk Laurie-2
2018-03-29 13:28 GMT+02:00 Andrew Gierth <[hidden email]>:
>>>>>> "Dirk" == Dirk Laurie <[hidden email]> writes:
>> Lua is the most commonly used [language with 1-based indexing]

Correction: Not mine. I was quoting Charles Heywood.

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Dong Feng
In reply to this post by Axel Kittenberger


On Thu, 29 Mar 2018 at 01:52 Axel Kittenberger <[hidden email]> wrote:
> Moreover, it is not strictly correct to say that Lua has 1-based indexing.

Mixed 1-based and 0-based indexing is the worst possible state of affairs regarding this for a programming system.


It's not if there is proper justification. 0-based indexing should be an "offset". It's justified in C because C array indexing is always an "offset". You can write:

int a[3];
1[a] = 5;
*(a+2) = 6;

Not the case for all languages' arrays.
Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Coda Highland
On Thu, Mar 29, 2018 at 10:30 AM, Dong Feng <[hidden email]> wrote:

>
>
> On Thu, 29 Mar 2018 at 01:52 Axel Kittenberger <[hidden email]> wrote:
>>
>> > Moreover, it is not strictly correct to say that Lua has 1-based
>> > indexing.
>>
>> Mixed 1-based and 0-based indexing is the worst possible state of affairs
>> regarding this for a programming system.
>>
>
> It's not if there is proper justification. 0-based indexing should be an
> "offset". It's justified in C because C array indexing is always an
> "offset". You can write:
>
> int a[3];
> 1[a] = 5;
> *(a+2) = 6;
>
> Not the case for all languages' arrays.

The argument isn't that it's absolutely bad for a language to have
1-based indexing.

The argument is that it's bad to MIX 1-based and 0-based indexing in
the same system, and unfortunately for Lua's design as an embeddable
and extendable language, that's exactly the case here. The vast
majority of applications that will embed Lua will use 0-based indexing
internally, and a similar majority of libraries that one might wish to
bind to Lua will also use 0-based indexing.

While it is of course possible to translate between the two at the API
level so that each language involved can operate using their own
preferred style, programmers that are working in both the host's code
and Lua code, or who are using the same library both directly and
bound to Lua, will have to mentally switch between the two indexing
styles, which increases the likelihood of human error.

Obviously, it's stuff that's usually going to shake out during testing
and debugging. But it's still a level of overhead that wouldn't be
there if host language and embedded language were in agreement.

On the other hand, it would be a BIGGER source of errors to CHANGE the
status quo after the ecosystem has been established. This is the
reason that I have never argued that Lua should change -- it's a speed
bump, but only a minor one, and one that any competent developer will
be able to manage with experience; the alternative is far worse.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Dong Feng

The argument isn't that it's absolutely bad for a language to have
1-based indexing.

The argument is that it's bad to MIX 1-based and 0-based indexing in
the same system, and unfortunately for Lua's design as an embeddable
and extendable language, that's exactly the case here. The vast
majority of applications that will embed Lua will use 0-based indexing
internally, and a similar majority of libraries that one might wish to
bind to Lua will also use 0-based indexing.



I think people are customized to the 0-based indexing in the context of C. But that's all about it, just a custom, and one should be restricted within the C context (because everything is more or less an offset).

A programmer could more easily fall into more serious trap if he/she mix the mindset of offset and index (e.g. using continuous block with using non-continuous data structure). I think Lua's approach, though not solving all problems (and has its own pitfall), is not by nature inferior to a universal 0-base indexing system. We would explore auxiliary pattern such as always naming parameter properly as "index" or "offset".

Dong

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Coda Highland
On Thu, Mar 29, 2018 at 2:11 PM, Dong Feng <[hidden email]> wrote:

>>
>> The argument isn't that it's absolutely bad for a language to have
>> 1-based indexing.
>>
>> The argument is that it's bad to MIX 1-based and 0-based indexing in
>> the same system, and unfortunately for Lua's design as an embeddable
>> and extendable language, that's exactly the case here. The vast
>> majority of applications that will embed Lua will use 0-based indexing
>> internally, and a similar majority of libraries that one might wish to
>> bind to Lua will also use 0-based indexing.
>>
>>
>
> I think people are customized to the 0-based indexing in the context of C.
> But that's all about it, just a custom, and one should be restricted within
> the C context (because everything is more or less an offset).
>
> A programmer could more easily fall into more serious trap if he/she mix the
> mindset of offset and index (e.g. using continuous block with using
> non-continuous data structure). I think Lua's approach, though not solving
> all problems (and has its own pitfall), is not by nature inferior to a
> universal 0-base indexing system. We would explore auxiliary pattern such as
> always naming parameter properly as "index" or "offset".
>
> Dong
>

Yes, that's... exactly what I said: It isn't by nature inferior, but
mixing up the mindsets is the real risk, and Lua's nature as an
embeddable/extensible language puts it in a position where you might
have to be exposed to both at once.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Dirk Laurie-2
In reply to this post by Dong Feng
018-03-29 17:30 GMT+02:00 Dong Feng <[hidden email]>:

>
>
> On Thu, 29 Mar 2018 at 01:52 Axel Kittenberger <[hidden email]> wrote:
>>
>> > Moreover, it is not strictly correct to say that Lua has 1-based
>> > indexing.
>>
>> Mixed 1-based and 0-based indexing is the worst possible state of affairs
>> regarding this for a programming system.
>>
>
> It's not if there is proper justification. 0-based indexing should be an
> "offset". It's justified in C because C array indexing is always an
> "offset". You can write:
>
> int a[3];
> 1[a] = 5;
> *(a+2) = 6;
>
> Not the case for all languages' arrays.

This comment is perceptive.

For files, it's always an offset, since a file has a position.

When it comes to ordinals, most natural languages call things first,
second, third, fourth etc, which naturally map to 1,2,3,4 etc.

There is just one place in Lua besides file offsets where base-0
should have been used: the position pattern (). It should have
captured the position just before the current position.
(''):match"()" should have been 0, not 1; ('abcd'):match'().*()'
should have been 0 and 4, not 1 and 5, etc. It annoys me in LPEG too.
But it's too late now.

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Marc Balmer
In reply to this post by Coda Highland


> Am 29.03.2018 um 19:01 schrieb Coda Highland <[hidden email]>:
>
> On Thu, Mar 29, 2018 at 10:30 AM, Dong Feng <[hidden email]> wrote:
>>
>>
>> On Thu, 29 Mar 2018 at 01:52 Axel Kittenberger <[hidden email]> wrote:
>>>
>>>> Moreover, it is not strictly correct to say that Lua has 1-based
>>>> indexing.
>>>
>>> Mixed 1-based and 0-based indexing is the worst possible state of affairs
>>> regarding this for a programming system.
>>>
>>
>> It's not if there is proper justification. 0-based indexing should be an
>> "offset". It's justified in C because C array indexing is always an
>> "offset". You can write:
>>
>> int a[3];
>> 1[a] = 5;
>> *(a+2) = 6;
>>
>> Not the case for all languages' arrays.
>
> The argument isn't that it's absolutely bad for a language to have
> 1-based indexing.
>
> The argument is that it's bad to MIX 1-based and 0-based indexing in
> the same system, and unfortunately for Lua's design as an embeddable
> and extendable language, that's exactly the case here. The vast
> majority of applications that will embed Lua will use 0-based indexing
> internally, and a similar majority of libraries that one might wish to
> bind to Lua will also use 0-based indexing.

If you embed Lua in any hosting program, you will have to carefully provide Luaisms anyways (iterators, strings as options etc.).  So index basing is one such thing.  An come on, adding or subtracting 1 really isn’t that hard, right?

>
> While it is of course possible to translate between the two at the API
> level so that each language involved can operate using their own
> preferred style, programmers that are working in both the host's code
> and Lua code, or who are using the same library both directly and
> bound to Lua, will have to mentally switch between the two indexing
> styles, which increases the likelihood of human error.

I disagree strongly.  This is exactly my day job, to dig in C and Lua code and make sure they properly interact.  It’s really easy to get this right.  And yes, I even speak more languages than my mother tongue and I can switch on the spot…

>
> Obviously, it's stuff that's usually going to shake out during testing
> and debugging. But it's still a level of overhead that wouldn't be
> there if host language and embedded language were in agreement.

Two languages can never be in „agreement“ because they are, well, different languages.

>
> On the other hand, it would be a BIGGER source of errors to CHANGE the
> status quo after the ecosystem has been established. This is the
> reason that I have never argued that Lua should change -- it's a speed
> bump, but only a minor one, and one that any competent developer will
> be able to manage with experience; the alternative is far worse.

Changing this would indeed be a very bad idea ;)

>
> /s/ Adam
>


Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Francisco Olarte
In reply to this post by Dirk Laurie-2
On Thu, Mar 29, 2018 at 10:36 AM, Dirk Laurie <[hidden email]> wrote:
.....
> Negative indices are useful; take for example the 'arg' array. I much
> prefer the Lua way, where t[-1] means t[-1], to the Python way, where
> t[-1] means t[#t-1],

You are comparing lua tables, which are dictionaries with some fancy
optimization for positive integer indexes, to python lists, which are
not.

If you use the closer type to a lua table in python, a dict(), t[-1]
works as in lua ( and neary all other operations ).

> and to the C way, where t[-1] means "let's hope
> the pointer t is pointing to something that won't make the program
> crash".

t[-1] is used a lot in C, and means *(t-1), C is a totally different
beast than lua. C [] are a dereference operation, so you should be
"hoping" for it everytime you use it. I normally do not hope for my
pointers to be valid, I know they are. Sometimes I'm wrong, but this
is life. It's one of the problems of using a more low level language,
which lets you do more things, like implementing lua.

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Doug Gale


On Fri, Mar 30, 2018 at 11:05 AM, Francisco Olarte <[hidden email]> wrote:
On Thu, Mar 29, 2018 at 10:36 AM, Dirk Laurie <[hidden email]> wrote:
.....
> Negative indices are useful; take for example the 'arg' array. I much
> prefer the Lua way, where t[-1] means t[-1], to the Python way, where
> t[-1] means t[#t-1],

​...



​I hate one-based indexing​. It adds a ton of confusion when embedding lua into a native programming language project, and tarnishes what I'd consider to be a fantastically good language otherwise.

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Enrico Colombini
On 31-Mar-18 14:24, Doug Gale wrote:
> ​I hate one-based indexing​. It adds a ton of confusion when embedding
> lua into a native programming language project, and tarnishes what I'd
> consider to be a fantastically good language otherwise.

I started with 6502 assembly (0-based) then learned BASIC (1-based) then
C (0-based) then Pascal (1-based) then... well, you get the idea :-)

After a while you just get used to think in the context of the language
you are writing in, even when you are mixing them in the same project.

As Marc Balmer said, it is the same as for human languages.

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Albert Chan
> On Mar 31, 2018, at 10:31 AM, Enrico Colombini <[hidden email]> wrote:
>
> I started with 6502 assembly (0-based) then learned BASIC (1-based) then C (0-based) then Pascal (1-based) then... well, you get the idea :-)
>
> After a while you just get used to think in the context of the language you are writing in, even when you are mixing them in the same project.
>
> As Marc Balmer said, it is the same as for human languages.
>
> --
>  Enrico
>

You can use lua table with arbitrary base by using the hash part:

local k = {[0]=0, 1}   -- 0-based table

Above is actual code of my primepi.lua (line 20)
Doing it in 1-based array is messy and slower.

https://github.com/achan001/PrimePi/blob/master/primepi.lua



Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Albert Chan
In reply to this post by Enrico Colombini

>> On 31-Mar-18 14:24, Doug Gale wrote:
>> I hate one-based indexing. It adds a ton of confusion when embedding lua into a native programming language project, and tarnishes what I'd consider to be a fantastically good language otherwise.

My last post responded to the wrong comment ... Sorry

You can use lua table with arbitrary base by using the hash part:

local T = {[0]=0, 1}   -- 0-based critical table

Above is actual code of my primepi.lua (line 20)
Doing it in 1-based array is messy and slower.

https://github.com/achan001/PrimePi/blob/master/primepi.lua

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

KHMan
In reply to this post by Enrico Colombini
On 3/31/2018 10:31 PM, Enrico Colombini wrote:

> On 31-Mar-18 14:24, Doug Gale wrote:
>> ​I hate one-based indexing​. It adds a ton of confusion when
>> embedding lua into a native programming language project, and
>> tarnishes what I'd consider to be a fantastically good language
>> otherwise.
>
> I started with 6502 assembly (0-based) then learned BASIC
> (1-based) then C (0-based) then Pascal (1-based) then... well, you
> get the idea :-)
>
> After a while you just get used to think in the context of the
> language you are writing in, even when you are mixing them in the
> same project.
>
> As Marc Balmer said, it is the same as for human languages.

+1, folks who react badly to 1-based indexing may be loud on blogs
and other places, but there are plenty of people who use multiple
programming languages just fine.

But because discussions are often about possible defects or
perceived defects, such 'negative' issues are over-represented.

Coders who think indices cause problems should abstract it away in
some sort of container or object, like the range loops that
Stroustrup recommends in C++, then nobody will get hurt.


--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia


Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Coda Highland
On Sat, Mar 31, 2018 at 10:37 AM, KHMan <[hidden email]> wrote:
> Coders who think indices cause problems should abstract it away in some sort
> of container or object, like the range loops that Stroustrup recommends in
> C++, then nobody will get hurt.

Yes, and then ipairs gets argued about. :P

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

KHMan
On 4/1/2018 3:17 AM, Coda Highland wrote:
> On Sat, Mar 31, 2018 at 10:37 AM, KHMan wrote:
>> Coders who think indices cause problems should abstract it away in some sort
>> of container or object, like the range loops that Stroustrup recommends in
>> C++, then nobody will get hurt.
>
> Yes, and then ipairs gets argued about. :P

Let them argue, I think nothing much will happen even if the
discussion runs to a thousand postings in a week. No problem for
me, I will just watch from my armchair (and yawn). :-) There is
more than one way to abstract things. Or put in safeguards. I
don't think anyone has made coding as safe as coding for aerospace
software and yet be greatly productive in churning out code.

I have used many primitive dev tools and so tend not to complain a
lot these days considering the richness of the dev tools
available. I'm also less interested in perfection in a programming
language these days; I do not expect perfection, or to have a
programming language to be perfectly suited for my personal tastes
and preferences. Would any programming language be perfectly
suited for every type and personality of coders? (A magic wand
would work though.) If the thing gets the job done, I'm OK.


> /s/ Adam


--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia



Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Ahmed Charles
Written before I was born, EWD831 (http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html) describes why 0 based ranges are superior better than I can. It has nothing to do with programming languages and everything to do with math.
Reply | Threaded
Open this post in threaded view
|

Re: 1-based indexing in Lua

Oliver Kroth


Am 03.04.2018 um 15:02 schrieb Ahmed Charles:
> Written before I was born, EWD831
> (http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html)
> describes why 0 based ranges are superior better than I can. It has
> nothing to do with programming languages and everything to do with math.
Actually, Dijkstra just prefers to have items counted like 0,...,N-1
instead of 1,...,N
odd.

When I count things, let's say, cows, I start with the first cow and say
"one", second cow "two",  third cow counted "three"...
I would not start with the first cow and say "zero", I say "zero" when
there is no cow at all.
A zero-th cow is the cow in the empty corral, and actually "not a cow"...

--
Oliver


123