ComputerCraft Forums

ComputerCraft => Programs => APIs and Utilities => Topic started by: ihack2712 on Mar 21, 2020, 06:12 PM

Title: Lambda string functionality in Lua
Post by: ihack2712 on Mar 21, 2020, 06:12 PM
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 (https://ihacks.dev/cc-archive/Hack/src/branch/master/Hack/core/Array.lua) class.

The lambda string function manipulates a string in two ways, it replaces `$x` with `__args__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 (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 (https://ihacks.dev/cc-archive/lambda/src/branch/master/lambda.min.lua)

Also on pastebin:

https://pastebin.com/87huSSSd (https://pastebin.com/87huSSSd)
Title: Lambda string functionality in Lua
Post by: SquidDev on Mar 21, 2020, 06:21 PM
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?
Title: Lambda string functionality in Lua
Post by: ihack2712 on Mar 21, 2020, 09:10 PM
Indeed, visibility has been fixed :)