New month, new language. So far in this series:
- May: Scala
- June: Lua
I think I do want to amend the original rules set up for this Language of the Month series. At first I though I can write a new project in any language I learn. That is probably too ambitious. So new rules: every month write a new project in the given language OR contribute to an open-source project in that language. This should take away most of the stress and add some social aspect as well. :)
I’m intrigued by this language because of it’s niche: scripting language within other software. How one sets out to do something like this? What are the requirements for the language, in terms of design, syntax and so on?
So far I was trying out some code snippets and example scripts. First thing to notice was that Lua is pretty darn quick to start. And pretty quick to run as well, though I haven’t used it for very heavy computation yet. E.g. the “get_all_factors” code at the Lua Crash Course (using a larger example number, 1029384756) is timed at ~7ms, wheres the same version on Python (the language that I probably know best and compare other languages to) runs ~10x slower. The snappiness of a scripting language is a surprisingly happy feeling. :)
Tables look very interesting, how the same thing can implement several data structures at once (it’s like Python’s dict, list and kinda-struct at the same time).
Quite intriguing that the Lua Virtual Machine is stack based, probably that’s the reason the language is so embeddable. A while back I was looking for a language with small resource (especially memory) requirements. I had a few suggestions (Dalvik, Forth) that were interesting, but maybe Lua is the one?
The “local” keyword (and the reasons for having it) seems to be a possible source of many harder to debug scenarios. Got to keep this in mind, again not being in Pythonland anymore.
Interesting how the “function” can be defined inline, so looks like there’s essentially no need for a special “lambda” keyword, at the expense of tying a few characters more…
The interpreter is quite well done (with it’s intelligent indentation probably even better then ipython, though would love to see more colours ^^)
As an exercise, here’s a version of FizzBuzz:
function fizzbuzz(num) --[[-- The usual FizzBuzz from Jeff Atwood Doing it a bit roundabout to try some Lua language features --]]-- local answer = "" if (num % 5) == 0 then answer = "Fizz" end if (num % 3) == 0 then answer = answer .. "Buzz" end if answer ~= "" then print(answer) else print(num) end end -- Run the thing for num=1,100,1 do fizzbuzz(num) end
This section will be updated gradually as I find more information.
(Edit: added the second part of this experiment.)
- A crash course on Lua, very short but poignant
- Tutorial directory on Lua Users Wiki
- Lua for Beginners
- Lua on Wikipedia, a quick overview
Articles and info
- Lua mentions on Hacker News (search results)
- Presentations on Slideshare (search results)
- World of Warcraft AI scripting with Lua (video)
Source of code
- Lua code on Github
- Lua search on Google Code Search, add a keyword, e.g. “function” to actually see something