r/lua • • Sep 08 '17

Currying in Lua

I'm pretty new to Lua so please excuse any obvious oversights.

  1. 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), but curry(f)(1)(2)). Are there any good libraries that do this?
  2. Is there any possibility of curried functions being built into Lua? Or does it conflict with other fundamental language constructs, like varargs or something.
  3. 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.f around, but it seems in order to pass the method a:f you have to write function(args) a:f(args) end. With currying, this wouldn’t be a problem since a:f would be syntax sugar for a.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

9 comments sorted by

View all comments

Show parent comment

2

u/smog_alado Sep 09 '17 edited Sep 09 '17

That proposed function a:f syntax would be a breaking change (unlike just adding the a:f syntax 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 calling string.sub(s,1,2) and string.sub is a function implemented in C that just happens to take s as 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 pcall instead of try/catch blocks)