
Learn Lua in 15 Minutes (2013) - jakogut
http://tylerneylon.com/a/learn-lua/
======
VWWHFSfQ
Lua is a funky little language and runtime but it is absolutely perfect for
scripting in the domain that it was designed for: embedded.

I used to do a lot of game dev and it was incredible for testing different
interactions/movements etc at runtime just to get all the inputs right and
then implement in the game code. It has saved me probably 1,000s of man-hours
in my career. Nowadays I use it in NGINX for high-velocity requests that are
just backed by Redis. Lua, LuaJIT, and Redis have made me a lot of money in my
career. I'm forever indebted to their incredible creators and community.

~~~
AceJohnny2
How does it compare to TCL?

I understand that the fact of the matter is that Lua dominates, but I'm
curious if there are technical reasons or "right thing/right time" kind of
luck, after TCL's star dimmed.

~~~
corysama
Don't know much about TCL. But, if you are familiar with JavaScript, Lua ended
up being semantically a much cleaner, simpler JS without all the WOT?? and
committees to please.

It is small, fast, simple, powerful and easy to embed.

It's also very easy to contain in that the standard library is small and most
of that can be removed easily. Isolating Lua's allocation arena and
controlling Lua's GC is pretty easy.

~~~
hajile
All the little operators that make things nice like += are missing. Their
object implementation is meta-tables, so every single codebase does stuff in
fundamentally different paradigms. Operator overloading in such a dynamic
language makes things extremely hard to understand. While `"0" == 0` is false,
the language will still sloppily convert between floats and ints and will also
convert strings to numbers in other places. Worst of all, array indexes are
base 1.

~~~
hombre_fatal
Lua manages to surprise even Javascript developers with how bad its standard
library is.

------
tylerneylon
Author here. I'm delighted to see this is still interesting / useful to folks.
In case anyone is curious, here's a short origin story for this tutorial.

I learned Lua for a game jam - during the course of the game jam itself. Like
many, I found it a bit weird at first. 1-indexing feels wonky (you get used to
it, though); now that I use Python all day, I would miss syntax shortcuts like
list comprehensions going back to Lua.

When I was a kid, I learned C from a little pocket reference book while
building little self-motivated projects. I still think this is the best way to
learn - have a project in mind, and just enough fundamental examples to get
you there.

I threw together the Lua tutorial mostly in one sitting, spending some time to
edit, and going back and forth on how to keep the whole thing runnable while
still explaining how external modules worked. Shortly after publication, Adam
Bard contacted me with his idea for learnxinyminutes.com. I said great idea
and ok'd the inclusion of the tutorial on Adam's site. Adam invited others to
write their own similar-style tutorials, and his site grew up quickly.

Lua itself is a truly beautiful language, though I understand this is not a
popular opinion. It's beautiful because you can learn the entire language,
through and through, quickly. Memory and comprehension of the language itself
get out of the way, so the only hard problems left are your decisions about
what to do with the language. (You ask "How will I?" vs "How can I?") Instead
of being feature rich through added features, it's feature rich through good
language design. I expect some will disagree with me, but as one comparison
point, consider the complete syntax of Lua:

[https://www.lua.org/manual/5.1/manual.html#8](https://www.lua.org/manual/5.1/manual.html#8)

versus, say, Chapters 11-14 of the ECMAScript spec:

[https://tc39.es/ecma262/#sec-ecmascript-language-lexical-
gra...](https://tc39.es/ecma262/#sec-ecmascript-language-lexical-grammar)

It's not apples-to-apples - the ECMAScript spec is spelling things out in
detail, while the Lua section is an overview - but it's still a thought-
provoking contrast.

~~~
saagarjha
> Lua itself is a truly beautiful language, though I understand this is not a
> popular opinion.

I think most people who actually _use_ Lua come away marveling at its elegance
and simplicity. The ones that don’t get stuck at “arrays start at 1?! What a
garbage language!” In fact, this is a fairly good way to tell if someone has a
well-reasoned opinion about Lua ;) (I will put up Go as a counter-example:
“hurr durr no generics” is a meme complaint but it remains one once you
actually use the language…)

~~~
CyberDildonics
I've used julia and lua and tend to think if people complain about indexes
starting at 1 instead of C's offsets starting at 0, they can't be working on
anything too difficult, because it just isn't a big issue in the grand scheme
of things.

------
fosk
Lua + LuaJIT has been a great combination when it comes to Kong[1] which I
believe is the largest open source OpenResty application. Not only we can
leverage the underlying NGINX networking primitives for fast non blocking IO,
we can also hook into each request and response via Lua to provide APIM
functionality and run the whole thing on the piece of art that is LuaJIT[2]
created by the legendary Mike Pall.

I have to admit it does have its quirks (Lua lists have a base index of 1
instead of 0), but so far it's been phenomenal. As far as I know, Cloudflare
is also leveraging the same stack (OpenResty) for their global CDN.

Disclaimer: I am a Kong contributor.

[1] - [https://github.com/Kong/kong](https://github.com/Kong/kong)

[2] - [https://luajit.org/](https://luajit.org/)

------
samatman
I grant that it would have gone over 15 minutes, but it's a pity that such a
pithy summary doesn't cover coroutines or environments at all.

Coroutines are what elevate Lua beyond "a better Javascript" into something
truly great, while first-class environments, although something you won't
reach for often, allow for some things which just aren't possible any other
way.

------
chx
OpenResty embeds Lua in nginx, the results are out-of-this-world speed and
power. I know OpenResty can be used for complex projects but I always used it
for just that little extra simple configuration can't do.

------
valgaze
I've only encountered Lua in a game context

The first few releases of the "Monkey Island" series from LucasArts head a
grimy pirate-themed tavern called the "S.C.U.M.M. Bar" (for Script Creation
Utility for Maniac Mansion).

When they transitioned to a "3d" experience and switched engines/tooling, the
SCUMM Bar was replaced with tiki-style called "Lua Bar":
[https://monkeyisland.fandom.com/wiki/Scumm_Bar](https://monkeyisland.fandom.com/wiki/Scumm_Bar)

------
alectroem
feel the need to bring up the wonderful
[https://learnxinyminutes.com](https://learnxinyminutes.com), which has a ton
of references just like this for other languages. It actually looks like the
lua file there is the exact same one linked!

~~~
ahmedelgabri
It looks the same, because he is the author of that page

[https://github.com/adambard/learnxinyminutes-
docs/commit/9b4...](https://github.com/adambard/learnxinyminutes-
docs/commit/9b451ce38e4a721d8e5f9aee40483950654f2546#diff-7afcf759f7294538191b6c31dc1aec8cR3)

------
dang
Discussed at the time:
[https://news.ycombinator.com/item?id=5935320](https://news.ycombinator.com/item?id=5935320)

------
lattalayta

      -- Indices start at 1 !! SO CRAZY!
    

As of a few years ago, I use Lua regularly in my day job and this one still
baffles me. In Lua, the first element in a list is index 1, rather than 0.

~~~
mdorazio
In contrast, indices starting at 0 always baffled me in other languages. In
pretty much any real-world scenario, you always start at 1 (it's literally
"the first element" as in "1st"). It always seemed to me like language
developers for some odd reason decided to apply spatial coordinates to arrays.

~~~
Pelic4n
[https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html)

~~~
ufo
This always seems to pop up in these discussions but I don't think I am
terribly convinced by his argument. For example, he doesn't mention that
option (c) is better than option (a) if you need to iterate backwards. In that
case option (a) becomes option (b), which everyone can agree objectively sucks
:)

~~~
pansa2
Yes! Reverse ranges in Python are awkward for this reason. The reverse of
`range(0, 5)` (i.e. [0, 1, 2, 3, 4]) isn’t `range(5, 0, -1)`, it’s `range(4,
-1, -1)`.

I think it would be better if the end-points were “low” and “high” rather than
“start” and “stop”, so the reverse of `range(0, 5)` would simply be `range(0,
5, -1)`.

~~~
bad-apple
I think Rust has another good alternative to this, although it might be
cheating a little: you can just write (lo..hi).rev(). That way, the common
case of going forwards is nice, and the case of going backward isn't much
worse.

------
juancampa
As someone who works with lua/nginx seeing a variable declared without `local`
makes me immediately nervous as it's creating a global. Maybe start with a
`local`?

------
rkoten
"Read a compilation of Lua basics in 15 minutes"

~~~
fiblye
If you've got decent programming experience, that's all it takes.

I started learning Lua and began working on a game within an hour without
needing to reference language documentation much at all. It's a language
without many secrets.

------
anvemaha
Can anyone recommend IDEs for Lua? I've used Visual Studio Community for C#
and Eclipse for Java and been really happy with them. Mainly looking for code
auto-completion and debugger. Right now I'm doing a small game with C# and
Otter2d, but Lua with löve2d seems interesting.

~~~
sigzero
ZeroBrane Studio

[https://studio.zerobrane.com/](https://studio.zerobrane.com/)

------
spicyramen
I used Lua back in 2012 to perform call processing for SIP messages. It was
one of the first interprovider solutions for video calls. It was our best
option over TCL/JS to provide fast processing of text, eventually we allowed
users to enable it in Cisco Callmanager and SBCs.

------
runxel
Great! My interested in Lua was actually sparked by knowing you could write
pandoc plugins with it.

------
gmac
Perfect. Just started using IceSL
([https://icesl.loria.fr/](https://icesl.loria.fr/)) as an alternative to
OpenSCAD for designing things to 3D print, and its scripting is all Lua-based.

------
tomcam
Thanks for the post! It greatly improved my understanding of Lua, which I've
followed but never used in a project. I'd love to see more tutorials like
this, which assume the reader is a programmer but not in that particular
language.

------
ed25519FUUU
I really dislike that many languages still allow you to do strings with single
_or_ double quotes. Why not just choose one and stick with it? It’s another
pedantic thing that allow people argue about the “best way”

~~~
Ao7bei3s
Because it lets you use single quotes ("isn't") or double quotes ('error:
"%s"') without escaping ('isn\'t', "error: \"%s\"").

A rare case where readability and consistency may _not_ be the same. (Let's
not argue about this, it's subjective. I'm aware of the obvious counter- and
counter-counter-arguments, such as syntax hilighting and greppability.)

~~~
ed25519FUUU
The problem is that a single quote will often times appear in strings as well,
and sometimes (like in the case of bash) can't even be escaped. So you end up
requiring double quotes either way.

Why not just settle permanently on double quotes? This is what Go and other
languages did and I think it's a good idea.

~~~
VWWHFSfQ
> This is what Go and other languages did and I think it's a good idea.

Well Lua is about 30 years old and Go is, what, 10 years old? I would expect a
language that is barely a decade old to improve on past ideas and mistakes.

------
chadlavi
If this guy's here: your ssl cert is expired.

EDIT: oh never mind, it's just borked? HN hug of death?

------
ulisesrmzroche
Hah. Anyone else grew up making Wow mods? Little did I know how much I’d miss
that

------
Vaslo
I always wanted to learn this to make World Of Warcraft online addins!

------
rsecora
Luajit + sol2. It rocks.

------
kristopolous
4 places I use lua: mpv, notion (window manager), sysdig, gimp

------
Traster
Well that's the first language where I've responded negatively to the comment
syntax - and I've worked with tcl.

Also, seriously? Double is more than enough for integers? No. No. No. I'm not
going to attack every single line of this article, so lets just say I was
triggered.

My daily use languages are Verilog, Python and Tcl.

Edit: I'll actually backtrack to say that this post does a great job of
demonstrating things that are non-obvious.

~~~
jolmg
What's wrong with the comment syntax?

> Also, seriously? Double is more than enough for integers?

Not saying that I like it, but it's the same as Javascript. If I recall right,
Lua is heavily influenced by JS.

~~~
SanderSantema
I don’t think it’s influenced by js:
[http://www.lua.org/doc/hopl.pdf](http://www.lua.org/doc/hopl.pdf)

~~~
jolmg
Must have been just the impression I got. When I read about it many years ago,
I thought it was very much like JS except with a different syntax. I mean,
besides the number thing, it uses prototypes for inheritance, and its "tables"
are very much like JS objects in the way of how o.x == o["x"] for example, or
how arrays are basically built on them (though augmented with a little syntax-
sugar). I don't know of other languages that share these characteristics.

~~~
colonwqbang
Lua is older than Javascript so if anything it is Javascript which took
inspiration from Lua.

------
gpvos
More like 30 minutes, but okay

