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
1
u/flappypengujn Sep 09 '17
This is a good point, and it's apparent to me now that generically converting existing functions to a curried version is pretty much impossible.
As for varargs, that doesn't seem to be a fundamental barrier to currying by itself. As an exercise I tried to make a function such that
sum() = 0,sum(1)() = 1,sum(1)(2)() = 3etc., and I think the following works(There may be uses for variable length arguments that can't be taken care of like this, but I'm having trouble coming up with an example of a good reason to use varargs that doesn't involve a fold.)
Although, it is way easier to just have varargs be handled through a table. Similarly with nil.
Actually, it seems that Lua functions are more or less equivalent to a 1-argument function accepting a table argument. There is nothing wrong with this, but I guess what I'd really like to see is better support for (from the language) and more explicit use of (from the community) currying where appropriate. For example if your function has 2 mandatory arguments and a bunch of vararg/possibly nil arguments, then I see no downside to explicitly write it as a curried function with 3 arguments where the last is a table.
Unfortunately, right now coding that would be a pain because of having to nest anonymous function declarations. But that just seems to be simple syntax fix: just have
function(a)(b) return a+b endbe syntax sugar forfunction(a) return function(b) return a+b end end, and so on. This type of change should be easy to make and can only make life easier for programmers.I guess the problem with this is that the way Lua is currently implemented might make the latter more inefficient than
function(a,b) return a+b endwhen callingadd(a)(b)vsadd(a,b). Do you know whether this is true?This is another good point. I haven't thoroughly thought through the details but it seems that currying in imperative languages can be quite tricky to get right.
Why would it point to
curryinstead of the underlying function (e.g. in your code, wouldn't it step intoreturn func(table.unpack(args, 1, args_n))and then error insidefunc)? I agree that if the function was applied partially though that it'd be hard to tell at which step a bad argument was passed.Yeah, although I actually think that
a:fshould be syntax sugar for something else (see my other response in this thread).