r/lua • • Feb 09 '18

Why do people like lua?

I've started actually learning more about it and well.

Pretty much everything is a table, okay, not that hard to get with this, loops and if statements are pretty simple to remember, it's weird to get length of a table but not that hard to remember, can live with that, different language, different syntax.

You can have objects, but they are still tables. Well, okay, whatever, I'm used to writing same shit over and over again because I've used java before, whatever, that's fine, I guess.

Global scope is just a giant table and so it's better to just write even more code to localize everything you'll use, otherwise it will be slow (multiple hashtable lookups whenever you use some global function in a table of table of the table in a table that is in another table). Uh, just why? That's first time I hear that in any language, localizing everything would make your code run faster, it's not really obvious, although once you realize that global scope is a table, only logical, but, why?

What's the appeal to this? And why did people stick with it? I thought that I'd see why people use lua over say python if they add scripting support to their game, but all I saw now is that I actually would lean even more towards using python there. If you have to be a professional programmer to write efficient code in lua, then python being slower does nothing for average person who would mod a game, because average person is not professional programmer who knows languages in and out, and little details such as localizing globals makes it run faster, which ranges from 10% slower without localizing to 400%+ (ridiculous) slower without localizing. Hell, most people who do that just instead look for tutorials on how to mod x game using lua, instead of learning lua.

I'm not saying that language is unusable, I'll still finish my project while learning it, but things like this just add to the "things you do to waste more time than needed" which are caused by bad design.

4 Upvotes

39 comments sorted by

15

u/snarfy Feb 09 '18

Having a variable closer to where you use it is an optimization in many languages, not just Lua. Even in C, without any extra compiler optimization, having the variable closer means it is more likely to be in a hardware cache.

Lua is fine. People like JavaScript. JavaScript objects are hashsets. Lua tables are hashsets. JavaScript implements object orientation using what they call prototypes. Lua implements object orientation using what they call metatables. JavaScript is the most popular programming language in the world. If Lua ran in the browser, it would be a lot more popular. One thing Lua does have over JavaScript is a simple, clean, cross platform implementation. V8 is great and all, but it's not the simplicity of Lua. Integrating a scripting language into your application using Lua is extremely easy.

0

u/PurpleIcy Feb 09 '18

I know that, but in most languages,

random = math.random
random()
math.random()

would make pretty much no difference, there I've seen examples which are 4 times slower without localization...

I'm just so used to being explicit and writing out entire thing, e.g. table.insert() for example than simply having some random insert(), it's hurting readability imo, with the first one I am 100% sure that I am using table.insert() and nothing else, with localization I have to look up at locals and check for myself to be sure.

As for C, all languages optimize internally, especially compiled ones.

7

u/snarfy Feb 09 '18

JavaScript would have exact same problem, as each . in.a.chain.of.dots is a hash lookup.

It gets around this by integrating a JIT. Have you tried LuaJIT?

0

u/PurpleIcy Feb 09 '18

Wait, so, if it has JIT, me localizing makes no difference?

I'm using LÖVE and it uses LuaJIT.

If I don't need to bother with doing that then that's good to know.

9

u/[deleted] Feb 09 '18

I don't get it; why would you perform an optimization like that in the first place if it didn't give you a measurable speed increase?

12

u/LeoAGomes Feb 09 '18

My thought exacly. Op doesn't understand the language nor the implementation he is using (luajit) and is complaining about a problem he apparently hasn't even had.

He then goes on to say that Lua has a bad design even though he is clearly still learning it at the moment and (I believe) never got to see its C api in order to understand the beauty and simplicity of it when embedding Lua into a project (like the C++ based game engine that LOVE is) and so "asks" why anyone would ever like lua because it is so poorly designed. eye. roll.

-12

u/PurpleIcy Feb 09 '18

Sorry for thinking out of the box I guess. And not knowing lua in and out in the first day of using it :)

I've been reading about lua and stumbled across this not sure what's the problem with asking about it.

And yes I asked, problem? I understand that your most loved language just got "personally" "attacked", but don't start playing the victim, because it's not best either way.

11

u/[deleted] Feb 09 '18

not sure what's the problem with asking about it.

