Ruby反向咖喱:这可能吗? [英] Ruby Reverse Currying: Is this possible?

查看:57
本文介绍了Ruby反向咖喱:这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Ruby 1.9.x中的curring,我已经在某些地方使用它了,并且可以像proc参数一样基本支持默认参数来进行翻译:

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

非常方便,对吧?事情是,我希望能够反向咖喱,这意味着用随机值填充proc的最后一个参数.像这样:

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)

我想要的结果将是3.这将返回-3.

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

推荐答案

我认为没有直接的方法,而您正在做的事情有些狡猾,可能比后向递归更好的解决方案

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

要获得所需的结果,您可以做的是将更多proc包裹在proc周围:

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

这篇关于Ruby反向咖喱:这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