Quantcast

What makes Lua special?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

What makes Lua special?

Eike Decker
Hello Lua Users,

I'm preparing for a talk at my company in which I want to focus on things that are exclusive about Lua (and which I keep missing in other programming languages). 

While metatables are are neat, the concept isn't foreign in other languages (albeit it's often not as complete nor as concise). 

Things that come to my mind which I have never/rarely/barely encountered in other languages is:

1) The debug API which enables me to inspect the stack variables and defining hook functions within my program. While this can somehow be done in other languages, I am not aware of any language where this is an intrinsic part of the language API itself; It's always a mere VM functionality that is "chiseled in stone". 
Also cool is the ability to set a hook function that gets executed only after a certain amount of instructions, which is great for programming contests where the most efficient solution is searched - at least this makes the comparison very fair.

2) Environment tables which allows faking/sandboxing the global registry for functions: This is not impossible to do in other languages, but it's usually not as simple and often not even possible at all within the language itself. 

3) Coroutines: True coroutines are available in some other languages and some languages offer extensions for this, but again, this isn't easy nor cheap to do - and often not possible as well.

This is the list I would go through and showing examples for each point and what it can be used for.

Do you have anything else that comes to your mind? I would also greatly appreciate input regarding other languages that implement these things above and how it's done there. I'm quite fluent in Java, C#, C, Javascript and knowledgeable in C++ and PHP. While reflection works in these languages (somehow), it's often requires very arcane knowledge to do similar things as mentioned 1-3. 

Please let me know your thoughts!

Cheers & have a nice Weekend,
Eike 

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Daurnimator
On 21 April 2017 at 17:04, Eike Decker <[hidden email]> wrote:
> Do you have anything else that comes to your mind?

How about:

  - Size: in lines of code, complexity, #functions and actual runtime size
    Compared to other languages, you can actually learn+remember the
whole language
    You can read through the whole code base without getting lost
    You can remember the whole standard library!

  - Philosophy of "mechanism, not policy"
    If there is more than one way to do something, lua will generally
choose neither
    and instead provide an unopinionated api on which you can build your own

  - Embeddability and ease of interoperation with existing code
    Lua's strong C api and enclosed runtime makes it easy to integrate
into existing applications/kernels/games.
    Because of this you can use existing libraries instead of writing your own.
    Not to mention that it allows you to have cross-language teams, or
phase in Lua's introduction.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Oliver Kroth
In reply to this post by Eike Decker

> Do you have anything else that comes to your mind? I would also
> greatly appreciate input regarding other languages that implement
> these things above and how it's done there. I'm quite fluent in Java,
> C#, C, Javascript and knowledgeable in C++ and PHP. While reflection
> works in these languages (somehow), it's often requires very arcane
> knowledge to do similar things as mentioned 1-3.
>
What I like very much at Lua, and use a lot:

- you can use anything as index in a table, including other tables,
functions, coroutines, and userdata, like sockets.
This saves a lot of CPU time when managing these resources

- multiple vale assignment, and especiall the fact that functions may
return multiple values like
   a,b = b,a
or
   angle, radius = toPolar( x,y )

- closures are great for, e.g. scheduling actions, as parameter for
pattern substitution, iterators, etc.
once I got the concept, I don't want to miss them again.

- string.gsub() is one of the best find and replace implementations I
came across  in my not too short programming history.
Not even that one can use a function (more precisely a closure) as
parameter for the substitution, a table is fun enough.
It is not often that a UTF-8 to something else conversion is a one-liner:
   win1252 = utf8:gsub(  '[\xc0-\xef][\x80-\xbf]+', utf8ToWin1252 )
(utf8ToWin1252 is a table with the UTF8 encoding as keys and the
win-1252 encoding as values)

--
Oliver

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Aapo Talvensaari
In reply to this post by Eike Decker
On Fri, Apr 21, 2017 at 10:04 AM, Eike Decker <[hidden email]> wrote:
Hello Lua Users,

I'm preparing for a talk at my company in which I want to focus on things that are exclusive about Lua (and which I keep missing in other programming languages).

I did a generic presentation about the Lua for one company (here is the translated and shortened one of that):

Not sure if you find anything valuable there, but just letting you know.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Nagaev Boris
In reply to this post by Eike Decker
Hey!

On Fri, Apr 21, 2017 at 8:04 AM, Eike Decker <[hidden email]> wrote:
> Do you have anything else that comes to your mind? I would also greatly
> appreciate input regarding other languages that implement these things above
> and how it's done there. I'm quite fluent in Java, C#, C, Javascript and
> knowledgeable in C++ and PHP. While reflection works in these languages
> (somehow), it's often requires very arcane knowledge to do similar things as
> mentioned 1-3.

