Learn Lua in 15 Minutes
tylerneylon.com
tylerneylon.com
That said, Lua is quite the special case, as it does not require much time to get up and running with it. This is one of the reasons why it is the scripting language of Redis by the way (Antirez even wrote Total time require to master it enough? 5 minutes.[1] regarding building scripts for Redis).
[1]:http://oldblog.antirez.com/post/scripting-branch-released.ht...
This method, however, is PERFECT for anyone who already knows how to program. I love it, and actually came here to say that I wish everything everywhere had a page like this on the web.
The brass tax, get down to business, of ANY topic in 15 minutes.
Or the brass tacks, even:
http://www.phrases.org.uk/meanings/get-down-to-brass-tacks.h...
Sadly, English isn't as logical as Lua.
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.
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.
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.
I would cover assert() statements and the common (exit_status, result) var return, since that is so idiomatic. Maybe also loading chunks.
-- 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." > local g=function() print(type(g)) end g()
nil
> local function g() print(type(g)) end g()
functionOn 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.
I also found it considerably easier to integrate than something like V8, and it appears to be easier to use for non-programmers.
It definitely deserves to be more popular.
* Lua has proper lexical scoping instead of the confusing "function scoping" of Javascript (so there is no need to use the IIFEs hack).
* Lua has a Python-style lexicaly scoped self instead of the error prone dynamically scoped "this". No need to use `var taht = this` if you have nested functions.
* The metatable system is much more flexible than the simple JS prototypal inheritance system (you can sort of implement "method missing" instead of being forced to only delegate to a concrete table.
* You can use anything as a Lua table key. In JS all keys are converted to strigns. Lua also supports weak references.
* Coroutines! Its sort of like ES6 generators and is super awesome. Async programming is infinitely more pleasant in Lua than it currently is in JS because you don't need to convert code to continuation passing style.
Well, I wasn't advocating Lua as a good language for non-programmers to write programs, but for scripting. When I make games/applications scriptable, it's closer to configurating than programming, and Lua's table constructors allow for great declarative DSLs.
Someone still has to actually do the work. Then it has to get adoption. No one has bothered to do either.
EDIT: Also Fortran.
Not POSIX.
Plain old C.
Until C11, unicode didn't exist in C. And it will be a long time before C11 is the default. Hell, there's lots of places where C99 is the exotic interloper.
(Disclaimer: The missions look good, but I haven't personally tried them.)
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 :)
* 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.Having a great tool that gives great feedback right away makes learning a language more fun IMO.
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.
There were the expected "WTF?" moments but having something running and doing something practical is a great motivator.
Since I became a Lua programmer, I really can't stand any other languages, and I've rapidly switched to it for my scripting needs. Tables are just soooo lovely. ;)
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.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.
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.
Isn't that exactly the reason JavaScript exists in the first place (i.e.: an embedded scripting engine for browsers) ?
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!
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.
Looks nice and has better documentation than LÖVE. Thanks for posting it, I didn't know about it before.
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 ;-).
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.
Here's one
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.
I do wish someone had handed me this on the first day I realized I needed lua.
nice write up comparison of lua based game engines: http://www.gamefromscratch.com/post/2012/09/21/Battle-of-the...
[1] http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes
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.
Thanks for the great article and indeed fun references!
(so, yes. :P)
-- 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.
The last one is awesome.
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.
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.
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.