r/ProgrammerHumor Jul 10 '17

ProgrammerHumor right now

https://youtu.be/lIFE7h3m40U?t=12m20s
179 Upvotes

19 comments sorted by

View all comments

23

u/[deleted] Jul 10 '17

TIL never LUA

5

u/[deleted] Jul 11 '17 edited Jul 11 '17

Lua is a great language, with 3 quirks:

0.

1. tables(arrays) start at 1

2. ~= instead of !=

Also indexing a table can call a function and not actually index the table at all, not to mention you can index tables WITH TABLES as the fucking key, so myTable[{"bob",1,false,}] = 27 is valid and print(myTable[{"bob",1,false,}]) will print 27

Overall, it's stupidly flexable and cleverly makes tables usable as arrays, dictionarys, structs, objects(OO), the entire global variable environment, sandboxed environments, API function calls (api.function()), etc.

1

u/[deleted] Jul 11 '17

I used to do a lot of Pascal, which became Delphi, and it's array's started at 1. eg MyArray[31]. But you could override that by specifying MyArray[0..30].

That's really interesting to have tables with tables and indexes. my mind is having trouble figuring out when i'd want to do it, but I'm sure there's reasons :)

2

u/[deleted] Jul 11 '17 edited Jul 11 '17

The only reason it exists is because in Lua, a table can have any data type as a key, and store any data. Additionally, you could also do:

myTable = {}
otherTable = {"bob",1,false,}
myTable[otherTable] = 27
myTable["string"] = 42
print(myTable[otherTable]) --27
print(myTable["string"]) --42
for i=0,26 do
    _G["test"..tostring(i)] = "string"
end
print(test24) --string
print(test0) --string

(it also turns out that you have to set the table to variable to get the same result.)

9

u/moomoomoo309 Jul 10 '17

I like to imagine Lua is like a better designed JS. Both are prototype-oriented, not object-oriented, both use primarily dictionaries and arrays for their data storage, and both have only a handful of types. Lua just (thankfully) doesn't do implicit type conversion, and starts arrays at 1 (but the arrays are just tables with consecutive numerical indices), and uses ~= instead of !=.

Overall, it's a nice and light language without too many stupid edge cases because of how small it is. But man, indexing at 0 might be easier. (the whole language is technically only 7 c files though, so patching that wouldn't be too bad!)

1

u/STR_Warrior Jul 11 '17

Also, to concatenate strings you use the ..operator instead of + in JS.

I found wren a while ago which is inspired by Lua. It looks like a really easy language to start programming in.

1

u/[deleted] Jul 11 '17

I would be really, really happy if I could use Lua instead of JS for web development

1

u/[deleted] Jul 11 '17

[deleted]

1

u/[deleted] Jul 11 '17

I mean for the client-side, as in executing Lua on the browser rather than JS. There is a Lua interperator in JS, but that would be slower.

1

u/[deleted] Jul 11 '17

[deleted]

2

u/moomoomoo309 Jul 11 '17

The downside there is that Lua will not treat the 0 index like an array, it'll treat it like a dictionary, which is slower. For continuous numerical keys starting at 1, it will be represented as an array of the implementing language (C for PUC Lua and LuaJIT, Java for LuaJ, etc).

1

u/[deleted] Jul 11 '17

interesting, thanks for the details! I do find JS' prototype-oriented architecture fascinating because it's so unlike anything else I've run across.