Take a look at Go language. It has many features you mentioned. It is
a statically typed language but it is much more convenient than C or
C++. As for me, it is somewhere in-between Lua and C. With Go I don't
have to write modules in C because Go itself is fast enough and has
enough access to low level stuff. At the same time I don't have to
embed a script language in a program for writing business logic - the
language itself is convenient enough to do that (e.g. it has GC and
rich reflection so I can for instance enumerate fields of a
structure). Finally the whole project can be written in a single
language.

> 1) The debug API which enables me to inspect the stack variables and
> defining hook functions within my program. While this can somehow be done in
> other languages, I am not aware of any language where this is an intrinsic
> part of the language API itself; It's always a mere VM functionality that is
> "chiseled in stone".
> Also cool is the ability to set a hook function that gets executed only
> after a certain amount of instructions, which is great for programming
> contests where the most efficient solution is searched - at least this makes
> the comparison very fair.

Go provides stack information: https://golang.org/pkg/runtime and
https://golang.org/pkg/runtime/debug/
You can also print stacks of all running goroutines (that is how
coroutines are called in Go).
There are also http://golang.org/pkg/runtime/pprof/ and
https://golang.org/pkg/runtime/trace/ packages which are very useful
to find bottlenecks in a program.

> 3) Coroutines: True coroutines are available in some other languages and
> some languages offer extensions for this, but again, this isn't easy nor
> cheap to do - and often not possible as well.

Go provides goroutines. Lua coroutines all run in a single OS thread,
but Go schedules goroutines automatically on up to GOMAXPROCS OS
threads. Lua coroutines have only one "channel" of commutation with
the rest of the world (through arguments of yield and resume), but
goroutines can have any number of channels to exchange variables.

On Fri, Apr 21, 2017 at 9:17 AM, Oliver Kroth <[hidden email]> wrote:
> - multiple vale assignment, and especiall the fact that functions may return
> multiple values like
>   a,b = b,a
> or
>   angle, radius = toPolar( x,y )
>
> - closures are great for, e.g. scheduling actions, as parameter for pattern
> substitution, iterators, etc.
> once I got the concept, I don't want to miss them again.

Go provides these two features as well.

As a bonus it has brilliant build tools which infer all the
information from Go files themselves, so you don't have to write
rockspecs, CMakeLists.txt etc. Other thing I really like is that the
package system is decentralized: import paths look like
"github.com/user/repo". There is no thing like a luarocks server for
publishing packages: you just push your code somewhere (e.g. Github or
your homepage) and other people can import it directly from there.

--
Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

steve donovan
On Fri, Apr 21, 2017 at 12:24 PM, Nagaev Boris <[hidden email]> wrote:
> language itself is convenient enough to do that (e.g. it has GC and
> rich reflection so I can for instance enumerate fields of a
> structure). Finally the whole project can be written in a single
> language.

I enjoy Go, but it's a curious fact that there are numerous projects
to provide binding to Lua (i myself have done one (luar) which
leverages Go reflection). So we have to ask, why Lua? It's not that Go
programs are slow to compile (unlike the big C++ game with Lua
situation) but it's because Lua scripts can modify running behaviour
dynamically.

> rockspecs, CMakeLists.txt etc. Other thing I really like is that the
> package system is decentralized: import paths look like
> "github.com/user/repo". There is no thing like a luarocks server for
> publishing packages: you just push your code somewhere (e.g. Github or
> your homepage) and other people can import it directly from there.

Go build system is brilliant, yes, but it's a known problem that you
are at the mercy of upstream every time you make a github reference in
a project.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Axel Kittenberger
In reply to this post by Eike Decker
Long story short: The main selling points of Lua are very small memory footprint for a dynamically, interpreted language and being in the realm of classical non-JIT languages the fastest beast around including fast upstart times (where JITs generally suck)

If these things are among your top considerations for a project, Lua it is for you.

If not there are likely better options for that particular project, be it a compiled language, a natively JIT enabled or one of the several scripting languages with more "batteries included".

Metatables, _ENV, coroutines etc. are just relatively minuscule flavor details, for which you'll easily find a functional equivalent anywhere, maybe it's not so much your flavor but that just details.

On Fri, Apr 21, 2017 at 9:04 AM, Eike Decker <[hidden email]> wrote:
Hello Lua Users,

I'm preparing for a talk at my company in which I want to focus on things that are exclusive about Lua (and which I keep missing in other programming languages). 

