DEV Community

Jeremy Woertink
Jeremy Woertink

Posted on • Edited on

5 reasons you should learn Lua

If you haven't heard of Lua, it's a really small programming language. Maybe you have heard of it, but never had a reason to learn it because your programming language of choice does everything you need it to. Well, here are a few reasons you should learn Lua anyway.

5.

Have you heard/used nginx? You can use mini Lua scripts inside of nginx with the openresty setup. This would allow you to do things like dynamically assign SSL certs through Let's Encrypt to different domains.

auto_ssl:set("allow_domain", function(domain)
  return ngx.re.match(domain, "^(mysite.com|yoursite.com)$", "ijo")
end)
Enter fullscreen mode Exit fullscreen mode

Use Apahce instead of nginx? Lua still has you covered.

4.

Another popular tool that is used in the web is Redis. If you use redis to store lists of timeline event IDs or something, you may need to do some crazy sorting on this data. Writing RPOPLPUSH statements in redis can get a little crazy, and sometimes the language you choose may not be as fast as you'd like. For example, if you're using ruby, there's a gem called Wolverine. You write a Lua script that does all the crazy sorting you need, and then pass that data back to your rails app. This gives you a massive performance boost.

3.

If you need lots of performance for your web app, but you don't have the capacity to re-write in something different, then you may employ heavy cache in to your app. One way to do this is to throw Varnish in front of your app. Varnish will handle all the cache, and only proxy back to your app when the cache is expired.

Varnish uses these vcl scripts which can get a little confusing. With a little lua magic you can write some nice programmatic cacheing. Maybe cache different lengths based on the domain in a multi-tenant situation?

2.

Are you big in to playing video games? Maybe you've heard of some of these?

  • Angry Birds
  • Civilization V
  • Far Cry
  • Garry's Mod
  • Heroes of Might and Magic V
  • L.A. Noire
  • Mafia II
  • Roblox
  • Saints Row 2
  • SimCity 4
  • Star Wars Battlefront
  • Stepmania
  • Warhammer
  • World of Warcraft

All of these games use Lua in some capacity, and there's MANY more. Most games or game engines will be written in C++ or C#, but using Lua on the front end for scripting allows for rapid development of these games. No need to re-compile each time. Compile your engine, and have it run the lua scripts. You can get started with Love2d or defold

1.

Because Lua is fun to write! If you don't know it, it wouldn't hurt to learn a new language. If you already know how to program, picking up lua should be pretty quick. Read over this 15 min of lua, and you will have a decent understanding of the syntax. At that point it's just a matter of digging in!

Top comments (33)

Collapse
 
jld3103 profile image
jld3103

Minus: Arrays start at 1 ;)

Collapse
 
jwoertink profile image
Jeremy Woertink

Ha! Yeah, there's a few weird things in Lua, but once you learn that, then it opens your mind to the fact that not all languages are created equal. Now, if we were to not use a language because it had something weird, we'd all have dropped javascript by now :p lol

Collapse
 
ricky11 profile image
Rishi U

whaaaaa...... mind blown.

Collapse
 
ondrejs profile image
Ondrej • Edited

Another reason - Nmap is written in Lua; customize your Nmap!

Collapse
 
jwoertink profile image
Jeremy Woertink

That's true. Look like a movie hacker with a little lua :D

Collapse
 
ondrejs profile image
Ondrej

Yeah, why not? :) Btw I was joking a little - it's great to customize your nmap, but Lua is also good general scripting language. Sadly, not much used in comparison with Python/Ruby.

Collapse
 
tobiassn profile image
Tobias SN

I’ll admit that Lua was actually my first programming language.

Collapse
 
renatosuero profile image
Renato Suero • Edited

Great post Jeremy,

For me as Brazilian is an honor see people write about Lua <3
I'd like to recommend exercism.io, I think that it can be useful to improve the learning.

Collapse
 
booterror profile image
Vighnesh SK

Minecraft with computercraft mod has computers and robots(turtle) which are programmed in Lua to do things like dig a strip mine or control machinery with redstone.

Collapse
 
jessekphillips profile image
Jesse Phillips

I don't like Lua as a programming language. It is nice to use to extend a language. I like using LuaD to add runtime configuration and logic. The simplicity of the structures and something else I don't know haw to describe is really cool.

Collapse
 
jwoertink profile image
Jeremy Woertink

It's definitely not a primary language for me. Not one of those "do everything" tools, but it has it's place. Almost like that weird screw driver you use on faucets. Most people never need one, but the second you do, it's a nice thing to have.

Collapse
 
samyeyo profile image
Sam

I don't agree. Lua as a programming language has all it takes : garbage collection, iterators, lambdas, speed, simple but powerful types...
It just lacks a better standard library to be able to "do more".

Thread Thread
 
jessekphillips profile image
Jesse Phillips

Lua is definitely not limited as a language. I find that it's focus on being a configuration language to create some not so desirable synthetic choices.

I would probably much rather write a parser for Lua than yaml though.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I have for the 3rd time looked at Lua but this time with an open mind. I'm interested in webdev. I see LuaRocks and some JavaScript or C like familiarity but also some quirks. I am very excited to try writing something, but I'm not sure what.

Collapse
 
jwoertink profile image
Jeremy Woertink

Making a game love2d.org/wiki/Getting_Started is fun way to start. Or just start by making a small console app and getting that to work. That will help you feel out the flow of lua.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Very nice idea! Segway: what is the dotnotation in this function declaration function love.load().

Thread Thread
 
jwoertink profile image
Jeremy Woertink

Lua has 2 different type of notations. Since it's not an object oriented language, you can think of . like a class method, and : like an instance method. So love.load() vs love:load(). Lua basically determines what self is based on how you call the functions (if that makes sense)..

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

It's been a little while since I commented about wanting to learn Lua. I lovit ha, see what I did... Anyway I'l start with what I dislike.

  1. ~= is not !=
  2. Braces are not a thing, I write in braced languages and I find this a little jarring.
  3. require is not node js require

What I love, Lua is tiny, fast and I can change it as I like. I'm currently hijacking V8 to launch the majority of my node js web app in Lua. That includes using the c binding directly from node with node require and a js proxy.

Because why not, it's fun.

Collapse
 
jwoertink profile image
Jeremy Woertink

Awesome! Yeah, I agree with the ~= thing. That throws me off all the time especially since my main languages (Ruby & Crystal) use =~ for regex matching lol. But I don't work in curly brace languages, so for me I love that it doesn't have those :D

Sounds like you found a great use for using Lua! Thanks for sharing.

Collapse
 
tadman profile image
Scott Tadman
  1. It takes very little time! There's not too much to Lua so you can pick it up quickly.
Collapse
 
wfelix profile image
W. Felix

Recently I wrote a script to authenticate users against Redis with Lua on Nginx. The result was great in simplicity and performance. The code is available on my Github github.com/waldyrfelix/nginx_lua_auth