Haskell 的 Prelude 有一个有用的函数,可以交换函数的参数:http://zvon.org/other/haskell/Outputprelude/flip_f.html
我需要在 Ruby 中做同样的事情。我不想仅仅定义一个自定义方法,而是想猴子修补 Proc
类,以便我可以将 flip
与 Proc#curry
一起使用。有点像
f = lambda {|x, y| [x, y]}
g = f.flip.curry.(2)
为 y
提供一个值。
我不知道如何重新打开 Proc
类来做到这一点。
最佳答案
class Proc
def flip
lambda { |x, y| self.(y, x) }
end
end
f = lambda { |x, y| [x, y] }
f.flip.(1, 2)
#=> [2, 1]
关于ruby - 需要为 Proc 实现 Haskell 的翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979814/