While metatables are are neat, the concept isn't foreign in other languages (albeit it's often not as complete nor as concise). 

Things that come to my mind which I have never/rarely/barely encountered in other languages is:

1) The debug API which enables me to inspect the stack variables and defining hook functions within my program. While this can somehow be done in other languages, I am not aware of any language where this is an intrinsic part of the language API itself; It's always a mere VM functionality that is "chiseled in stone". 
Also cool is the ability to set a hook function that gets executed only after a certain amount of instructions, which is great for programming contests where the most efficient solution is searched - at least this makes the comparison very fair.

2) Environment tables which allows faking/sandboxing the global registry for functions: This is not impossible to do in other languages, but it's usually not as simple and often not even possible at all within the language itself. 

3) Coroutines: True coroutines are available in some other languages and some languages offer extensions for this, but again, this isn't easy nor cheap to do - and often not possible as well.

This is the list I would go through and showing examples for each point and what it can be used for.

Do you have anything else that comes to your mind? I would also greatly appreciate input regarding other languages that implement these things above and how it's done there. I'm quite fluent in Java, C#, C, Javascript and knowledgeable in C++ and PHP. While reflection works in these languages (somehow), it's often requires very arcane knowledge to do similar things as mentioned 1-3. 

Please let me know your thoughts!

Cheers & have a nice Weekend,
Eike 


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Frank Kastenholz-2
In reply to this post by Daurnimator
On 4/21/17 3:19 AM, Daurnimator wrote:

> On 21 April 2017 at 17:04, Eike Decker <[hidden email]> wrote:
>> Do you have anything else that comes to your mind?
>
> How about:
>
>   - Size: in lines of code, complexity, #functions and actual runtime size
>     Compared to other languages, you can actually learn+remember the
> whole language
>     You can read through the whole code base without getting lost
>     You can remember the whole standard library!
>
>   - Philosophy of "mechanism, not policy"
>     If there is more than one way to do something, lua will generally
> choose neither
>     and instead provide an unopinionated api on which you can build your own
>
>   - Embeddability and ease of interoperation with existing code
>     Lua's strong C api and enclosed runtime makes it easy to integrate
> into existing applications/kernels/games.
>     Because of this you can use existing libraries instead of writing your own.
>     Not to mention that it allows you to have cross-language teams, or
> phase in Lua's introduction.

I had a similar problem at work; a project we did embedded Lua in our
larger code base and got a lot of "what is Lua?" and "Why use Lua?"
kickback. As Daurnimator pointed out, Lua's size and embedability are
extremely strong assets. Another strong point for Lua is its
license. The MIT license is better for my particular
environment. Your company may find the license compelling
since it does not place the same requirements on you that the GPL
does.

It's portability (strict adherence to C99, for example) is another
strong factor in its favor. If your environment is all Linux, or
the like, this isn't a big deal. For me, we had to run our software
in a variety of targets, not all of which run a Unix or Windows OS.

The other thing that might help you sell Lua is to get numbers
comparing its size/complexity to other languages such as Python
or Java. Besides pointing out the difference in code base sizes (number
of lines of code) and executables ... I brought one of the O'Reilly
Python books and a copy of PiL to a meeting and pointed out the
difference in sizes. There is a 5:1 difference in size. It was
one of those demonstrations that management easily understood.

Frank



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Aapo Talvensaari
In reply to this post by Nagaev Boris
On Fri, Apr 21, 2017 at 1:24 PM, Nagaev Boris <[hidden email]> wrote:
Finally the whole project can be written in a single
language (Go).

It depends on a project. But projects I have been doing in my whole career
very rarely can be written in a single language. You may need to access a
backend with say SQL. You may need to write Javascript for the UI (and
Go btw, is not particularly good choice for GUIs).

If you are writing networking servers / clients, Go might be one of the better
choices, but you hardly can "go" with a single language in any more complex
project. Bu you could just grab say OpenResty and write your networking server
with Lua (leveraging all the hard work and configurability done by Nginx team).
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Rena
In reply to this post by Oliver Kroth
On Apr 21, 2017 4:17 AM, "Oliver Kroth" <[hidden email]> wrote:

Do you have anything else that comes to your mind? I would also greatly appreciate input regarding other languages that implement these things above and how it's done there. I'm quite fluent in Java, C#, C, Javascript and knowledgeable in C++ and PHP. While reflection works in these languages (somehow), it's often requires very arcane knowledge to do similar things as mentioned 1-3.

What I like very much at Lua, and use a lot:

