this post was submitted on 24 Jan 2024
477 points (99.2% liked)

Technology

34125 readers
377 users here now

This is the official technology community of Lemmy.ml for all news related to creation and use of technology, and to facilitate civil, meaningful discussion around it.


Ask in DM before posting product reviews or ads. All such posts otherwise are subject to removal.


Rules:

1: All Lemmy rules apply

2: Do not post low effort posts

3: NEVER post naziped*gore stuff

4: Always post article URLs or their archived version URLs as sources, NOT screenshots. Help the blind users.

5: personal rants of Big Tech CEOs like Elon Musk are unwelcome (does not include posts about their companies affecting wide range of people)

6: no advertisement posts unless verified as legitimate and non-exploitative/non-consumerist

7: crypto related posts, unless essential, are disallowed

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] sugar_in_your_tea 10 points 7 months ago (2 children)

Yeah, Lua is really nice. It's also fast and small. In fact, "Programming in Lua" is imo one of the best programming language books available, up there with Kernighan and Richie's "The C Programming Language."

[–] [email protected] 4 points 7 months ago (2 children)

Yes, I was shocked at how small it is. I had no experience working with such limited resources going into this project. Our router had 32MB of storage. At one point I was looked into adding a python interpreter, and it was like 11MB. The Lua interpreter is like 250KB. Tiny!

Also, the ternary operator has the best syntax of any language I have ever used.

x = [condition] and [true value] or [false value]

No question marks or colons or anything weird. It's a logical extension of && and || after commands in bash using keywords since it is a verbose language. I wish every language had this syntax.

For contrast, python is:

x = [true value] if [condition] else [false value]

It just seems weird to me to have the condition in the middle.

[–] sugar_in_your_tea 3 points 7 months ago* (last edited 7 months ago) (1 children)

Yeah, I've always hated Python's ternary, and I use it every day at work. Though you can do the same in Python if you want:

x = [condition] and [true value] or [false value]

I consider that bad style because the dedicated syntax is preferred. You can also do similar in JS:

x = [condition] && [true value] || [false value]

The caveat in both (and Lua) is that you'll get the false value if the true value is falsey.

My favorite syntax is Rust:

x = if [condition] { [true value] } else { [false value] };

This preserves the flow you get with the ? :, allows [true value] to be falsey, and is idiomatic without having a lot of extra syntax.

My favorite thing about Lua is that tables separate numeric from string keys, so you can do this:

x = { metadata = ... }
x[1] = 3
x[2] = 4
print(#x) -- prints 2

This is really nice for representing something like an XML/HTML DOM, where numeric indices are child nodes, and string keys are attributes. Or you can store metadata about a list in the list itself (e.g. have a reference to the max value, min value, etc). It's just really nice to work with.

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago)

Haskell's if is pretty nice: if cond then truthy else falsy

[–] [email protected] 3 points 7 months ago

Lua is so friendly, I've run in to it in a few instances in different contexts. Currently have a "sound computer" shield for rpi that uses Lua to run community scripts, which turn the thing in to anything from synths to sequencers to loopers and some really esoteric stuff. Lua is so intuitive I'm able to tweak stuff and even add functionality to existing scripts without much effort.