Download (5 KB)
For Luanti 0.4.16/17 and above

How do I install this?

lua-lambda

Takes a string, returns a function for quick function definition.

Usage

The lambda format is as follows:

l"(args) expr"

Example:

local add_one = l"(x) x+1"
print(add_one(5)) --> 6

Why?

Because Lua needs more atomic modules which add functionality without creating a huge library. This module is supposed to be dead-simple to grab and start using in your project without adding anything extra. One of Lua's strengths is minimalism, and I think that it's ecosystem would do well to mirror that.

Reviews

Review

Do you recommend this mod?

  • English

    Would not recommend

    This mod hacks syntactic sugar for "lambda functions" into Lua by simply loading and caching strings containing Lua code via the (global!) l function.

    Unfortunately, the way this is done has too many drawbacks to be viable:

    • You lose anything static that operates on the code - syntax highlighting, static analysis, formatting, etc.
    • You lose syntax checking at load time - you can now get syntax errors at run time.
    • You lose upvalues, which often play a very important role when lambdas are involved.
    • You may lose some performance.
    • You may lose programmers - who will have to look up where l comes from (and why it should be used).

    Just bite the bullet and instead of l"(...) ...", write function(...) return ... end.

    If you can't take Lua not having enough syntactic sugar for you anymore, find an established Lua preprocessor that works for you and roll with that.

    0 comments
  • English

    Barely A Line

    As a "mod", it's barely providing any functionality at all. Lua has a built in function called loadstring that does this exact thing. A quick peek under the hood, and that is, in fact, all you're doing. You're also caching the calls, which is helpful for performance, but overall, loadstring is heavy, and if you're using it in your projects, you're probably doing something wrong in the first place. Who is this for?

    0 comments