Hacker News new | ask | show | jobs
by fifilura 743 days ago
I have not looked at Lua but this:

> I can usually accomplish whatever I need to without looking anything up"

And "everything is a table"

Makes it sound a lot like SQL.

Edit: I prefer replies to downvotes.

2 comments

Lua isn't anything like SQL. "Table"s in Lua are like maps/dictionaries/hash tables, not relational tables.
But still maybe the concepts for working with them (group by/ join/ aggregate) are similar?

There are lots of things you can do with a table as a general purpose data structure. And I was curious if this was a similarity?

When you say that tables are like dicts/maps, are they tables or are they dicts?

And I apologise for not knowing more about Lua. Maybe i should have known better than to ask about things I dont know.

> Maybe i should have known better than to ask about things I dont know.

It is fine to not know things. But when I look at your post, I see that you didn't ask any question. You only made statements that were based on very unfounded assumptions.

But to get back to what tables are: They are key-value pairs with both array- and hashmap-semantics. By convention, arrays start at 1 in Lua. They are very similar to dictionaries in Python , associative arrays in JavaScript and other key-value data structures in other programming languages. However, lua also uses them for storing variables in environments (some people would call environment "scopes") and there are special callbacks for missing variables and accessor functions when working with these tables.

> associative arrays in JavaScript

You're thinking PHP. Arrays and objects are discrete things in JavaScript. You can add random properties to arrays (since they are also objects) but don't expect them to behave well when doing things line loops, getting they length, etc.

JS objects are associative arrays with some fluff on top.
They really aren't. Tacking random properties onto an array doesn't affect the array's length, they won't show up when using `forEach`, map, reduce, filter, or any of the other built in functions.
Yeah I forgot a question mark.
So it seems like what they call tables are in fact maps or dicts. "Symbol tables".

I initially raised my eyebrows since what actual tables would have brought would be joins on non unique keys and multiple columns.

That would have unlocked more versatile "table programming" as in SQL, pandas, R and to some degree Excel.

The real benefit of "table programming" is that it is default distributable. All operations are atomic and immutable.

(I also lost some interest in Lua after -4 downvotes).

https://www.lua.org/pil/2.5.html

“The table type implements associative arrays.”

Simplified (and some hand waving): In Python associative arrays are called dict, in Lua they are called table.

It’s really much more like JavaScript (due to its prototypical nature), where “everything is an object” is effectively saying the same thing.
It's much more like JS without all the prototype stuff.
Aren't Lua metatables and JS prototypes almost the same mechanism?

I say almost, because I think metatables are more broad than JS prototypes - you can emulate JS prototypes behavior using Lua metatables[0], but I don't think the reverse can be done (maybe with some Proxy[1] hackery?).

[0] https://www.lua.org/pil/13.4.1.html

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Yes, you're right, `__index` does behave a lot like `__proto__`. I often forget that it is one of the few entries in the metatable that doesn't have to be a function.