- you can use anything as index in a table, including other tables, functions, coroutines, and userdata, like sockets.
This saves a lot of CPU time when managing these resources

- multiple vale assignment, and especiall the fact that functions may return multiple values like
  a,b = b,a
or
  angle, radius = toPolar( x,y )

- closures are great for, e.g. scheduling actions, as parameter for pattern substitution, iterators, etc.
once I got the concept, I don't want to miss them again.

- string.gsub() is one of the best find and replace implementations I came across  in my not too short programming history.
Not even that one can use a function (more precisely a closure) as parameter for the substitution, a table is fun enough.
It is not often that a UTF-8 to something else conversion is a one-liner:
  win1252 = utf8:gsub(  '[\xc0-\xef][\x80-\xbf]+', utf8ToWin1252 )
(utf8ToWin1252 is a table with the UTF8 encoding as keys and the win-1252 encoding as values)

--
Oliver

Agreed with all this. What I love about Lua is not just that it provides powerful tools such as closures, coroutines, first-class functions, multiple assignment, pattern matching, and hash tables​ all built in, but that it does so while remaining very easy to understand and very lightweight.

In Python I often find myself having to double check if I've created a closure correctly; multiple assignment sometimes trips me up when I want to return a list, not everything can be used as a dict key, and I'm not sure about coroutines. It has all the same features, but Lua just makes them all so elegant.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Javier Guerra Giraldez
On 21 April 2017 at 16:49, Rena <[hidden email]> wrote:
> In Python I often find myself having to double check if I've created a
> closure correctly


this

Python and Javascript are often mentioned as "also having" some
features, but in many cases they don't get it just right, so the user
has to be constantly on guard for lots of corner cases.  ultimately
that results in those nice features being relegated as "too
sophisticated, just for language wizards", while the real power of
them comes when they're so natural in the language that everybody can
use them (and most importantly, read code that uses them!)

--
Javier

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Andrew Starks-2
In reply to this post by Eike Decker
On Fri, Apr 21, 2017 at 2:04 AM, Eike Decker <[hidden email]> wrote:

> Hello Lua Users,
>
> I'm preparing for a talk at my company in which I want to focus on things
> that are exclusive about Lua (and which I keep missing in other programming
> languages).
>
> While metatables are are neat, the concept isn't foreign in other languages
> (albeit it's often not as complete nor as concise).
>
> Things that come to my mind which I have never/rarely/barely encountered in
> other languages is:
>
> 1) The debug API which enables me to inspect the stack variables and
> defining hook functions within my program. While this can somehow be done in
> other languages, I am not aware of any language where this is an intrinsic
> part of the language API itself; It's always a mere VM functionality that is
> "chiseled in stone".
> Also cool is the ability to set a hook function that gets executed only
> after a certain amount of instructions, which is great for programming
> contests where the most efficient solution is searched - at least this makes
> the comparison very fair.
>
> 2) Environment tables which allows faking/sandboxing the global registry for
> functions: This is not impossible to do in other languages, but it's usually
> not as simple and often not even possible at all within the language itself.
>
> 3) Coroutines: True coroutines are available in some other languages and
> some languages offer extensions for this, but again, this isn't easy nor
> cheap to do - and often not possible as well.
>
> This is the list I would go through and showing examples for each point and
> what it can be used for.
>
> Do you have anything else that comes to your mind?
>

One that I had not seen mentioned is a controllable and replaceable
garbage collector. This allows you to control latency over all and in
critical spots within your code.

--
Andrew Starks

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Rena
On Apr 21, 2017 12:43, "Andrew Starks" <[hidden email]> wrote:
On Fri, Apr 21, 2017 at 2:04 AM, Eike Decker <[hidden email]> wrote:
> Hello Lua Users,
>
> I'm preparing for a talk at my company in which I want to focus on things
> that are exclusive about Lua (and which I keep missing in other programming
> languages).
>
> While metatables are are neat, the concept isn't foreign in other languages
> (albeit it's often not as complete nor as concise).
>
> Things that come to my mind which I have never/rarely/barely encountered in
> other languages is:
>
> 1) The debug API which enables me to inspect the stack variables and
> defining hook functions within my program. While this can somehow be done in
> other languages, I am not aware of any language where this is an intrinsic
> part of the language API itself; It's always a mere VM functionality that is
> "chiseled in stone".
> Also cool is the ability to set a hook function that gets executed only
> after a certain amount of instructions, which is great for programming
> contests where the most efficient solution is searched - at least this makes
> the comparison very fair.
>
> 2) Environment tables which allows faking/sandboxing the global registry for
> functions: This is not impossible to do in other languages, but it's usually
> not as simple and often not even possible at all within the language itself.
>
> 3) Coroutines: True coroutines are available in some other languages and
> some languages offer extensions for this, but again, this isn't easy nor
> cheap to do - and often not possible as well.
>
> This is the list I would go through and showing examples for each point and
> what it can be used for.
>
> Do you have anything else that comes to your mind?
>

