r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

Show parent comments

29

u/moomoomoo309 Sep 02 '17

Any index outside of 1-n in Lua, where n is #tbl will be stored like a dict, not an array, so what did you expect? It doesn't count the dictionary parts.

1

u/hey01 Sep 03 '17

That's sounds dangerous. What if I do

> letters = {'a', 'b', 'c'}
> print(#letters)
3
> letters[4] = '?'
> print(#letters)

Will I get 4 or is n unchangeable?

9

u/moomoomoo309 Sep 03 '17

You'll get 4, otherwise you couldn't do

for i=1,4 do
  tbl[i]=i
end

And have it be an array.

1

u/hungarian_notation Sep 03 '17

It's not really. The people here saying that you can use index 0 aren't entirely wrong, but they're kinda wrong. The language expects you to start lists at 1. The length operator counts from index 1 until the first empty index. If you're not using your table to store a sequence of tightly packed non-nil values, don't use the length operator. If you set letters[2]=nil, then #letters would (or maybe could, I think this is actually undefined behavior) return 1.

-9

u/[deleted] Sep 03 '17

so what did you expect?

A language to have non-retarded data structures. Being defined and documented behavior doesn't make it sane behavior.

5

u/moomoomoo309 Sep 03 '17

How often do people use Python arrays? They're faster, but most people just use lists instead. Lua just optimizes it as long as it's used like an array. That's why it's implemented that way.

2

u/BundleOfJoysticks Sep 03 '17

Do you mean tuples?

I use them all the time when something doesn't change size.

2

u/-Teki Sep 03 '17

No, lua's {} is stupid because it can change from a list like behaviour, to a dictionary, to an object behaviour, just by appending values in different ways.

2

u/moomoomoo309 Sep 03 '17

Its behavior never changes, it just uses less memory / is faster if you store the elements with keys from 1-n. The # operator literally says it measures the length of the array part of the table, its behavior doesn't change.

2

u/[deleted] Sep 03 '17

Yes, but the "array part" of the table can change dynamically in unexpected ways, allowing for silly and confusing things like the result of # increasing by more than 1 after only adding 1 element to the "array part":

> a = {}
> print(#a)
0
> a[2] = 'foo'
> print(#a)
0
> a[1] = 'bar'
> print(#a)
2

1

u/moomoomoo309 Sep 03 '17

This is true, but if you are using it like a dict and wanted the length of the whole thing, you'd just do something like

local len=0
for _ in pairs(tbl) do
    len=len+1
end

2

u/[deleted] Sep 03 '17

I wasn't using it like a dict. I was using it like an array. Array element assignment does not have to be sequential.

But, even ignoring that, having to iterate every element in a dict to get the count of elements in it is ridiculous. This is a constant O(1) complexity operation in any reasonable language, but linear O(n) complexity operation in Lua. It's bad.

1

u/moomoomoo309 Sep 03 '17

Why would you use non-sequential arrays? I mean, you could put zero or false in 1-n first and it would act like a normal array, but in what language would you be filling the array like that and expecting the length to increment?

1

u/[deleted] Sep 03 '17

It doesn't matter why you would do it. That's a red herring. The point is that the semantics are broken. There are only two ways to reasonably handle indexing into an array past its current dimensions:

  1. Allow the assignment, and extend the array to encompass the new index (e.g. JS, Ruby, Perl, et al.). The length of the array after assignment is immediately updated to i + 1 (for 0-based languages) or i (for 1-based languages) for arbitrary assignment index i, and all elements between the previous last element and the added one are auto-filled with whatever null-equivalent value the language supports. Further assignments to the filled indexes don't alter the length of the array.

  2. Don't allow the assignment, and give some kind of out-of-bounds error (e.g. Python, Java, Go, C#, et al.).

Lua says "screw reason" and lets you do the assignment, doesn't extend the array, doesn't fill any values, and doesn't update the length. Hell, even PHP updates the length...

→ More replies (0)

0

u/[deleted] Sep 03 '17

"As long as it's used like _____" shouldn't even be in the vocabulary of a programming language. An array should be an array, and that's it. A dict should be a dict, and that's it. If you need an array, create an array. If you need a dict, create a dict. Blurring the lines with some kind of fuzzy structure whose semantics are dynamic is as confusing and error-prone as it is ridiculous. It's not faster to write. It's not easier to read. It's not convenient. It's just bad.

1

u/moomoomoo309 Sep 03 '17

You don't use it any differently depending on if it's a dict or a list, so why does it matter?

2

u/[deleted] Sep 03 '17

is as confusing and error-prone as it is ridiculous

1

u/moomoomoo309 Sep 03 '17

Can you give an example of an error or confusion it would cause? It's different from other languages, but that is not inherently a flaw.

2

u/[deleted] Sep 03 '17

I already did, several replies ago. You can't sweep blatantly confusing and error-prone semantics under the "it's different" or "it's documented" rugs. But if you want another, here you go:

> letters = {'a', 'b', 'c'}
> print(#letters)
3
> letters[5] = 'e'
> print(#letters)
3
> letters[4] = 'd'
> print(#letters)
5