Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Reverse Currying: Is this possible?

Concerning currying in Ruby 1.9.x, I've been using it in some places, and can be translated like basically supporting default parameters to the proc arguments:

p = proc {|x, y, z|x + y + z}
p.curry[1] #=> returns a lambda
p.curry[1, 2] #=> returns a lambda
p.curry[1, 2, 3] #=> 6
p2 = p.curry[1, 2]
p2.(2) #=> 5
p2.(4) #=> 7

very handy, right? thing is, I would like to be able to curry in reverse, that means, fill the last argument of my proc with a random value. Like this:

p = proc{|x, y| x - y }.curry[1]
p.(4)

my desired result would be 3. this returns -3.

like image 559
ChuckE Avatar asked Nov 12 '22 18:11

ChuckE


1 Answers

i think there's no direct way of doing that and what you're doing is a bit dodgy, there probably is better solution to your problem than back-currying

what you could do to achieve desired result is wrap more procs around your procs:

p = proc{|x, y| x - y}
q = proc{|y, x| p[x, y]}
q.curry[1].(4)

in fact you can reorder arguments any way you want but believe me it gets messy very quickly

like image 53
keymone Avatar answered Jan 04 '23 03:01

keymone