One that I had not seen mentioned is a controllable and replaceable
garbage collector. This allows you to control latency over all and in
critical spots within your code.

--
Andrew Starks

That's another great point. One issue that Java used to have that gave it a bad reputation is that the garbage collector liked to kick in at inopportune times and bring everything to a screeching halt. In Lua you can easily control when the GC runs, so that it's happening during the idle time at the end of your game loop instead of during time-critical rendering. And it's very simple (one function call to change it to manual mode, one to run it) and the defaults are sane.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Nagaev Boris
In reply to this post by steve donovan
On Fri, Apr 21, 2017 at 11:58 AM, steve donovan
<[hidden email]> wrote:

> On Fri, Apr 21, 2017 at 12:24 PM, Nagaev Boris <[hidden email]> wrote:
>> language itself is convenient enough to do that (e.g. it has GC and
>> rich reflection so I can for instance enumerate fields of a
>> structure). Finally the whole project can be written in a single
>> language.
>
> I enjoy Go, but it's a curious fact that there are numerous projects
> to provide binding to Lua (i myself have done one (luar) which
> leverages Go reflection). So we have to ask, why Lua? It's not that Go
> programs are slow to compile (unlike the big C++ game with Lua
> situation) but it's because Lua scripts can modify running behaviour
> dynamically.

Good to know about luar. Looks very interesting!

Why to modify running behaviour dynamically, if you can recompile it
from scratch in few seconds?

>> rockspecs, CMakeLists.txt etc. Other thing I really like is that the
>> package system is decentralized: import paths look like
>> "github.com/user/repo". There is no thing like a luarocks server for
>> publishing packages: you just push your code somewhere (e.g. Github or
>> your homepage) and other people can import it directly from there.
>
> Go build system is brilliant, yes, but it's a known problem that you
> are at the mercy of upstream every time you make a github reference in
> a project.
>

This is a well known feature and it is a reflection of the practice of
putting everything in one large repo you can find in some companies.
If a project is actively developed then somebody takes eye on changes
of its deps. If a project doesn't build it is likely to be abandoned.

To pin versions of deps, you can fork all of them to your github user
and reference forks. Or create a vendor dir and put Git modules there.

--
Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What makes Lua special?

Eike Decker

2017-04-24 20:20 GMT+02:00 Nagaev Boris <[hidden email]>:
On Fri, Apr 21, 2017 at 11:58 AM, steve donovan
<[hidden email]> wrote:
> On Fri, Apr 21, 2017 at 12:24 PM, Nagaev Boris <[hidden email]> wrote:
>> language itself is convenient enough to do that (e.g. it has GC and
>> rich reflection so I can for instance enumerate fields of a
>> structure). Finally the whole project can be written in a single
>> language.
>
> I enjoy Go, but it's a curious fact that there are numerous projects
> to provide binding to Lua (i myself have done one (luar) which
> leverages Go reflection). So we have to ask, why Lua? It's not that Go
> programs are slow to compile (unlike the big C++ game with Lua
> situation) but it's because Lua scripts can modify running behaviour
> dynamically.

Good to know about luar. Looks very interesting!

Why to modify running behaviour dynamically, if you can recompile it
from scratch in few seconds?

>> rockspecs, CMakeLists.txt etc. Other thing I really like is that the
>> package system is decentralized: import paths look like
>> "github.com/user/repo". There is no thing like a luarocks server for
>> publishing packages: you just push your code somewhere (e.g. Github or
>> your homepage) and other people can import it directly from there.
>
> Go build system is brilliant, yes, but it's a known problem that you
> are at the mercy of upstream every time you make a github reference in
> a project.
>

This is a well known feature and it is a reflection of the practice of
putting everything in one large repo you can find in some companies.
If a project is actively developed then somebody takes eye on changes
of its deps. If a project doesn't build it is likely to be abandoned.

To pin versions of deps, you can fork all of them to your github user
and reference forks. Or create a vendor dir and put Git modules there.

--
Best regards,
Boris Nagaev



Thank you everyone letting me know your points! Though I really like all the points that were mentioned, I really want to focus on things that just work differently on a language level - also to keep it short.

I'll try to make the presentation public once it's done :)

Cheers,
Eike
Loading...