
Learn Lua in 15 Minutes - tylerneylon
http://tylerneylon.com/a/learn-lua/
======
karpathy
This is awesome. I will take [a single huge list of self-explanatory example
snippets (with a few short comments here and there) for 95% of use cases] over
[detailed walls-of-text docs pages spread across a hierarchy of topics] any
day.

~~~
tylerneylon
Thanks. My goal was to write the tutorial I wish I had when I started. Your
description also matches my own preferred learning style.

~~~
StavrosK
Were you inspired by
[http://www.stavros.io/tutorials/python/](http://www.stavros.io/tutorials/python/),
by any chance?

------
dave1010uk
Along the same lines is [http://hyperpolyglot.org/](http://hyperpolyglot.org/)
(which has been posted here a few times). This is useful both for learning a
new language and for a quick reference for languages you're not so hot on.
It's also very interesting to compare similarities and how languages have
influenced each other.

It seems (from the comments here and my own experience) that many people
really like this style of learning. It would be great to see it applied in
more areas.

~~~
copx
Thanks for reminding me of Io.. not.

I was so happy with Lua and thought it was such an elegant and clean
language.. then I looked at the side-by-side comparison between Lua and Io on
the site you linked.. and now it seems clumsy and ugly.

Unfortunately a language is only as good as its implementation and tooling and
Io's implementation is "experimental" at best (and development is pretty much
dead) + the utter lack of tools and third-party support so I will have to
stick with Lua.. and try very hard to forget Io again.

------
silentbicycle
This is a good start, but there's also some deeper ideas in Lua. It has a good
implementation of coroutines, for example; if you're familiar with
continuations from Scheme, they fit most of the same uses. (They're like
generators from Python or fibers from Ruby, but with less edge cases.)

The C interop is also a Big Deal. Lua seems like a cute little language, like
a cleaner Javascript, but it's a LOT more useful if you're also proficient in
C. Also, Lua will run anywhere you have an ANSI C compiler and a modest amount
of space.

~~~
chalst
Coroutines are tricky, and you need to explain them, not just present a bit of
code. I think it is the right decision to omit them from a 15-minute intro.
The C API is way out of scope.

I would cover assert() statements and the common (exit_status, result) var
return, since that is so idiomatic. Maybe also loading chunks.

~~~
silentbicycle
I agree, they're tricky. I'm currently working on a blog post about them. :)

------
sharkbot
I'm going to be "that guy":

    
    
      -- These are the same:
      -- ...snip...
      local function g(x) return math.sin(x) end
      local g = function (x) return math.sin(x) end
    

According to the Lua Reference Manual [1], this example isn't correct. Rather,
this is the correct translation:

    
    
      local function g(x) return math.sin(x) end
      local g; g = function (x) return math.sin(x) end
    

From the manual: "This only makes a difference when the body of the function
contains references to f."

