Lambda string functionality in Lua

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

Previous topic - Next topic

ihack2712

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__
  • ` 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
local cb = lambda "$1 + $2";

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

Example: Lambda multi-statement & multi-line function
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.

https://ihacks.dev/cc-archive/lambda

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

https://ihacks.dev/cc-archive/lambda/src/branch/master/lambda.min.lua

Also on pastebin:

https://pastebin.com/87huSSSd
:)

SquidDev

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

ihack2712

Indeed, visibility has been fixed :)
:)