Last active
December 14, 2021 18:11
-
-
Save stuartpb/975399 to your computer and use it in GitHub Desktop.
Lua script for fancy named parameters with default values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local fp = require "fancyparams" | |
myfunction = fp( | |
{{"a"},{"b",7},{"c",5}}, | |
function(a, b, c) | |
print(a, b, c) | |
end | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local unpack = unpack or table.unpack | |
local function fancyparams(arg_def,f) | |
return function(args) | |
local params = {} | |
for i=1, #arg_def do | |
local paramname = arg_def[i][1] --the name of the first parameter to the function | |
local default_value = arg_def[i][2] | |
params[i] = args[i] or args[paramname] or default_value | |
end | |
return f(unpack(params,1,#arg_def)) | |
end | |
end | |
return fancyparams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment