Currying in Ruby

Currying in Ruby

Ruby defines curry for Method and Proc, allowing procs to return partially applied procs when they get called with fewer than the required number of arguments. For example:

multiply = -> x,y { x * y }.curry
#=> #<Proc:0x007fed33851510 (lambda)>
multiply[2,3]
#=> 6
double = multiply[2]
#=> #<Proc:0x007fed35892888 (lambda)>
double[3]
#=> 6

Note: While Proc#curry has been around since Ruby 1.9, Method#curry was only added in Ruby 2.2.0. For versions before 2.2.0, you will first need to convert your method object to a proc via Method#to_proc.

Check out the Ruby docs for more details.

原文出处

如果觉得我的文章对您有用,请在支付宝公益平台找个项目捐点钱。 @Victor Jan 24, 2015

奉献爱心