20 ms·
Learn Lua in 15 Minutes (2013)
- dang 6y agoDiscussed at the time: https://news.ycombinator.com/item?id=5935320 https://news.ycombinator.com/item?id=5935320
- rkoten 6y ago"Read a compilation of Lua basics in 15 minutes"
- chx 6y ago"Read a compilation of Lua basics in 15 minutes in sensible organizaton with helpful explanations" FTFY
- fiblye 6y agoIf 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.
- Traster 6y agoWell 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.
- haberman 6y agoI think this information is obsolete. Lua supports integers (64 bits, by default) as of 5.3.
- jolmg 6y agoWhat'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.
- VWWHFSfQ 6y agoLua predates JavaScript by several years. Maybe you meant that JavaScript was influenced by Lua?
- jolmg 6y agoHuh. You're right. Though not several years apart, Lua appeared in 1993 and JS in 1995. I wonder if there's a similar HOPL paper for JS that could confirm Lua's influence.
- SanderSantema 6y agoI don’t think it’s influenced by js: http://www.lua.org/doc/hopl.pdf http://www.lua.org/doc/hopl.pdf
- jolmg 6y agoMust 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 6y agoLua is older than Javascript so if anything it is Javascript which took inspiration from Lua.
- alwillis 6y ago
- tdeck 6y agoBoth VHDL and SQL use -- for comments, so I'm kinda surprised that this us the first time you've seen this style.
- schwartzworld 6y agoForth uses parentheses.
- catalogia 6y agoVimscript has the worst comment syntax, hands down. Comments start with the " character, except when they cannot (see :h map-comments)
- foobiekr 6y agothe "use a float for integers" thing is pretty common for scripting languages. I am sorry for your suffering, btw. I, too, have used Tcl.
- chadlavi 6y agoIf this guy's here: your ssl cert is expired. EDIT: oh never mind, it's just borked? HN hug of death?
- alectroem 6y agofeel 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 6y agoIt looks the same, because he is the author of that page https://github.com/adambard/learnxinyminutes-docs/commit/9b451ce38e4a721d8e5f9aee40483950654f2546#diff-7afcf759f7294538191b6c31dc1aec8cR3 https://github.com/adambard/learnxinyminutes-docs/commit/9b4...
- VWWHFSfQ 6y agoLua 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.
- arthurcolle 6y ago> Nowadays I use it in NGINX for high-velocity requests that are just backed by Redis. Can you talk more about this? Sounds like some dark magic
- leetrout 6y agoNot the OP but I used openresty (fork of nginx) to cache POST requests to Redis. It’s lightning fast. 16,000+ requests per second served from the cache.
- ethanpil 6y agoCan you point me in the right direction for this type of implementation?
- leetrout 6y agoThis came up from a quick google search https://blog.cloud66.com/supercharging-nginx-with-lua-part-3/ https://blog.cloud66.com/supercharging-nginx-with-lua-part-3...
- anonymoushn 6y agoAt one company that served a lot of requests from an Openresty cache, we used nginx's cache for caching full HTTP responses and lua-resty-mlcache for caching other things. lua-resty-mlcache requires one to provide a callback to get the data as a string, then a way to deserialize the string, because the l2 cache in nginx's shared memory zone will store the item as a string. We used msgpack for this part.
- gpvos 6y agoMore like 30 minutes, but okay
- samatman 6y agoI 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.
- rsecora 6y agoLuajit + sol2. It rocks.
- ed25519FUUU 6y agoI 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”
- runxel 6y agoFun thing: It might even help. I actually like this possibility since it gives you more meaning: E.g. I would always tag localized strings by encapsulating them differently. It's great!
- Ao7bei3s 6y agoBecause 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 6y agoThe 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.
- dragonwriter 6y ago> The problem is that a single quote will often times appear in strings as well, but they can't be escaped While having choice of quotes reduces the need for escaping, most languages that provide it also still support escaping. E.g., Ruby has double-quoted strings with escaping, single-quotes without, %Q(...) with, %q(...) without, and two different heredoc forms, with and without escaping (the forms that allow escaping also support interpolation).
- VWWHFSfQ 6y ago
- runxel 6y agoGreat! My interested in Lua was actually sparked by knowing you could write pandoc plugins with it.
- chx 6y agoOpenResty 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.
- Vaslo 6y agoI always wanted to learn this to make World Of Warcraft online addins!
- lattalayta 6y ago-- 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.
- corysama 6y agoLua was originally created to be used by a group of FORTRAN programmers. Lua starts with 1 because FORTRAN starts with 1. FORTRAN starts with 1 because Math starts with 1 (Sum of f(Xi) for i in 1 to N)
- tzs 6y agoSometimes math starts with 1 and sometimes with 0. I was once implementing some math algorithm whose description had various subscripted variables. Some ran from 0 to whatever, and some ran from 1 to whatever. I was implementing in C++, with each of these variables becoming an array. I didn't want to write var[i-1] all over the place for the ones that ran from 1 in the book--it was a tricky algorithm and for maximum clarity I wanted my code to read as close to the book as possible. What I did was overload the () operator to make it a 1-based array access operator. I.e., var(i) == var[i-1]. That worked out reasonably, although it probably would have been better just to make the arrays for the 1-based variables one longer and just ignore var[0]. In other words, if var in the text has subscripts that run from 1 through 10, make the var array in C++ 11 long, and just use var[1] through var[10].
- lordgrenville 6y agoSame story with Julia. Julia is 1-indexed because MATLAB is 1-indexed, and MATLAB is 1-indexed because maths is 1-indexed.
- mdorazio 6y agoIn 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.
- fosk 6y agoLua + 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/
- spicyramen 6y agoI 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.
- tylerneylon 6y agoAuthor 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-grammar https://tc39.es/ecma262/#sec-ecmascript-language-lexical-gra... 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.
- jmiskovic 6y agoIt's a very well designed language. Authors call this 'orthogonal design'; features like iterators, meta-tables and coroutines are independent and you mix and match those to produce architecture you need. It takes some hard work to design things orthogonally, to identify and isolate usable abstractions, and then put them together in a way that has no rough edges. It seems Lua has pulled it off quite well, with very little accidental complexity. I'm amazed the language hasn't caught on more. It's perfect for education, as it is simple to learn basics and algorithm design. It's already used in some of most popular games (Roblox, Minecraft mods, WoW...) so there's established playground for children creations. Code flows like English language (which is bad for advanced users but perfect for kids). I'd say it's more suited for education than Python because it is simpler and more transparent. The things missing are tooling and documentation. There's only single popular IDE. The Programming in Lua book is awesome read (I'd recommend it even to non-Lua developers), but it's not a good language reference. The reference itself is quite useful but sparse on examples. I wish there was clojuredocs.org for Lua, community-sourced examples are amazing idea.
- valgaze 6y agoI'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
- tomcam 6y agoThanks 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.
- kristopolous 6y ago4 places I use lua: mpv, notion (window manager), sysdig, gimp
- anvemaha 6y agoCan 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.
- ulisesrmzroche 6y agoHah. Anyone else grew up making Wow mods? Little did I know how much I’d miss that
- gmac 6y agoPerfect. 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.
- juancampa 6y agoAs 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`?