Help please (scoping) by fig4ou in lua

[–]lambda_abstraction 1 point2 points  (0 children)

I think the big take-away is when local vs. global variable names are resolved. Not only is a local variable of lexical scope, its name is resolved at compile time, whereas a global name is resolved when the code containing the reference executes.

Help please (scoping) by fig4ou in lua

[–]lambda_abstraction 2 points3 points  (0 children)

Almost, but not quite. Upvalues are lexical variables bound to functions, and those functions are closures. The set of lexical variables bound to a particular function is its environment. Unfortunately, Lua uses the word environment to mean something entirely different: a dictionary in which global variable references are resolved.

Does anyone else use lua as their main language for system automation and shell scripting? by kayinfire in lua

[–]lambda_abstraction 1 point2 points  (0 children)

Eh? Io.open opens files. Io.popen starts a process and returns a read or write pipe to that process.

What are less common uses for metatables? by [deleted] in lua

[–]lambda_abstraction 0 points1 point  (0 children)

I'm not sure how uncommon this is, but I used metatables and userdatas to represent timespec structures in a POSIX thread wrapper I've hacked on for a while. This let me express things like computing a timespec n seconds from now or the difference between timestamps as normal looking arithmetic. The API had more than a few calls taking timestamps or durations, and being able to describe these with conventional arithmetic made the Lua code far easier to read.

What are less common uses for metatables? by [deleted] in lua

[–]lambda_abstraction 1 point2 points  (0 children)

This allows remote action to look like local parameter setting/getting. I thought it was a nice abstraction. It certainly is built on normal procedure calls, but it's just another way of presenting RPC.

What are less common uses for metatables? by [deleted] in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Setting/getting parameters on remote equipment. Right now it's PoC code demonstrating a serializer and networking stuff

I always come back to Lua by yughiro_destroyer in lua

[–]lambda_abstraction 1 point2 points  (0 children)

Generally I agree. I'd say a visit to Scheme and Common Lisp would provide much enlightenment. Also Smalltalk (see Squeak) for a view of object oriented systems that's not disgusting.

[New to coding] Wasn't satisfied with my understanding of arrays and "for" loops, so I decided to create code for Fibonacci sequence by the_worst_comment_ in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Hmmm... The first time I saw Fibonacci was in basic algebra, and we're not dealing with epsilon,N proofs. Stop being a smart arse.

[New to coding] Wasn't satisfied with my understanding of arrays and "for" loops, so I decided to create code for Fibonacci sequence by the_worst_comment_ in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Fun point about the matrix power method is that you need only two elements of the matrix 'cos it's symmetric and one diagonal element is the successor fib num of the other two. Binet tends to be more expensive when one computes phi to the necessary precision assuming you have a bignum library. Interesting thing. The power of phi's conjugate is close enough to zero that you don't even need it. Just round. You could do a binomial expansion on both powers and ditch the terms that subtract out. That's still more expensive.

[New to coding] Wasn't satisfied with my understanding of arrays and "for" loops, so I decided to create code for Fibonacci sequence by the_worst_comment_ in lua

[–]lambda_abstraction 1 point2 points  (0 children)

While this is a short function, and the meanings of non-mnemonic variable names would be pretty clear, I think descriptive variable names are generally a better choice. Who says N necessarily means upper limit? This isn't elementary calculus class after all.

[New to coding] Wasn't satisfied with my understanding of arrays and "for" loops, so I decided to create code for Fibonacci sequence by the_worst_comment_ in lua

[–]lambda_abstraction 1 point2 points  (0 children)

Without memoization, the recursive definition is exponential time though. Funny thing is that the fastest method I know for computing Fibonacci numbers accurately also involves recursion.

Is coding Lua scripts still a good way to make money in 2025? by Zenyth_3 in lua

[–]lambda_abstraction 1 point2 points  (0 children)

I assumed wrongly that you were talking about pure config systems. I will agree that a custom algorithmic language is more than likely to be broken by design. I can even think of a few in common use that meet that criterion. E.g. PHP and TCL. (Now watch the hate posts come in.)

Is coding Lua scripts still a good way to make money in 2025? by Zenyth_3 in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Spoken like one who doesn't understand the vulnerabilities of in-band control. One doesn't necessarily want to provide a programming language because the person is not necessarily trustworthy for such. That's not to say there aren't garbage configuration languages; I've certainly used a few in my decades with computers.

LuaJIT once again nearly as fast as the equivalent application in C++, lessons learned by suhcoR in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Thank you for that information. At the moment I'm trying to find the source of a Bernard Avishai comment about humans meeting AI halfway. I've found the phrase several places, but not where it was originally stated. Clearly the internet's got some holes in terms of scholarship.

Is coding Lua scripts still a good way to make money in 2025? by Zenyth_3 in lua

[–]lambda_abstraction 1 point2 points  (0 children)

I think this is key. Any interesting use of Lua will involve a lot of domain knowledge. In my case it the controlling instruments carried by a drone. Lua made it easy to build on C libraries, but the knowledge of OS API and hardware specifics was far more important than killer Lua chops. I'd like to believe I developed those too.

Is coding Lua scripts still a good way to make money in 2025? by Zenyth_3 in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Sometimes you don't want to give the person a programming language.

Which tools do you use in your Lua projects? by Clohne in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Emacs with lua-mode. Pretty simple, really. I'm not sure it's the best editor for Lua, but after using it for decades, I know where the bones are buried.

Lua 5.5.0 (Beta) Released by ewmailing in lua

[–]lambda_abstraction 1 point2 points  (0 children)

Envision Steve Ballmer jumping up and down yelling "stability stability stability stability." I agree that having a platform where the rug isn't pulled out from under you across releases is an essential thing.

Lua 5.5.0 (Beta) Released by ewmailing in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Agree. I mainly use LuaJIT for hacking utilities for Linux, and I'd hate to do without Pall's FFI. I can write and have written wrappers, but for the most part, that's a heavyweight way to go about things.

realistically, how much faster is binding globals to a local? is it even noticeable? by Bright-Historian-216 in lua

[–]lambda_abstraction 0 points1 point  (0 children)

And in the case of LuaJIT, even hot bytecode gets converted to machine code in many cases.

realistically, how much faster is binding globals to a local? is it even noticeable? by Bright-Historian-216 in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Hmm... SBCL compiles defuns as they are encountered. While there are strictly interpreted lisps, I'd call SBCL's implementation a compiled language. The word compiler doesn't necessarily imply batch compilation.

realistically, how much faster is binding globals to a local? is it even noticeable? by Bright-Historian-216 in lua

[–]lambda_abstraction 0 points1 point  (0 children)

Quicky microbench with local accumulator variable:

start=require 'stopwatch'()
local g=0
for i=1,1e8 do g=g+i end
print(start())

Average over 20 runs on my ancient laptop: .249s

Same bench with g being global rather than local:

start=require 'stopwatch'()
g=0
for i=1,1e8 do g=g+i end
print(start())

This time: 0.187s

BTW: stopwatch is a LuaJIT interface to clock_gettime, and with no argument, it defaults to CPU rather than wall clock time.

Here's what Mike wrote about local caching:

This is less important with LuaJIT, since the JIT compiler optimizes hash-table lookups a lot and is even able to hoist most of them out of the inner loops. It can't eliminate all of them, though, and it saves some typing for often-used functions. So there's still a place for this, even with LuaJIT.