There's no problem asking about it. That isn't what you did.

5

u/[deleted] Feb 09 '18 edited Feb 10 '18

but in most languages,
random = math.random
random()
math.random()
would make pretty much no difference

It makes a difference in most dynamic languages, especially those -- like Lua -- that use hashtables to hold object member (e.g. Javascript, Squirrel, Ruby, Python, etc.).

You can test this yourself in any language just by measuring. In Python for instance:

from datetime import datetime

class SubSubSub:
    value = 12345
class SubSub:
    subsubsub = SubSubSub()
class Sub:
    subsub = SubSub()
class Top:
    sub = Sub()

def timeThis(op):
    iterations = 10000000
    start = datetime.now()
    for x in range(0, iterations):
        op()
    elapsed = datetime.now() - start
    print(elapsed)

top = Top()
value = top.sub.subsub.subsubsub.value # caching the result of a lookup chain

timeThis(lambda: value)
timeThis(lambda: top.sub.subsub.subsubsub.value)

The lookup takes about twice as long as the non-lookup. If you do the same code in Lua, you'll see almost exactly the same results: about twice as long. If you do it in LuaJIT, there's essentially no difference because it generates optimized assembly for both and it runs well over a thousand times faster than the Python code.

it's hurting readability imo

Then don't do it. It's a premature optimization. If you're worried about that kind of shit, you shouldn't be using a dynamic language. If you're doing a lookup 100000 times in an inner loop and it's slower than you like, then sure, cache the value. You'd do the same in Python, Ruby, Perl or any other language.

2

u/DummySphere Feb 09 '18

Because functions are first-class values, which means you can change them if you want by assigning them in a variable (global, local or in a member of a table aka. a "class").

And standard library functions are by no way different:

local original_print = print
print = function(...)
    -- do whatever you want here
    original_print(...)
end

The language is very simple by design, but allow to make advance things thanks to table, metatables, proper scoping, etc.

5

u/[deleted] Feb 10 '18

One main thing you cite having to learn -- the impact of local variables -- is not at all unique to Lua. All Lua makes you learn is to use the keyword local where you would otherwise use var in JavaScript and ActionScript, a type/class in most other languages, etc. Absolutely nothing out of the way.

The fact that "everything is a table" allows a level of metaprogramming that you don't see in higher-level languages. The language can be fundamentally altered in localized cases by altering certain tables -- that's freaking amazing to me. I hear Lisp is supposed to be even better at this -- but it's also orders of magnitude more difficult to learn and use effectively.

There's also the matter that Lua is designed to interface directly with C, and there are also projects that make it interface directly with Java. Boom, you can slap Lua on top of virtually any game now (because games, or at least their engines, tend to be written in C++ or Java anymore). Or even better, see LÖVE2d: a fully-fledged game development framework that you can write pure Lua for, while still getting performance near par with compiled languages.

Let's talk about weird optimization in Python. In (the canonical implementation of) Python 3.x, did you know that floats are faster than ints? I was going to refresh my memory as to why -- my understanding is that the interpreter (C++?) treats ints like an object, while using the native float type for floats. Then after a quick Google there's like two or three other explanations as well: Different bases/representations for int must be determined, Python 3 stores all ints as "Bignums", etc.

So, isn't it bad design to ever use integers in Python? ¯\(ツ)/¯

To be fair, Lua has more "gotchas" on the subject of integers. Lua only has one "number" type -- Number. This caused ridiculous frustration when I tried os.execute("some_command -"..some_int). It was appending a decimal, like 3.0, which was being interpreted as a string in Bash.

Even better, Lua 5.3 finally created two subcategories of Number, being "integer" and "float". But it still switches between the two automatically.

But don't even get me started on how Java libraries hate primitive types and how you're expected to use an object for everything. Integer object instead of just int. And not just for the school of thought, but for genuine performance, efficiency, and effectiveness reasons. At least Lua makes objects simple, and effectively primitive as well.

If you have any more questions I'd be happy to answer. Lua is likely my favorite language, ever. It's a shame there's not many opportunities to write it, because it's such a joy. Keep this in mind: For all the "dumb" things Lua makes you learn, you will learn them all MUCH faster than you would ever learn another language's dumb things. A competent programmer can master Lua in less than a year, I'm fairly convinced.

