Lambda string functionality in Lua

Started by ihack2712, Mar 21, 2020, 06:12 pm

Previous topic - Next topic


Lua Lambda string function

I created a lambda function to manipulate a string of lua code and parse it into a callable function. When I made the lambda function I was in need of a simple or fast way to write short simple functions used as a callback in looping functions in my Array class.

The lambda string function manipulates a string in two ways, it replaces `$x` with `__args__[x]` and adds a return statement in the start of the code to return the first statement in your code automatically. That can be avoided by writing 'do' in front of the string (see examples).

Known Issues

* Dollar arguments does not respect whether or not it is placed in a string or comment node.

Example: Simple lambda function
Code Select
local cb = lambda "$1 + $2";

Example: Lambda multi-statement function
Code Select
local cb = lambda "do local x, y = $1, $2; return x * 2, y * 2;"

Example: Lambda multi-statement & multi-line function
Code Select
local cb = lambda [[ do
  local x, y = $1, $2;
  return x * 2, y * 2;

The source code for the lambda function lies in the URL below.

Although, if you are planning to use it in your own code you should probably use the minified version:

Also on pastebin:


Oooh, looks neat! Almost reminds me of Scheme's cut/cute macros, though (ab)using text processing instead.

Worth noting the links to your Gitea repo are 404ing - I assume the repository hasn't been made public?
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod


Indeed, visibility has been fixed :)