Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to curry a method?

Tags:

ruby

consider this code

def f x, y
  x + y
end

g = lambda(&method(:f)).curry.(1)

g.(2) # => 3

the expression for g is too hard to read. Can it be simplified?

like image 552
akonsu Avatar asked Dec 20 '12 18:12

akonsu


Video Answer


1 Answers

If you are using Ruby 2.2.0 or later, you can use Method#curry:

def f(x, y)
  x + y
end
g = method(:f).curry[1]
p g[2] # => 3
like image 164
David Grayson Avatar answered Sep 19 '22 18:09

David Grayson