Suggestion: Reusable formula in Lua

125 views
Skip to first unread message

名前이름

unread,
Oct 23, 2025, 8:34:36 AMOct 23
to lua-l
Hey everyone,

Hello, I'm new here, so I'm sorry if I wasn't clear or posted this in the wrong area. I just had an idea I thought might be useful or practical for Lua, where you have a variable having a value of a math formula, and within that formula are different variables being used. And then, in the same code, you may encounter a situation where you can reuse the same math formula. Instead of manually creating another variable with the same formula, you can reuse the predefined variable and a function to replace the variables with different ones. It’d make stuff like game scripting, math simulations, or quick prototyping a lot easier, kinda like reusable expressions.

Does something like this already exist or sound possible to implement?

Thanks!

Андрей Закатов

unread,
Oct 23, 2025, 9:23:20 AMOct 23
to lu...@googlegroups.com
I guess I did not really understand you well enough, but what is stopping you from making your function a global, just passing the argument in there.

Or do you want something more like a closure, like this?

function greeter(prefix)
  return function(name)
    return string.format("Hello, %s %s",prefix,name)
  end
end


--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/0a54e879-7af6-4a28-a660-f8691030e9f2n%40googlegroups.com.

Scott Morgan

unread,
Oct 23, 2025, 9:27:54 AMOct 23
to lu...@googlegroups.com
It sounds like you're just talking about functions. Can you give an
example of what you're thinking about?

Scott

Sewbacca

unread,
Oct 23, 2025, 12:45:12 PMOct 23
to lu...@googlegroups.com

Sure, let's say you want to calculate the volume of a cylinder, which is the area of the radius times the circle (I'm just contriving an example to make a point):

local volume = h * math.pi * r^2

If you want to reuse the circle equation just define a function

local function circlearea(r)
    return math.pi * r^2
end

and you can reuse the calculation of the circle for the volume like this:

local volume = h * circlearea(r)

名前이름

unread,
Oct 24, 2025, 12:10:20 AMOct 24
to lua-l
What if it's a long formula, custom formula specifically that you didn't even know you would need it again only until now

Martin Eden

unread,
Oct 24, 2025, 2:39:08 PM (13 days ago) Oct 24
to lu...@googlegroups.com
On 2025-10-24 06:10, 名前이름 wrote:
> What if it's a long formula, custom formula specifically that you didn't
> even know you would need it again only until now

Use "up arrow" when running Lua in dialogue mode.

-- Martin


Reply all
Reply to author
Forward
0 new messages