11 ms·
What makes Lua tick?
- NelsonMinar 13y agoMy favorite thing about Lua is that the language is so simple, once you figure out one way to do something it's probably the only way. Like, say, summing up all the numbers in a list of strings. In Perl there are many ways, in Python there are many ways but one is right, and in Lua there's probably only one way so once you figure it out, you're done. Simplicity is a nice thing in an embedded tool language.
- eieio 13y agoAs a huge Python fan, comments like this(and many others) have made me want to check out Lua. Is there a canonical Lua introduction out there? I'd love to give it a try.
- mintplant 13y agoThis was posted here a while back: http://learnxinyminutes.com/docs/lua/ http://learnxinyminutes.com/docs/lua/
- gebe 13y agoThe PIL, http://www.lua.org/pil/ http://www.lua.org/pil/, is as canonical as it gets, written by the creator of Lua.
- oblique63 13y agoAlternatively, there's also http://luatut.com/ http://luatut.com/ for a quick useful intro
- Derbasti 13y agoReally an awesome book! It not only teaches you about the programming language itself, but also how to use it well and idiomatically. Also, it is very focused and compact, which is a great thing in a programming book--although it makes for kind of a dry read.
- Elv13 13y agohttp://tylerneylon.com/a/learn-lua/ http://tylerneylon.com/a/learn-lua/ as simple as it can get. Lua is a very small (in term a amount of syntax) language.
- phaedryx 13y agoI first started playing around with Lua here: http://repl.it/languages/Lua http://repl.it/languages/Lua
- joelg236 13y agoTry http://learnxinyminutes.com/docs/lua/ http://learnxinyminutes.com/docs/lua/
- speeder 13y agoIt is not like that... Most things can be done in many ways, some of those in excessively many ways... It just happen that for most projects any of.them are "right" except the most esoteric ones... You can fully pull some crazy "clever" stuff, but requires effort, usually the first idea in your head is already good enough.
- sheng 13y agowould you mind to state some of the so many ways of summing up all the numbers in a list of strings for python? And give a counter example for lua?
- plorkyeran 13y agoOff the top of my head: list = ["1", "2", "3"] sum1 = sum(int(num) for num in list) sum2 = reduce(map(list, int), operator.add) sum3 = 0 for num in list: sum3 += int(num) Lua only has the third of these three options. I don't really consider this a particularly compelling advantage as a user.
- oblique63 13y agoNo, but I think a lot of python users consider this a particularly compelling advantage as readers. At least, I know I sure spend a lot more time reading code than I do writing code; so I appreciate any language that tries to optimize for that use-case and helps me minimize my cognitive overhead.
- zeckalpha 13y agoThe second would work, if the functions were defined correctly.
- anonymoushn 13y agofunction curry(f,x) return function(...) return f(x, ...) end end function reduce(f,t,r) r = r or t[1] for i=2,#t do r = f(r,t[i]) end return r end addf = function(a,b) return a+b end sum = curry(reduce, addf) t = {"1", "2", "12"} print(reduce(addf, t)) -- 15 print(sum(t)) -- 15 res = 0 for i=1,#t do res = res + t[i] end print(res) -- 15 res = 0 for k,v in ipairs(t) do res = res + v end print(res) -- 15
- Kaali 13y agoLua is a really simple language, and is quite malleable to different use cases not included in the language per se. Usually in my book, it is an advantage, for example how many Lisps can implement new features that seem like language features, but aren't. But this simplicity can have a downside, as there might not be canonical ways of doing things that users are accustomed to. For example, object orientation. It could be a hard sell to someone coming from an object-oriented language, when you have to understand, and choose between multiple different styles of object-orientation and implement them consistently in your project, or use one of the many libraries made just for this. For example, see http://lua-users.org/wiki/ObjectOrientedProgramming http://lua-users.org/wiki/ObjectOrientedProgramming Lua really is simple, but it not always easy.
- anonymoushn 13y agoIt is a bit nice to not be limited by any particular OO system. I don't get much use out of this, since my current class.lua is about 14 lines and doesn't do inheritance, but it would be easy to add in the future. It's probably a bit easier for people with js experience to get used to OO in Lua. Coming from js, most of the language just seems like a copy with more expressive power and fewer mistakes.
- swah 13y ago...and it involves table-based programming.
- thinkpad20 13y agoBig fan of Lua. I have only poked around at the language, and never really done anything significant with it, but I love how simple and powerful it is. Every time I break out the lua programming book I wish I were using that at work instead of python. I don't agree with every choice they've made, but it's a great language for sure.
- PavlovsCat 13y agoSame here: I once wrote a bunch of simple plugins for Renoise in LUA which was my first (and so far last) contact with it.. I forget everything about it since then, other than that it was very fun! I spent time learning the Renoise API and figuring out UI stuff, LUA itself just kinda worked. It was like jumping into cold water and finding it rather warm and pleasant.
- jacques_chester 13y agoJust FYI, it's "Lua", not "LUA". It's a word, not an acronym.
- scardine 13y agoLua means moon in Portuguese (the native language of the designers).
- jzwinck 13y agoWhat do you dislike about Python for work?
- copx 13y agoYears ago I read a Lua tutorial but I didn't like the language much. Nowadays I am working on a large 99% Lua code base and I love the language! Lua has a small set of building blocks and only if you try to write something non-trivial in Lua you realize how incredibly well these blocks fit together. It's funny, Lua is the opposite of Go for me there. I was excited about Go after reading the tutorial, but then I tried to seriously use it the experience was underwhelming.
- sker 13y agoThe thing is indeed fast. Looking at these benchmarks[1], it always makes it to the top 10 on the tests where it appears. Edit: Apparently, OpenResty[2] is largely responsible for the impressive performance in those benchmarks, and Lua may be secondary. 1. http://www.techempower.com/benchmarks/#section=data-r6&hw=i7&test=db http://www.techempower.com/benchmarks/#section=data-r6&hw=i7... 2. http://openresty.org/ http://openresty.org/
- bhauer 13y agoLua is quick. OpenResty and nginx are certainly providing a very high performance ceiling, but the test is indeed Lua code [1]. Many test implementations for other dynamic languages are similarly provided a high ceiling by nginx but then fail to deliver on that potential. [1] https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/openresty/app.lua https://github.com/TechEmpower/FrameworkBenchmarks/blob/mast...
- rsiqueira 13y agoI love the fact that Redis database can run Lua scripts, allowing very sofisticated queries.
- oakaz 13y agoI wrote some Lua couple of years ago but can't see any reason to use it since from that time.
- ulisesrmzroche 13y agoI remember people used Lua to write WoW mods, but that's about it. Anything significant written in Lua?
- marshray 13y agoA lot of WoW itself seems to be written in Lua. A bug in Lua would occasionally allow a cheats in WoW, so it got heavily tested by motivated folks. I'd call that a significant deployment.
- deleted 13y ago[deleted]
- kemayo 13y agoIt's more fair to say that a lot of the UI for WoW is written in Lua, and most of the API available to that UI is also available to addons that end-users write for the game. You can take a look at the UI code, since it's dumpable from the game client: http://wowprogramming.com/utils/xmlbrowser http://wowprogramming.com/utils/xmlbrowser
- compay 13y agoAdobe Lightroom is about 40% written in Lua (the stat comes from Wikipedia). IIRC C and C++ are used for image processing and compute-intensive stuff, but the rest of it is all Lua. I believe that would be the most widely used app written mostly in Lua, though Apache, Redis, Nginx, Vim and some other big projects support using it for scripting.
- jzwinck 13y agoAlso note that Lightroom was written before LuaJIT was really a thing; had LuaJIT come first there might have been even more Lua in Lightroom. Like World of Warcraft, Lua also provides a plugin system in Lightroom (but very few people use it or even know it exists).
- Elv13 13y agoVLC, MySQL (and MySQL Workbench), Wireshark, AwesomeWM, NMAP, NGINX, Ogre, CEGUI (and most corporate game engines), OpenWRT, Recent TI calculator (bye bye TI basic) Many more, but it is what come to my mind right now
- cageface 13y agoIf I needed an embedded scripting language for a project I'd reach for Lua without a second thought. I've been strongly tempted to use it for actual core app development but every time I look at it the scarcity of common libraries compared to something like Ruby or Python cools me off.
- frik 13y agoIt would be great if one could use Lua as general purpose language. The missing part is the lack of a bigger official standard library. Several addon libraries that try to fill the gap are outdated, unmaintained and/or not of the same high quality code as Lua and it's official libraries. The Lua community would also benefit from an website that has unified documentation and comment functionality like php.net and Ruby Gems or Node.js NPN repository functionality. (I know Lua has a Wiki and repository ...but sadly, they look very dated, its features are from last century). Using Lua as embedded language is great though, especially with LuaJIT2 it is top notch. Interfacing with C is easy. C++ requires a bit of research as there are several possibilities (e.g. third party wrappers).
- justincormack 13y agoI don't think it needs an official standard library, just a community agreed and supported one which is well maintained.
- malkia 13y agocoroutines in lua a great for game development. Also the reference, luajit and one more commercial that I've seen are all based on Lua state object, rather than global data (python?). And luajit is more alien technology than common lisp :)
- da02 13y agoWhen you say "alien tech", do you mean "highly advanced" or "very different/hard to understand"?
- spc476 13y agoIt just works (for code written to Lua 5.1; it's about 95% compatible with Lua 5.2, which changed how modules work) and can pretty much replace Lua (either the standalone executable, or linking it as a library). The "alien tech" probably comes from how it works, converting Lua to native code. I can't say how difficult to understand, since I haven't really looked into the internals, but from what I understand, Mike Pall created a new way of looking at assembly language as part of LuaJIT.
- da02 13y agoThanks! It's also amazing how he did it with little resources compared to other projects.
- malkia 13y agoIt was a pun on Common Lisp (great language btw) - http://lisperati.com/logo.html http://lisperati.com/logo.html But then after seeing LuaJIT2 I think there was something even more alien. It made me understand that I don't know programming very well after seeing what Mike Pall did there.
- clumsysmurf 13y agoFor the JVM there is a native (no JNI) Lua port called luaj: http://luaj.sourceforge.net/ http://luaj.sourceforge.net/ I would be interested to hear anyone's experience using luaj on Android (thats where I plan to use it), and how it may compare to other options like Rhino.
- e12e 13y agoI'd love to hear how that works out -- generally I'd think it wasn't the best idea -- and that you'd be better off using actual lua (via the ndk): http://partiallyappliedlife.blogspot.no/2009/06/lua-on-java-on-dalvikvm-on-android.html http://partiallyappliedlife.blogspot.no/2009/06/lua-on-java-... See also: https://groups.google.com/forum/#!topic/android-developers/7PTpMTilY4E https://groups.google.com/forum/#!topic/android-developers/7... and: https://github.com/roscopeco/deelang https://github.com/roscopeco/deelang
- dottrap 13y agoI concur it is probably not a good idea. I know from other benchmarks that Dalvik in general has terrible performance and adding a layer on top of it is just going to make it worse. [3 days ago on HN] Dalvik vs. SpiderMonkey vs. Native https://news.ycombinator.com/item?id=6173757 https://news.ycombinator.com/item?id=6173757 Appcelerator originally did Rhino on Dalvik and moved to V8 after benchmarks showed Rhino was terrible. http://developer.appcelerator.com/question/133696/what-is-v8-what-is-rhino-what-is-android-runtime http://developer.appcelerator.com/question/133696/what-is-v8...
- dottrap 13y agoI never got past initial investigation of kahlua and luaj. However, they were appealing because I had a large server application based in Java that utilized spawning off other processes to help map/reduce data. We had written Lua scripts to do these helper tasks. But because of the Java, it was very expensive to spawn off other tasks, because they also needed to be wrapped in Java and starting a new Java instance ate a lot of memory and had a slow start up. The idea was to use kahlua or luaj to do the same helper tasks within the main process so other Java instances wouldn't have to be spawned. But for Android, plain old C based Lua works great with the NDK. There is also LuaJava and JNLua which can cross the native/Java bridge.
- sramsay 13y agoLua is indeed very, very aweseome -- for all the reasons enumerated, and then some. But your average Rubyist/Pythonista shouldn't be fooled. The ecosystem of Lua assumes you eat C for breakfast, and if you don't, you're going to be looking around for easy, plug-and-play libraries and wondering why they don't exist. They don't, because in Lua world, you either call C from Lua or embed Lua in C. You don't throw something together in Lua using the happy friendly libraries that others have written for your convenience (often, in C). I see comparisons being made with Lisp here and there. The most truthful comparison is sociological: as with the Lisp community, the Lua community (which is very friendly, approachable, and helpful) largely assumes you're smart enough to build it yourself. In C. If that frightens you, you should probably go back to whatever you were doing before.
- meddlepal 13y agoBut is that the attitude the community should be taking? Why can't it be both? In fact it would doubly-awesome if that were the case because having a language that has great support for C but can still be used with ease is a gap most languages fail to close because they choose the binary one or the other. What can be done to address that?
- kansface 13y agoReally, its one or the other by necessity. The things that make a language good for embedding are the opposite of what you want in a general purpose scripting language. Its also unlikely for any language to build great support for c unless thats the purpose of the language (ie, embedding). Lets consider Lua. Lua uses a prototype based inheritance system and coroutines for concurrency and has basically no standard library. Anyone who uses Lua extensively ends up building their own object/class system in addition to filling out the standard library. This makes sense; an embedded language shouldn't force its object model on its host and it doesn't need a standard library. The host most likely comes with a standard library of its own. The lack of common building blocks and the reliance on external code means Lua will never be a general purpose scripting language because its just too damn hard to reuse code.
- DigitalSea 13y agoI love Lua, it's definitely underrated. My first experience using Lua was via Indigo Rose Setup Factory, it uses Lua for scripting installer logic and the thing I love most is how easy it is to learn (even easier than a language like PHP). A few hours and you've grasped most basic concepts and conventions, if you already know another language you'll find the learning curve further reduced once more.
- vor_ 13y agoOne problem I have with Lua is that it silently returns nil when accessing non-existent elements. There are a few hacks around this, but the most common only protect global access. I have to wonder why the language reports no error in that situation given that it does report an error when comparing values of different types (no automatic conversion) or when attempting to index nil.
- pixie_ 13y agoDoes that make it like Objective-C because I love how accessing a null object doesn't blow up the application. So many trivial bugs in my iOS app that would have caused the entire application to crash are totally avoided. Unless you're creating software for the space shuttle, avoiding null exceptions entirely is a god send for my users.
- vor_ 13y agoIt's not like Objective-C, which returns a nil value when sending a message to nil. Lua, on the other hand, throws an error when attempting to index nil.
- anonymoushn 13y agoYou can change this to be more like Objective-C: debug.setmetatable(nil, {__call = function() return nil end, __index = function() return nil end}) f = nil print(f(f.bar.baz)()()()) -- nil This seems like an especially awful idea to me, though.
- SimHacker 13y agoStrongly disagree. You should fix trivial bugs before you ship the app, and Objective-C silently hiding them from you makes that much harder. It's one of the things I hate about Objective C. Makes it seem more like PHP with warnings off.
- pixie_ 13y agoWhy is it programmers love crashing their users for the most trival things? What if every javascript bug crashed the website you were browsing?
- ANTSANTS 13y agoThe title is misleading. I was expecting some kind of discussion of Lua's implementation, not yet another list of reasons why Lua is cool.
- copx 13y agoSome things to add: * Lexical scoping * Tail-call optimization * First class functions / closures Lua is not a functional programming language but I tend to have a large number of closures and recursive functions in my Lua code. Lua's limited feature set means that you end up using closures as a replacement for things which are special features in other language. Here is an example of a closure doing the job of C/C++'s function-scope static: View.Move = (function() local move = { [Direction.up] = View.Up, [Direction.left] = View.Left, [Direction.right] = View.Right, [Direction.down] = View.Down } return function(self, direction) move[direction](self) end end)() Note that a closure is more efficient than function-scope static here. The static requires a "is initialized?" check every time the function gets called, the closure does not.
- anonymoushn 13y agoThe features mentioned, along with coroutines and coroutine.clone, allow the creation of more or less any control flow the user could want. It's a total breeze to use. Is Closure(function ... end) really an improvement over the normal IIFE look of (function ... end)()?
- copx 13y ago>Is Closure(function ... end) really an improvement over the normal IIFE look of (function ... end)()? Now that you say it.. no, it's not. Changed.
- pygy_ 13y agoBetter still: do local move = { [Direction.up] = View.Up, [Direction.left] = View.Left, [Direction.right] = View.Right, [Direction.down] = View.Down } function View.move(self, direction) move[direction](self) end end
- Derbasti 13y agoLua's function definition syntax is really the best one I have seen yet. One simple statement that combines all the flexibility of lambda and def (in Ruby/Python-speak). I wish Ruby and Python had a powerful statement like that.
- frik 13y agoDoes someone use Lua for system scripting? Replacing bash and perl script on the server looks like a good fit. If so, can you share your experience?
- omaranto 13y agoYou'd need some libraries before that was comfortable. At the very least you would want to install LFS (Lua File System), for things like checking if files exists and what permissions they have. If Lua string patterns don't cut it for your text munging needs, you would want another library, either a regex library, or, even better LPeg. Even with those things, things like running several commands connected by pipes is not going to be as convenient as in sh or even Perl.
- justincormack 13y agoYeah have been working on it see https://github.com/justincormack/ljsyscall/ https://github.com/justincormack/ljsyscall/
- chalst 13y agoIt's used for some configuration/ scripting in NetBSD, after Marc Balmer pushed the idea a few years back. Some links: - The Lunatik GSoC project - http://netbsd-soc.sourceforge.net/projects/luakern http://netbsd-soc.sourceforge.net/projects/luakern - Marc Balmer's FOSDEM'13 paper - http://www.netbsd.org/~mbalmer/lua/lua_config.pdf http://www.netbsd.org/~mbalmer/lua/lua_config.pdf I gather that the principal virtues of Lua for this purpose are: 1. Lua tables have a nice syntax 2. Lua's load function is very fast 3. Lua has a small, well-tested trusted code base 4. It has a sane semantics for dynamic loading
- deleted 13y ago[deleted]
- microcolonel 13y agoI prefer scheme for an extension and wrapping language, but I can see how LUA's paradigm could be the fisher price buttons non-coders need.
- kazagistar 13y agoScheme is fine if your goal is only to have a small section of the programming community want to use it. Lua is great for everyone, including people who have never coded before in their lives as well as experienced software developers.
- snogglethorpe 13y agoScheme's a very decent language too, but so far I haven't found any scheme implementations out there which are anywhere near as good as Lua—small, portable, highly efficient, robust, easy to integrate [with the "underlying" program], well maintained, good and active community, etc—for this space. The most popular scheme implementations tend to be enormous, despite the language's historical reputation as a "small" language. Lua's a great language, but the amazingly good implementation is as much (or more) a factor than the language itself.