←back to thread

229 points generichuman | 2 comments | | HN request time: 0.439s | source
Show context
pmarreck ◴[] No.44001099[source]
I've been diving into Lua (a little late to this party, but turns out it's a perfect language to rewrite some commandline scripts I had that were getting unwieldy in Bash, especially with LLM assistance!) and it's really something of an eye-opener.

LuaJITted Lua code runs at 80% (on average, sometimes faster!) of the compiled C version of the same algorithm, typically. Lua is embedded in a surprisingly massive number of products: https://en.wikipedia.org/wiki/List_of_applications_using_Lua The startup time of a script is in nanoseconds. An "echo" written in Lua runs faster than the native echo implementation.

The only warts so far are 1-based indexing (you get used to it), and the fact that LuaJIT is stuck at Lua 5.1 while Lua itself is up to 5.3 or 5.4 and has added some niceties... with Lua proper running slower. And no real standard library to speak of (although some would argue that's a feature; there are a few options and different flavors out there if that's what you need, though- Such as functional-flavored ones...)

Anyway, there's nothing else like it out there. Especially with its relative simplicity.

There are also some neat languages that compile to (transpile to?) Lua, and deserve more attention, such as YueScript https://yuescript.org/, which is a still actively-updated enhanced dialect of MoonScript https://moonscript.org/ (described as "Coffeescript for Lua", although it hasn't been updated in 10 years) although neither of these are typed. HOWEVER... there IS this: TypescriptToLua https://typescripttolua.github.io/, which takes advantage of ALL the existing TypeScript tooling, it just outputs Lua instead of JS!

replies(5): >>44001229 #>>44003983 #>>44004723 #>>44005840 #>>44067553 #
jiehong ◴[] No.44003983[source]
Any recommendations going from bash to lua to watch out for except indexing?
replies(2): >>44005187 #>>44006636 #
nmz ◴[] No.44006636[source]
I do think you're better off using ruby, but if you insist.

Lots of default functionality missing so you MUST have these packages: inspect, luaposix, lrexlib-pcre, lrexlib-posix, lpeg, luastd/stdlib, luasocket, luahttp, luasec, luacheck, penlight

* luajit is unnecessary in almost all cases, you don't need the speed.

* use lsp or luacheck whenever you write something, entr -c luacheck file on everything.

* patterns are not regex which means they do not support lookups, backtracking or |, so you must install lrexlib-pcre or lrexlib-posix (frankly I never need pcre so I stick to lrexlib-gnu or lrexlib-posix).

* overload _ENV so it auto requires unknown things, I have a lua wrapper that does this and it makes it a joy not having all of my scripts with a bunch of require"posix" on all of them

* install inspect to inspect tables

* os.execute and io.popen only accepts strings as parameters which means you should overload it and make a function that accepts tables as well.

* 5.4 is still lacking support for many libraries, 5.3 has most of the libraries.

* assignments default to the global environment so you have to use local keyword or set _ENV to error on assignment (or better yet, don't care, just local _ENV = mymodule)

Overall, Lua is just a mixture of C with a pascal syntax and garbage collection (and also tables which is a weird data structure)

replies(1): >>44021562 #
1. pmarreck ◴[] No.44021562[source]
Ruby has a startup time and is slow. (Although compiled Crystal might be an idea...)

I am specifically looking for a language that:

1) is a scripting language (don't have to worry about compiling it)

2) starts up fast (so if I use it in a loop or a sequence of pipes, it won't necessarily be the bottleneck)

3) runs fast

4) is easy to work with and is either ridiculously simple or well-designed (Bash has so many footguns...), and scales up to medium-size code (anything that would take more than 1 file to implement should be promoted to another language)

Awk fit this. So did D, actually. Lua certainly does.

> luajit is unnecessary

Why not use it if it's there, it's faster, and you don't need features beyond 5.1?

> patterns are not regex

They're almost regex, they're faster than regex, and for 90% of use cases, you don't need full regex

> tables which is a weird data structure

Isn't a table basically just a JavaScript object, where metatables are the prototype object? Sort of, at least...

replies(1): >>44045047 #
2. nmz ◴[] No.44045047[source]
> Awk fit this. So did D, actually. Lua certainly does.

I do like awk and it definitely should be used more, but I stopped using it because of modules. gawk added it, but its strange having a script end in .awk and then its actually .gawk, and not working with mawk and so on.

So to me, modules are paramount, Lua focuses on embeddability and being an extension language means that writing to _G is fine. But as a system language you should never write to _G. this means that you may find a module for lua, but in reality its not lua, its a module for a program, usually a game.

Frankly, I think lua should not even be offered in your package manager and instead some sort of wrapper that works on POSIX systems should be used, batteries included and sane defaults as well. No more installing luaposix for basic functionality (if bash has it, this should as well). I and you are disparaging bash and claim lua is fast, but at the end of the day, bash has many builtins like test,cd,setenv while lua does not.

Also can D be used as a scripting language? I'm genuinely amazed and would give it a try.

> Why not use it if it's there, it's faster, and you don't need features beyond 5.1?

Sure, but I doubt anybody will need the performance unless of course you do.

FWIW there is a killer feature that I consider essential that everybody needs and that is <close>. I no longer have to worry about closing files, and I could probably use it for something else.

> They're almost regex, they're faster than regex, and for 90% of use cases, you don't need full regex

You may not need full regex (PCRE) but you usually need EREs specifically {} and | are used a whole lot.

> Isn't a table basically just a JavaScript object, where metatables are the prototype object? Sort of, at least...

I'd say JS is a weird language as well.

Mind you, I'm not disparaging lua, I do use it, but these are the foot guns. (Bundling is also a mess)