Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:
my_proc = proc {|*x| x.max }
1) curry
without arguments: my_proc.curry
. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)
2) curry
with arguments: my_proc.curry(n)
This way, the currying gets applied as if the proc would take n
arguments. For example:
my_proc.curry(3).call(2).call(5).call(1) #=> 5
So, how would you achieve currying with an arbitrary number of arguments? That means, if n
is not given?
One way that comes into my mind is to collect the arguments via proxying call
and then resolve the proc
via method_missing
(if any method other than call
is used / call
is used without arguments, call the proc
with the collected arguments), but I'm still looking for other ways to achieve it.
Update
As Andy H stated, the problem is when to stop currying. For my purposes, it would be ok if the currying stops / the proc evaluates when either any method besides call
is invoked or call
is invoked without arguments.