5

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.

ChuckE
  • 5,610
  • 4
  • 31
  • 59

1 Answers1

1

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

keymone
  • 8,006
  • 1
  • 28
  • 33
  • humm, looks clean enough, going to give it a try. The use case is to replicate in a certain place the same behaviour you get from having default arguments on methods, and these can only be set from the last argument backwards. I wanted to take the same method and replace the default argument by a different value in a different context. – ChuckE Nov 05 '12 at 14:42