News for hackers. Stuff that matters.
Re:From Python to Lua: Why We Switched by qwertyuiop924 on Tuesday January 26, 2016 @03:01PM (#10975588)
Yeah, the only real advantage of lua is that you can link your code into the binary, and that it's super lightweight. But you can still deploy a python binary with your code, so the only real reason for switching is, "I like lua, and I felt the need to make my team switch."

Not that I don't lua. It's like the best parts of javascript, mixed with some AWK and scheme, with all the crap removed. OTOH, from-1 indexing is evil.

Re:From Python to Lua: Why We Switched by cbetz on Tuesday January 26, 2016 @05:46PM (#10976846)
I find that the only reasonable complaint that can be made about Lua is the from-1 indexing. Like: "I found this really awesome language and it's ONLY wart is that it doesn't index arrays like every other programming language in the world".

Yet, I still admire the Lua creators for making this bold choice. Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Re:From Python to Lua: Why We Switched by bsder on Tuesday January 26, 2016 @06:21PM (#10977032)
> Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Well, we obviously can use 1-based indexing. Ada/VHDL allows you to define the ranges of your indicies even beyond 0 or 1 based (very useful in fixed point mathematics where you sometimes want to index your bits by a negative power of 2).

The big disadvantage is that you lose the algebraic properties of mapping to the ring of integers modulo N when your indicies are 1-based.

This leads to a bunch of off-by-one bugs in various places. One of the most pernicious is that while you can get to the next index with "next = prev % N + 1" you actually have to do "prev = (next - 1) % N". Note that the previous REQUIRES the parentheses due to precedence of operations.

0-based may be convention, but it isn't ill thought out. FORTRAN(1-based) and C(0-based) coexisted a LONG time. Pascal was(is?) 1-based. Most people had experience with both.

People who designed programming languages CHOSE the 0 convention. Overwhelmingly.

Re:From Python to Lua: Why We Switched by LeonS on Tuesday January 26, 2016 @10:53PM (#10978089)
After seeing the light with Ada I never want to go back - indexing based on enums, modulos, etc. just makes so much more sense (as long as you're using something strongly typed)
Re:From Python to Lua: Why We Switched by jmpeax on Tuesday January 26, 2016 @08:14PM (#10977549)
> Why should we use from-0 indexing for all eternity? Just because it's always been that way? This is a real question if you have the answer.

Here is the answer: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...

Re:From Python to Lua: Why We Switched by tzs on Tuesday January 26, 2016 @07:40PM (#10977422)
In addition to Lua, these languages are 1-based: ALGOL, AWK, COBOL, Fortran, Julia, Mathematica, MATLAB, R, PL/I, and RPG.
Re:From Python to Lua: Why We Switched by ufo on Wednesday January 27, 2016 @02:12AM (#10978715)
BTW, FORTRAN is the big reason Lua has 1-based indexing. Lua' s first users were engineers, which were more familiar with FORTRAN.
Re:From Python to Lua: Why We Switched by kencausey on Tuesday January 26, 2016 @09:45PM (#10977870)
And Smalltalk
Re:From Python to Lua: Why We Switched by technomancy on Tuesday January 26, 2016 @08:45PM (#10977662)
> I find that the only reasonable complaint that can be made about Lua is the from-1 indexing.

Lack of arity checks is definitely much more problematic than this, but still... if you can count the number of tragic design flaws in your language on one hand, you're in great shape compared to the rest of what's out there!

Re:From Python to Lua: Why We Switched by qwertyuiop924 on Wednesday January 27, 2016 @08:13AM (#10979822)
No, we use from-zero indexing because, A) it's closer to the hardware, and B) because of mod(). There was an interesting comment thread on ESR's blog about this a while ago, but unfortunately, I can't find it.
Re:From Python to Lua: Why We Switched by fit2rule on Tuesday January 26, 2016 @03:14PM (#10975673)
>>OTOH, from-1 indexing is evil.

This is a straw man. You're not forced to index[1]. You can create tables that index[0], no worries.

Re:From Python to Lua: Why We Switched by LewisJEllis on Tuesday January 26, 2016 @04:05PM (#10976059)
Sure, technically you /can/ do it, but only in the same way that you can use negative indices in JavaScript arrays and objects. The 0-indexed element won't tie into length operators without additional manual bookkeeping with table.setn.

  $ lua
  Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
  > x = {}
  > x[0] = 1
  > =#x
  0
  > =x[0]
  1
  > x[1] = 2
  > =#x
  1
  >
  $ node
  > x = []
  []
  > x[-1] = 0
  0
  > x[0] = 1
  1
  > x
  [ 1, '-1': 0 ]
  > x.length
  1
That's reason enough IMO to just suck it up and use 1-based indexing, even before you add munificent's (stronger) point about standard libraries and the rest of the ecosystem.

I agree that 1-based indexing isn't as much of a downside as people make it out to be, but saying you can ignore it and index from 0 "no worries" is just being silly.

Re:From Python to Lua: Why We Switched by qwertyuiop924 on Tuesday January 26, 2016 @05:51PM (#10976877)
In lua, iterating over arrays/tables with things like ipairs() is the idiom, so it's not so bad, but if you want to do anything involving mod, you have to add an extra plus 1. this is where bugs happen.
Re:From Python to Lua: Why We Switched by LewisJEllis on Tuesday January 26, 2016 @08:49PM (#10977678)
Yes, ipairs is idiomatic, but that doesn't matter here because ipairs doesn't return the 0 index.

  > x = {}
  > x[0] = 2
  > x[1] = 3
  > for i, v in ipairs(x) do print(i, v) end
  1	3
  > x[2] = 4
  > for i, v in ipairs(x) do print(i, v) end
  1	3
  2	4
Re:From Python to Lua: Why We Switched by qwertyuiop924 on Wednesday January 27, 2016 @08:19AM (#10979862)
No, what I mean is that it's not idiomatic to access lua "arrays" by index.
Re:From Python to Lua: Why We Switched by fit2rule on Wednesday February 03, 2016 @07:13AM (#11026027)
The only requirement is the Lua developer knowing how to use a Lua table for anything they need.
Re:From Python to Lua: Why We Switched by munificent on Tuesday January 26, 2016 @03:30PM (#10975790)
Except the standard libraries, all third party libraries, and every existing Lua user maintaining your code will assume 1-based indexing.

Cars don't require you to drive on the right side of the road either (or whatever side your nationality prefers), but you're going to have a bad time in traffic if you try to switch.

Re:From Python to Lua: Why We Switched by fit2rule on Wednesday January 27, 2016 @03:29AM (#10978933)
If you need 0-based arrays, use them. Its no big deal. Any Lua programmer worth their salt knows that Lua uses 1-arrays - so? Set up a 0-array and off you go.
Re:From Python to Lua: Why We Switched by qwertyuiop924 on Wednesday January 27, 2016 @08:20AM (#10979864)
Either you're an idiot, or you're being sarcastic. I will assume the ladder.
Re:From Python to Lua: Why We Switched by fit2rule on Wednesday February 03, 2016 @07:10AM (#11026012)
In fact, I'm learning a lot from this thread, particularly about idiocy and sarcasm, although very few actual real arguments for why array[1] is 'inferior' to array[0], because in fact there is no such valid argument.

Lua standard libraries do not suffer this problem. If you have a legacy / cross-platform issue about it, well then .. its a good thing one is 'wise enough to know array[0] is a thing', but its hardly relevant to the question of whether this 'inferior accessing of arrays in Lua' is anything more than fallacy.

Idiomatic Lua involves intelligent use of pairs/ipairs/table index schema, and metatables if needed, to solve the data-access problem. And guess what? A competent Lua developer either has had zero problems with this issue (because they are competent and know how to wire up a C/C++ data structure to the Lua vm), or they have infinite, multiple "problems with Lua", of their own devising, nevertheless, but hey .. you can't "blame Lua" for that. Lua gives you the keys to the universe. If you crash into someones mailbox, well then ..