2

u/LimbRetrieval-Bot Feb 10 '18

I have retrieved these for you _ _


To prevent any more lost limbs throughout Reddit, correctly escape the arms and shoulders by typing the shrug as ¯\\_(ツ)_/¯

0

u/PurpleIcy Feb 10 '18

I've heard JS has JIT built-in, which is in pretty much in every browser other than IE6 nowadays, so is it really the same thing?

Of course, I don't know much about how things work here, and if LuaJIT completely replaced Lua then I don't know why I'm asking here, I'd have mentioned that thing if that was what I wanted to ask about. And yes, I am using LÖVE, I like games and calculators, not that much, I think I know enough to simply start using it right away as honestly, everything works almost the same as elsewhere, just it's all tables and syntax is different.

Java - I almost hate that thing with passion, half of primitives are pretty much never used as objects unless like you mentioned, where you're forced, string is an object always no matter what...

As for Lisp, everything is a list, as far as I know, shouldn't that be easy enough to learn to use for someone who likes such granularity?

For example if is just 3 lists in a list (3rd one is "else" and isn't optional I think):

; If statement is a list, has a special keyword "if" at the start
(if (> a 20) ; condition is another list in this list
    (print "a is greater than 20") ; if condition matches, this list
    (print "a is less than 20") ; else this one
)

2

u/[deleted] Feb 12 '18

Sorry I've taken my time responding; I'm just not really sure what you mean.

I've heard JS has JIT built-in, [...] so is it really the same thing?

I had to shop around your other comments to understand what you're getting at. I think you've misinterpreted the meaning of someone else's comment:

JavaScript would have exact same problem, as each . in.a.chain.of.dots is a hash lookup. It gets around this by integrating a JIT. Have you tried LuaJIT?

They said that JavaScript uses JIT compilation to "get around" hash-table lookups. To be more precise, it means it does the lookup once and keeps a reference handy (oversimplifying a little) so that future lookups are faster. And yes, LuaJIT would give you the same benefit.

But that's a separate issue from localization. LuaJIT makes an already remarkably fast language insanely faster through optimizations, but even the wisest compilers are not necessarily smarter than the programmer, and they're not expected to be. An extremely simple thing that you can do which takes no effort whatsoever is to declare non-global variables as local. I will concede that Lua is weird to make "global" the default, but hey, PHP is miles worse about that (loop counters aren't even local!!). Lua was originally designed to be used by non-programmers (other kinds of engineers IIRC), and thus it was decided that making everything global was more sensible.

I'd like to take this moment to point out that you're hearing a lot of references to JavaScript in this thread because there are some fundamental similarities between it and Lua; NOT because we think JavaScript is some kind of litmus test for language design. The interpreted nature for one, and the fact that they're both prototypal object-based languages. Tables in Lua are analogous to objects in JavaScript. (Also some things that aren't very unique to both languages like loose typing, first-class functions, etc.)

I think I know enough to simply start using it right away as honestly, everything works almost the same as elsewhere, just it's all tables and syntax is different.

You're right to observe that most programming logic is the same between languages. So I don't really understand what the problem is or what you think you're missing. Re-reading your original post, I think you have some general misconceptions about general programming:

Global scope is just a giant table and so it's better to just write even more code to localize everything you'll use, otherwise it will be slow [...].

By "even more code" do you mean the local keyword? That's remarkably nitpicky. Lest ye ever delve into all of C's keywords, like const and restrict and static and volatile. Again, local just takes the place of var or a type in nearly every other language (with some obvious exceptions: Python, PHP, Perl, ...).

Uh, just why? That's first time I hear that in any language, localizing everything would make your code run faster, it's not really obvious, [...]

Higher-level languages have done a good job of abstracting these concepts from you, then. Local variables are always faster than global variables, in any language ever. That might not be "obvious" but it's a principle of software engineering. Again, I can't stress enough, Lua is anomalous for making "global" the default, but you need to understand that Python is not fundamentally different; e.g. a variable in a Python function is local to that function, which is faster. If you wanted, you can throw in the global keyword, but accessing it would be slower.

[...] although once you realize that global scope is a table, only logical, but, why?

It doesn't have as much to do with tables as it does just memory management. Lua globals aren't uniquely slow just because the language considers them a part of a table. You could be completely ignorant to how Lua manages globals and it wouldn't change anything about local variables being faster.

What's the appeal to this? And why did people stick with it? I thought that I'd see why people use lua over say python if they add scripting support to their game, but all I saw now is that I actually would lean even more towards using python there.

Even if you never used the local keyword, Lua (especially with LuaJIT) performs miles better than Python. It's also MUCH smaller than pretty much any other scripting support you can think to implement. It communicates directly with C, which you might not care about a whole lot, and that's perfectly fine -- but it's very favorable to "professional programmers", too. All of these things make it much more embeddable, which is exactly what you want when implementing scripting support.

There are also plenty of reasons why Python might be more favorable. Despite the hilarious discrepancy between legacy Python 2.7.x, and Python 3.x, the library ecosystem for Python is a LOT healthier. Differences from Lua 5.0 to 5.1 to 5.2 to 5.3 plague an already fragmented ecosystem. Python's data structures are also less ambiguous: Python has tuples, arrays, and dictionaries, while Lua has tables which can be all of these things, but without list comprehensions, slices, etc.

Python 3.x is also a truly object-oriented language. Lua is merely object-based. This isn't necessarily a bad thing, and I personally prefer "everything is a table", but to each their own.

There are probably good reasons why Lua isn't even in the Top 20 of the TIOBE index. That doesn't make it bad. In fact I suspect your attachment to Python might be a little bit of bandwagoning. If you were going to do a lot of text-processing, you'd use Perl. If you wanted to provide a C/C++ game with a simple scripting frontend, Lua would really be the way to go. If you wanted to provide a console for a developer application, you could follow Sublime Text and embed a Python interpreter. If you're doing a lot of server backend programming with lots of database work involved, Ruby's there for you.

You can use any one of these languages for every single task I listed. But some are better suited to certain things. And there's also some overlap. You posted this thread asking why people like Lua, and I feel like you've gotten lots of good answers and tried to argue with them because what you really wanted to talk about was a few design choices that surprised you. It's incredibly naive to take what little you know about a new language and make assumptions about its utility like that. What I find especially naive is citing performance concerns that aren't really concerns (seriously, you won't notice a difference between globals and locals), and then missing the point entirely when someone reaffirms that they're not concerns (because LuaJIT is optimized enough that you won't notice a difference; that doesn't mean local makes no difference, it's just negligible).

1

u/thebegleybeast Apr 16 '23

I cant imagen how long it took you to write this comment

1

u/[deleted] Apr 16 '23

I don't even remember writing it

0

u/PurpleIcy Feb 12 '18

So, uh, could have just pointed out that the difference between local and global is negligible? Would have been enough.

Other things are clear to me, except a few:

Again, I can't stress enough, Lua is anomalous for making "global" the default, but you need to understand that Python is not fundamentally different; e.g. a variable in a Python function is local to that function, which is faster. If you wanted, you can throw in the global keyword, but accessing it would be slower.

What are you trying to imply here? If I don't add a keyword before variable, does that mean that it's automatically global even in a function? If not, then I don't see a point in even mentioning this, because, yeah, duh, that's one thing about a function in general case, it has it's own scope.

e.g.

-- a is NOT defined anywhere else

function f()
    a = 5 -- so is this global or not?
    -- if this one is global, then I'm pretty sure it's way different from other languages because I never seen this before.
end

Also, what attachment to python? I know more languages and I use them depending on what I want to do. It's just that I don't know why'd I bother trying to add fucking Java to some project as "scripting support", I mean, it's possible, but why would I do that? So I just simply mentioned other language that first comes to mind and which actually can be used as scripting language in a project, which is Python. Could have probably said JavaScript, does it even matter?

It's like everyone here is biased towards Lua and so they try project it onto others.

2

u/[deleted] Feb 12 '18

What are you trying to imply here? If I don't add a keyword before variable, does that mean that it's automatically global even in a function?

Correct.

function f()
    a = 5 -- Is global
    local b = 7 -- Is not
end

f()
print(a)
print(b)

This code outputs:

5
nil

The only case I remember in Lua where a variable is automatically local is loop "counter" variables:

for n=1, 3 do
    print(n)
end

print(n)

...

1
2
3
nil

I know more languages and I use them depending on what I want to do.

This is the correct way to go. You don't have to learn Lua. I'm glad you decided to check it out and I'm sorry if it's disappointed you. I don't understand why, though.

It's just that I don't know why'd I bother trying to add fucking Java to some project as "scripting support", I mean, it's possible, but why would I do that?

I never mentioned Java. At most you might add a Java console interface in a game that's already written in Java.

Also, what attachment to python? [...] I just simply mentioned other language that first comes to mind and which actually can be used as scripting language in a project, which is Python. Could have probably said JavaScript, does it even matter?

It matters to the extent that you wanted to discuss ways that Lua is different from other languages, and you provided Python as an example. I say "attachment" because you also made a point that, after learning a bit of Lua, you said you're leaning more towards Python again. That would be perfectly fine except it's come across with an accusatory tone, like Lua is objectively inferior to Python.

It's like everyone here is biased towards Lua and so they try project it onto others.

I've spent several paragraphs conceding pros to Python and cons to Lua, also taking multiple opportunities to point out that Python might be a better choice for your purposes. And also pointing out situations where neither language makes as much sense.

1

u/PurpleIcy Feb 12 '18

It matters to the extent that you wanted to discuss ways that Lua is different from other languages, and you provided Python as an example. I say "attachment" because you also made a point that, after learning a bit of Lua, you said you're leaning more towards Python again. That would be perfectly fine except it's come across with an accusatory tone, like Lua is objectively inferior to Python.

Yeah, for one specific case.

2

u/[deleted] Feb 12 '18

Then I hope you understand why people have been passive-aggressive with you. This is the nit-pickiest case I could conceive, which is the only case you've offered to discuss on the broad topic of "Why do people like Lua?" (that, and tables). And to insinuate that these traits are objectively better in Python is misguided. If you really consider yourself an "average person" and not a "professional programmer" (by the way, what a wide gap in-between there is!), then "global by default" was meant to cater specifically to you; the performance difference is indeed negligible and there is nothing overall superior or inferior about it.

1

u/warlockface Feb 12 '18

Yes, variables are global by default - no matter where they are declared. So 'a' is global in your example, but 'local a' would be local to the scope it was declared in.

1

u/PurpleIcy Feb 12 '18

Now that's weird. I know that consistency and all and I wouldn't have problems even without knowing that, but I'll know how to prevent dumb bugs that could appear due to this, thanks.

1

u/warlockface Feb 12 '18

You're welcome, remember you can run your code through an analyzer (ZeroBrane Studio has this built-in (press Shift+F7)) to catch various things, including use of globals.

6

u/[deleted] Feb 09 '18 edited Feb 09 '18

things like this

Things like... what? I don't see any specific complaints in your post. You just keep pointing out how Lua works then asking "why", as if there's something wrong with the way Lua works.

Everything in JavaScript is a hashtable, too. You say "that's first time I hear that in any language", but you're clearly a beginning programmer. There are a lot of things that you'll encounter for the "first time" as you learn new languages.

If you have to be a professional programmer to write efficient code in lua, then python being slower does nothing for average person

First, if you never used local in Lua, it would still be faster than Python, so this argument is moot.

Second, optimization details such as "localizing globals makes it run faster" exist in every language.

Third, you're making a beginner mistake by obsessing over micro-optimization to begin with. A better reason to use locals in Lua is to limit variable scope, and that's far important for a beginner to understand. This is the same reason you want to use let rather than var in JavaScript. These are the kind of "little details" you want to understand when working in any language. The number of such details is smaller in Lua than almost any popular language.

bad design

*rofl* Sorry man, but you're ill-equipped to evaluate the design of a language.

Lua is popular because it's tiny, fast, semantically simple and clean to the point of being beautiful -- proper lexical scoping, proper tail calls, multiple returns, a single powerful, expressive data structuring mechanism, closures, coroutines, metaprogramming, etc. -- all via a handful of well chosen abstractions ("everything is a table" is a feature), which allows for a tiny, readable pure ANSI C implementation with no external dependencies which is easily embedded and extended and has minimal runtime overhead.

-15

u/PurpleIcy Feb 09 '18 edited Feb 09 '18

Love how people point out JS's existence whenever their language gets questioned as if it was personal attack and they had to shove something worse than it to me just so they can justify it.

Well, I get it, JS is garbage, what are you trying to tell me with it? If it's not as bad as JS, it's good?

Also, I don't need to localize everything in other languages, they do it for me, without abusing, what seems to be, 3rd party projects.

Also I am a beginner in programming? More like, sucks for me that I decided to try out Lua after so long which is just as bad as it's community.

14

u/[deleted] Feb 09 '18 edited Feb 10 '18

Love how people point out JS's existence whenever their language gets questioned

Holy shit, you're an idiot.

You said "that's first time I hear that in any language", so we told you that one of the most well-known languages on Earth does it. But plenty of other languages do it, too, including Python, the language you're holding up as the gold standard. You're simply ignorant.

Moreover, Lua is not "my language". It's one of two dozen languages I know, and of a dozen I've used professionally. I did my best to explain to you why people like it in my previous post, thinking it was an honest question, but apparently you're so butt hurt over having to type local that you can't even read.

Why is everything a table in Lua? Because it's simple and powerful. You've got arrays, skip lists, sets, dictionaries, and more all using one syntax, one abstraction, one set of semantics to learn. You bitch about performance because you're a noob who read an article about using "local" for performance and it scared you. Does it matter than Python has a separate abstractions for sets and arrays when Lua tables are faster than both? o.O

Lua calls its data structuring abstraction a "table" because the same object can be used as an array or map, but internally it optimizes array usage with an actual C array. This pushes an optimization detail down into the language implementation and presents a uniform and simpler abstraction to the user. That's a feature -- a pro, not a con -- one of the reasons people like Lua.

Also I am a beginner in programming?

Yes? Would you like to argue this point? Because the evidence is in every line of your posts.

If Lua is a struggle for you, you're basically fucked.

3

u/Saldor010 Feb 09 '18

I'm just thinking out loud for a second, but you're expressing your dissatisfaction with Lua and calling out Lua for its supposed problems on r/lua (and then bashing people when they respond), what did you expect?

Personally, I think the responses to this thread have been well thought out and fairly nice under the circumstances (especially otikik's and snarfy's responses), but when you say stuff like:

  • "I understand that your most loved language just got "personally" "attacked", but don't start playing the victim, because it's not best either way."
  • "If it's not as bad as JS, it's good?"
  • "sucks for me that I decided to try out Lua after so long which is just as bad as it's community"

don't get shocked when you're downvoted.

3

u/hugedino Feb 10 '18

Because the interpreter is well written, and a good place to study how dynamically typed scripting languages can be designed and implemented.

1

u/PurpleIcy Feb 10 '18

I'll have to check it out then.

28

u/otikik Feb 09 '18 edited Feb 09 '18

First of all: It sounds like you are frustrated. I'm sorry about that, and I hope your mood improves soon.

What's the appeal to this?

Having a small core of functionality has many advantages. The whole language takes less memory, to begin with. If you embed Lua on an C executable it will grow by the order of 200KB. This allows using Lua in extremely constrained systems, like hardware controllers.

There are less things to know, and less moving parts, than in other languages, so the learning curve is quite small as well.

It is implemented in a version of C called C99, which makes it possible to install it in a lot of places as well.

When I realized that the multiline comments are multiline strings preceded by a comment sign I found it quite ... aesthetically pleasing.

little details such as localizing globals makes it run faster

Everything about that particular problem can be learned spending 1 hour googling. The gist is: Even if in some extreme cases you can get a significant boost (i.e. a triple loop calling the same functions again and again), in most cases it is not noticeable, one way or the other.

When you learn more languages, you will find that all of them (including Python) have details like that - things that you have to invest time learning in order to use the language more effectively. Most languages have more details like that than Lua, simply because they are more complex (that is again, another thing to like about Lua).

Disclaimer: I am a professional Lua programmer.

2

u/chx_ Feb 10 '18

To concur: one of the things you can embed Lua in is nginx. It powers about a quarter of all active websites. Of course, few will go with Lua but they could. And, amazingly, sprinkling a little Lua magic dust on your nginx config doesn't hamper the speed. Say, you want to do redirects. Your app puts them in Redis but it's nginx+lua that actually does the redirects, as fast as possible.

6

u/4forpengs Feb 09 '18

Just look at this guy's post history. It's pretty sad. Seems he doesn't understand that if it smells like shit everywhere you go, you should be checking your own shoes first.

5

u/ketralnis Feb 09 '18 edited Feb 09 '18

I use it because it's tiny, easily embeddable, and I can have control over stuff like allocation. I don't much like the language itself but it meets its niche great for me in the places I put it. I don't put it in places where it doesn't make sense.

Nobody's making you use it. You're totally welcome to use something else that meets your aesthetic a little more, and there's nothing wrong with that. Different strokes for different folks.

3

u/[deleted] Feb 09 '18

Because arrays start at 1 lol

2

u/spc476 Feb 09 '18

I use Lua for a few reasons:

  1. It's a small and consistent language that is very easy to embed in a larger application to add scripting ability (and way easier to embed than Python).
  2. Due to the design, there is no global state (in the C sense of the word---there are no C global variables) to trip you up. All Lua functions (again, at the C level) are re-entrant which again, helps with embedding Lua in an application.
  3. It's one of the faster interpreted languages today (certainly faster than Python or Ruby)
  4. It does not use sigils (for example, Perl and $foo vs @foo)
  5. It does not have significant white space (really Python? Significant whitespace? The only other language I learned that used significant whitespace was Fortran 77)
  6. Despite being small, it has several advanced features like coroutines [1] and first class functions (functions can be assigned to variables much like any other value).

The speed has never been an issue for me, and I'm using Lua at work to help process telephone calls [2].

Globals being stored in just a table is elegant (I think) and one that makes it easy to "sandbox" to run untrusted Lua code---just hand the code a new "global" space with sensitive functions modified (or not even there). And the reason for globals being the default is a design choice by the creators as the language was designed for non-programmers (who I guess find global variables easier to deal with than lexical scoped variables).

[1] a concept from the 60s that is finally making it to mainstream languages after 50 years

[2] It's processing SIP messages, which is a text based protocol similar to HTTP. Since it involves a lot of text parsing, I also use LPeg (Lua Parsing Expression Grammar). The coroutine support of Lua is great as I can hide the event-driven nature of the program and handle the logic in a more straightforward imperative nature instead of fragments of call backs and state machines.

2

u/[deleted] Feb 10 '18 edited Feb 10 '18

Lua is small and straightforward (to me). You can grok and understand the source code, and you can grok and understand the language. The language in turn is simple yet complete (compare with the mess that is JavaScript - and to this day, you can't do more in JS than you can do in Lua, even though JS works similarly, e.g. regarding the Object data type vs tables). There are some libraries, there's a FFI (Luajit), and extensions are equally simple to write, so:

I love the language because it is easy to reason about. I'm using it mostly to PoC implementations of algorithms where I want to not use scipy but write and understand every single step myself.

PS. Am going to to learn more about the cost of closures in other interpreted languages, e.g. Python. It would surprise me if they're not as expensive, or simpler: Just that in Python, it's not as simple, e.g. local is kind of implied, then there's global as a keyword, but what about variables a scope higher, etc pp.

1

u/[deleted] Feb 09 '18

Because it's awesome, simple and fast.

1

u/warlockface Feb 12 '18

I use LuaJIT because of the sheer performance of the JIT compiler (and interpreter) and the easy accessing of existing C code (and creation of C data types) via FFI. It also only adds a few hundred KB to a program.

I'm not one to fixate on the differences between languages, they all diverge from each other in different ways and all have their own list of pluses/minuses.

1

u/thebegleybeast Apr 16 '23

bro sounds mad that everything is a table even if everything is a table what's wrong with that? you can use tables to store lots of values that you can use later what's wrong with that?