function eval(str)
if type(str) == "string" then
return loadstring("return " .. str)()
elseif type(str) == "number" then
return loadstring("return " .. tostring(str))()
else
error("is not a string")
end
end
function lambda(lambda_string,...)
--验证是否仅存在一个:号
pos = string.find(lambda_string,":")
if pos ~= nil then
if string.find(lambda_string,":",pos+1)~= nil then
error('more than one ":"')
end
end
if type(lambda_string) ~= "string" then
error("is not a string")
end
--lambda x:x+x 将其分割为 参数 x 和 表达式 x+x 的形式
parameter = string.sub(lambda_string,1,pos-1)
expression = string.sub(lambda_string,pos+1,-1)
fun = string.format("return function(%s) return %s end",parameter,expression)
local func = loadstring(fun)()(...)
return func
end
if type(str) == "string" then
return loadstring("return " .. str)()
elseif type(str) == "number" then
return loadstring("return " .. tostring(str))()
else
error("is not a string")
end
end
function lambda(lambda_string,...)
--验证是否仅存在一个:号
pos = string.find(lambda_string,":")
if pos ~= nil then
if string.find(lambda_string,":",pos+1)~= nil then
error('more than one ":"')
end
end
if type(lambda_string) ~= "string" then
error("is not a string")
end
--lambda x:x+x 将其分割为 参数 x 和 表达式 x+x 的形式
parameter = string.sub(lambda_string,1,pos-1)
expression = string.sub(lambda_string,pos+1,-1)
fun = string.format("return function(%s) return %s end",parameter,expression)
local func = loadstring(fun)()(...)
return func
end
本文详细解析了如何使用Lua脚本语言将字符串形式的Lambda表达式转换为可执行函数,包括验证字符串中是否只包含一个冒号、确保输入类型为字符串或数字、并正确分割参数和表达式部分。
293

被折叠的 条评论
为什么被折叠?