[1]
[http://www.lua.org/manual/5.2/manual.html#3.4.10](http://www.lua.org/manual/5.2/manual.html#3.4.10)

~~~
tylerneylon
You're right; I fixed that part.

~~~
fab13n
you also miss a couple of "local newObj" in constructors.

------
outworlder
This is awesome.

On that note, I don't understand all the javascript craze. Lua has been around
for quite some time, and it's small, with well-defined semantics, a nice set
of features, the interpreter is small and very fast, you can get even faster
with LuaJIT, etc. Browsers should have adopted that ages ago.

~~~
dagurp
Maybe when it supports unicode

~~~
anonymoushn
What do you want to do that you cannot do because of the lack of Unicode
support?

~~~
DrewAllyn
I'd imagine internationalized gamed would be a big one, although I'm not a
games programmer or a lua user myself.

~~~
anonymoushn
I had hoped that someone would mention what specific thing was missing,
because as far as I know it's trivial to do anything one would want to do when
making games already. That is, it is very easy to accept UTF-8 input, output
UTF-8, munge UTF-8 strings together prior to outputting them, compare UTF-8
strings for equality, and so on. If the game needs UTF-8-aware strlen (for
limiting the length of a field or alphabetizing things), it is like 10 lines
of code.

------
alecdbrooks
A logical next step might be to do do the Lua Missions (read: koans):
[https://github.com/kikito/lua_missions](https://github.com/kikito/lua_missions).

(Disclaimer: The missions look good, but I haven't personally tried them.)

~~~
jballanc
Actually, the Lua Missions are excellent! I highly recommend taking the time
to work through them. More than just a series of exercises (like most of the
foo-koans), there's actually a narrative and a larger lesson you can learn if
you work through the full problem set.

~~~
otikik
Hi, Lua Missions author here!

Thanks for the kind words. One word of warning though: I never got around to
make one mission about coroutines.

But I accept pull requests :)

------
daenz
Lua can be great. I've worked with it extensively on a personal project, where
I've embedded it in a C++ app. However, there are some things that you will
hate:

    
    
      * Trying to correctly do "inheritance"
      * Having to write all of your batteries for common ops
      * Array indices begin at 1!
      * Poor debugging support
    

Aside from those thing, Lua is great. It's crazy fast and easy to embed.

------
agscala
I prefer learning this way also, so I wrote something sort of similar for
Vimscript a while back. The # of people that ever need to use vimscript is
low, but it's out there for people to get their feet wet.

[http://andrewscala.com/vimscript/](http://andrewscala.com/vimscript/)

------
programminggeek
I would like to say that the easiest way for me to learn lua really fast was
to build games with Corona using lua.

Having a great tool that gives great feedback right away makes learning a
language more fun IMO.

~~~
potomak
Almost the same experience but with Love2D[1] instead of Corona.

After learning Lua I also suggest to take a look at MoonScript[2] a dynamic
scripting language that compiles into Lua, you can think about it as the
CoffeeScript of Lua.

[1] [http://love2d.org/](http://love2d.org/)

[2] [http://moonscript.org/](http://moonscript.org/)

~~~
Tloewald
Codea (ios) is also nice. My first forays into Lua were hacking WoW UI
components.

------
hanjos
Nice work!

One nitpick: local function declarations, like

    
    
        local function g(x) return g(x) end
    

are actually the same as writing

    
    
        local g;
        g = function (x) return g(x) end
    

. Without that first local g; statement, local functions wouldn't be able to
call themselves recursively.

~~~
tylerneylon
yep; fixed

------
piqufoh
I love this - if every language could have something like this written up it
would be amazing. It just about fits into my attention span!

~~~
chubot
Well, it helps a lot that Lua is by design a very small language :)

Python is actually much bigger nowadays -- I would challenge someone to come
up with something this simple that doesn't leave out a lot of stuff you will
encounter in real code.

The extra features ("bloat") do matter. I like the idea of Lua a lot, but when
I started programming in it, it unfortunately felt like a less capable Python
to me.

~~~
TillE
Lua and Javascript are quite similar in that way: nice little languages that
fall apart really quickly when you try building anything reasonably large or
complex.

I don't really understand the attraction, except in certain niches like Lua as
an embedded scripting engine, when you really need something that can be
trivially sandboxed.

~~~
landhar
> [...] except in certain niches like Lua as an embedded scripting engine,
> when you really need something that can be trivially sandboxed.

Isn't that exactly the reason JavaScript exists in the first place (i.e.: an
embedded scripting engine for browsers) ?

------
znowi
This is great! I wish every language had an intro like that :)

It was interesting to find resemblance of Lua's classes to Perl's blessing.
Always meant to look into the language and this tutorial made me do it.

I also learned about the LÖVE game engine, which I'm going to play with this
week. Thank you!

[http://love2d.org/](http://love2d.org/)

~~~
bananpermobil
If you're playing with love2d you might as well want to check out Zoetrope, I
found it very awesome. [http://libzoetrope.org/](http://libzoetrope.org/)

~~~
tripzilch
This looks interesting, can you explain the difference between Zoetrope and
LÖVE?

The former seems to be built on top of the latter, yes? Are there particular
things lacking in LÖVE that Zoetrope fills in? I couldn't find this info
quickly on their sites.

~~~
LeonidasXIV
Looks like it has collision detection whereas for LÖVE you'd use Hardon
Collider, has Tiled level loading whereas you'd need a plugin for LÖVE, etc.
Basically, lots of stuff that you'd include in your games anyway.

Looks nice and has better documentation than LÖVE. Thanks for posting it, I
didn't know about it before.

------
thyrsus
First, that was very useful; bookmarked.

That took me 30 minutes to read. I think I might feel like I've learned the
language after spending 8 hours working the examples a la "Learn Python the
Hard Way". Luckily, I'm not competing with the folks here that learned it
within 15 minutes ;-).

~~~
tylerneylon
The title is a bit optimistic :) That's how long it takes me to read as if I
were reading a novel, which isn't quite fair.

I wanted to express something like "hey, this guide is for programmers and is
surprisingly short and easy to consume considering it covers the large
majority of the language." The current title seemed a bit snazzier.

------
lukefreiler
I love this. I'd like/pay to have a consistent library of exactly this for
other languages.

~~~
digitalsushi
A scripting language rosetta stone.

Here's one

[http://www.lurklurk.org/rosetta.html](http://www.lurklurk.org/rosetta.html)

~~~
lukefreiler
Yes, this - but with more languages and in the format of this topic. Just a
really easy read.

------
Dylan16807
This is a neat little reference but I can't waste the opportunity.

I've seen people say they've never seen complaints about fonts being too big?
Well here I am, complaining that the fonts are so big this page is hard for me
to read without zooming out.

------
spacecowboy
That was a pretty cool crash course on Lua Tyler! Thanks for putting that
together and sharing it. After having had worked with numerous languages over
the years such as Fortran, Pascal, Ada, C, Java, C++, C#, Python and
Objective-C, I must say that working with Lua is my favorite. I'm thankful to
a buddy of mine for introducing it to me. My own personal experience has shown
me that working with Lua really allows you to focus on solving the problem
versus getting wrapped around the axle at the programming language level.
Thanks again

------
nathas
That was lovely. I always had a hard time wrapping my head around the exact
behavior of metatables when I messed with Lua a few years back. Thanks!

------
noonespecial
I learned to love lua when writing basic sysadmin scripts for OpenWRT based
systems. I had previously used perl for sysadmin stuff, but perl (even
microperl) took too much space on OpenWRT boxes with 4MB flash drives. Lua to
the rescue. Now I chafe when I have to go pack to perl.

I do wish someone had handed me this on the first day I realized I needed lua.

------
ErikRogneby
Maybe for the next Ludnum Dare...

nice write up comparison of lua based game engines:
[http://www.gamefromscratch.com/post/2012/09/21/Battle-of-
the...](http://www.gamefromscratch.com/post/2012/09/21/Battle-of-the-Lua-Game-
Engines-Corona-vs-Gideros-vs-Love-vs-Moai.aspx)

------
digitalsushi
Thanks! Lua is used all over the place in wireshark user scripts. This is
super useful.

------
mneary
Anyone else catch the Gauss reference[1] in the for loop section?

[1]
[http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes](http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes)

~~~
tylerneylon
You have a keen eye for subtle references! I didn't expect many people to get
that one :) There's another math anecdote reference hidden in there as well -
probably even more obscure than Karl's sum.

~~~
tekacs
Upon reading this comment had to skim the whole thing looking for more (hadn't
yet finished reading). No more maths references than tau found yet. :/

~~~
tylerneylon
Ramanujan and Hardy are two well-known mathematicians who collaborated in
Britain between 1914 and 1920. One day Hardy took taxi number 1729 to visit
Ramanujan, and remarked to him that he (Hardy) could think of nothing special
about the number (1729). Ramanujan replied that it was in fact the first
number expressible as the sum of two cubes in two different ways (1^3 + 12^3 =
9^3 + 10^3). The story has since become an anecdotal symbol of Ramanujan's
brilliant mind.

[http://en.wikipedia.org/wiki/1729_(number)](http://en.wikipedia.org/wiki/1729_\(number\))

That's a very obscure reference, though, so I apologize for sending anyone
looking based on my previous comment.

~~~
tekacs
Haha I'd heard this anecdote before, so it's my own fault for missing it. :)

Thanks for the great article and indeed fun references!

------
landhar
This is very cool, the only way I can see this could be improved upon is by
adding unit tests somehow (so that you can catch behavior changes when
upgrading to a newer version of lua).

~~~
spc476
I'm not sure it's really necessary---Lua 5.0 came out in 2003, 5.1 in 2006 and
5.2 in late 2011. It doesn't change that fast.

------
daGrevis
Lua is weird mix between JavaScript and Python. I kind of like it.

------
johnchristopher
Bonus point for Cloud Atlas reference :)

------
vishal0123
Just awesome. I wonder whether this type of example-based snippet exist for
other languages.

------
ufo
This would look much better with syntax highlighting.

------
jeltz
Having a font size of 25.33pt makes the website impossible to read without
zooming out 3 steps. I would be happy if people stopped using tiny and huge
default font sizes.

~~~
ricardobeat
You can zoom in/out as much as you'd like, as you already know, so there is no
need to force your personal taste on everyone else.

------
chii
very comprehensive and interesting! i wish theres one for other languages too.

------
dakimov
Great sheet.

    
    
      -- Variables are global by default.
      thisIsGlobal = 5  -- Camel case is common.
    

\-- Undefined variables return nil. \-- This is not an error: foo =
anUnknownVariable -- Now foo = nil.

\-- Only nil and false are falsy; 0 and '' are true!

Didn't read further. Bad language design. Must die.

~~~
anonymoushn
Could you tell us anything about why these things are bad?

Regarding the last point (the one that you cannot change), I'm not sure I
prefer the behavior of any other language. In Python, for instance, all of the
number-like things I tried were falsy iff they were 0, and I found that almost
any container is falsy iff it is empty, but Queues are always truthy. This is
a weird exception, and I would rather not have to remember it. Collections and
numeric types defined in external libraries can easily have the same problem.

~~~
dakimov
Global automatically declared variables is a well-known bad feature of some
non-modern languages that gives nothing but an opportunity to make hardly
detectable mistakes because of misprints.

Arbitrary nonsensical type casts must be prohibited. The
compiler/interpreter/IDE must infer types and help the programmer to catch
their mistakes as early as possible.

As a general rule of language design, if something looks unintentional or
ambiguous to a human, it should look so to a compiler/interpreter. The more
coherent the programming environment is to your conscience, the easier the
programming is.

~~~
anonymoushn
Thanks. I agree that Haskell is a great language, but this is a dynamically
typed scripting language. As such, perhaps it would be more useful to compare
it to languages that are actually comparable, like Ruby, Perl, your favorite
LISP, Javascript, or Python.

This language does not include any nonsensical type casts. You cannot add a
table to a string, take the arithmetic negation of a function, or index a
number. (Actually, if you wanted to you could write code that permits you to
do most of these things. For instance, I usually use some code that adds the
ability to index strings.) String concatenation and arithmetic addition are
different operators, so unlike in some prominent scripting languages you
cannot find a and b such that +(a+b) == 50005 and +(b+a) == 55.

It does, however, include some forms that allow you to branch based on whether
or not a value is a member of the set {nil, false}. I suppose that it would be
better for the compiler to be able to guarantee that the value was a member of
the set {true, false} and to yell at you rather than running your code if it
was not, but this is a fairly rare property across all programming languages.
I frequently use a programming language called Scala, and it pretends to
provide some typechecks, but in reality all reference types are silently
nullable, and all method calls are silently of the bottom type.

------
J_Darnley
That page could do with a little syntax highlighting, at least for the
comments. -- does not stand out.

~~~
dag11
There is and has been syntax highlighting for me. Weird that you don't see it.

------
nbouscal
Obligatory: Teach Yourself Programming in Ten Years —
[http://norvig.com/21-days.html](http://norvig.com/21-days.html)

