脱糖 Lua 运算符
由于 Lua 支持一等函数,我想知道是否可以像许多函数式语言一样对运算符进行脱糖操作。例如,在 OCaml 中,您可以执行以下操作:
let x = (+) 3 5
上面的代码使用值 3 + 5
初始化变量 x
。编写 (+)
相当于拥有一个接受两个参数并返回它们之和的本地函数。 (+) 3 5
使用两个参数 3
和 5
调用此函数。 其背后的动机是您可以将运算符直接传递给函数,而不必之前将其包装在函数中:
local t = {"ab", "d", "c" }
local function op_greaterthan (a,b) return a>b end
table.sort (t, op_greaterthan) --would like to write: table.sort (t, (>))
谢谢!
Since Lua supports first-class functions, I'd like to know if you can desugar operators, like in many functional languages. E.g in OCaml you can do:
let x = (+) 3 5
The code above inits the variable x
with the value 3 + 5
. Writing (+)
is equivalent of having a local function that takes two parameters and returns their sum. (+) 3 5
is calling this function with the two arguments 3
and 5
.
The motivation behinds this is that you can pass the operator directly to a function without having to wrapped it in a function before:
local t = {"ab", "d", "c" }
local function op_greaterthan (a,b) return a>b end
table.sort (t, op_greaterthan) --would like to write: table.sort (t, (>))
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。
Lua解释器很小,在处理运算符时“走捷径”;对于解析器来说,它们根本就不是“函数”。
如果您尝试使用不带参数的运算符,如下所示:
那么解释器将抛出语法错误。
由于这种实现,您只能使用已经讨论过的选项:要么使用包装函数(例如
add
),要么传入字符串等某种eval
>,如 jpjacobs 的解决方案所示。You can't.
The Lua interpreter is very small, and it "makes shortcuts" when dealing with operators; for the parser, they are simply not "functions".
If you try to use an operator without its params, like this:
Then the interpreter will throw a syntax error.
Due to this implementation, you are limited to the options already discussed: either you use a wrapper function (such as
add
) or you pass in a string and so some kind ofeval
, as in jpjacobs' solution.是的,你可以(我不明白它的意义,它会更慢,但这是可能的):
编辑:消除杂散全局变量 a 和 b,并放入记忆以提高性能,每个操作仅编译一次。
Yes you can (I don't see the point of it, and it will be slower, but it is possible):
Edit: Eliminated stray globals a and b, and put in memoizing to improve performance, by compiling each operation only once.