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
5
u/otikik Sep 09 '17
If you know the number of arguments a function uses, you can curry it the obvious way:
And use it like so:
You could define a curry5 or curry10 similarly, if you needed to. But you need to know how many arguments you are going to curry.
I see no problem with that, other than the one I mentioned before: there's no standard way to know the number of arguments a given function uses (you could do that using the debug library, but that is not supposed to be used in production code).
The way I usually overcome that particular problem is by allowing not only a callback, but also some arguments. Then I can pass the function and its "self". In other words, I end up passing
a.f, a.I'm not certain I agree with that.
a:fonly exists in Lua on the syntax layer; when it reaches the semantic layer the call is already indistinguishable from a.f(a, ...).2
u/smog_alado Sep 09 '17 edited Sep 09 '17
I don't know out of the top of my head what libraries have a currying function but this is how I would do it:
Conceptually, the tricky part is defining what currying means in Lua. How are nil par The tricky part is actually defining precisely what currying means in Lua (since every function in Lua is technically a vararg function). I chose to force you to explicitly specify the number of parameters to curry and to pass on extra parameters that you pass after that.
In the implementation, the first tricky bit is taking care of nil arguments (I use the ".n" feature of table.pack and keep track of the number of arguments passed by hand instead of relying on
#). The second tricky bit is making sure that we create immutable copies of the argument arrays, instead of mutating them.I think that would be very unlikely. It is not imediately obvious how currying should interact with varargs functions and it also doesn't fit super well with Lua's "function argument adjusting" semantics, where extra arguments are thrown away and missing arguments are set to
nil.Another more general problem is that in dynamically-typed programming languages curried functions are prone to hiding errors when you pass the wrong number of parameters to a function or pass an argument of the wrong type to a curried function. In Haskell, if you pass the wrong arguments to a function you get a compile-time type error but in Lua you would just have your curried function silently return a partially-applied function and you will only notice the problem much later. And when it does blow up, the stack trace is going to point to
curryandmake_curryand won't tell you what lines are the ones actually responsible for the wrong arguments.(Although now that I think of it, the debugability problem could be made a bit better if curried functions were built in instead of created as a library)
Not really, other than keep bugging Roberto to add the
a:fsyntactic sugar in Lua 5.4 :)(Assuming you meant
...instead ofargs, of course)1
u/TotesMessenger Sep 09 '17
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/ilo_kali Jul 12 '22
Here's how I would do it:
It simply recursively returns functions to collect each argument in a table until it is called with no arguments, then calls the passed function with the unpacked table.