r/lua • u/flappypengujn • Sep 08 '17
Currying in Lua
I'm pretty new to Lua so please excuse any obvious oversights.
- Is there a consensus on the right way to curry an existing function (and I don't mean nested partial applications like
curry(curry(f,1),2), butcurry(f)(1)(2)). Are there any good libraries that do this? - Is there any possibility of curried functions being built into Lua? Or does it conflict with other fundamental language constructs, like varargs or something.
- One particular case that drives me crazy it that it seems really annoying to pass methods around, e.g. as a callback. It’s very easy to carry a function
a.faround, but it seems in order to pass the methoda:fyou have to writefunction(args) a:f(args) end. With currying, this wouldn’t be a problem since a:f would be syntax sugar fora.f(a). This seems to be far more intuitive and (at least to me) the “obvious” way the language should behave. Without currying, is there a simple way around this?
8
Upvotes
View all comments
Show parent comment
2
u/smog_alado Sep 09 '17 edited Sep 09 '17
That proposed
function a:fsyntax would be a breaking change (unlike just adding thea:fsyntax sugar) but I imagine you already know that...I am not sure that having methods be automatically curried is more intuitive or natural in Lua-land. For example when we call string methods like
s:sub(1,2)it will end up callingstring.sub(s,1,2)andstring.subis a function implemented in C that just happens to takesas its first argument and it doesn't do currying or know how to do so.BTW, this reminds me of another thing. For Lua it is very important that all language features are available for C functions using the C-API. If we were to add currying to Lua and modify the VM to better support currying we would also have to think of a way to support currying on the C side of things. (one example of this philosophy is how Lua has
pcallinstead oftry/catchblocks)