Haskell's Prelude has a useful function that swaps a function's arguments: http://zvon.org/other/haskell/Outputprelude/flip_f.html
I need to do the same in Ruby. Instead of just defining a custom method, I would like to monkey patch the Proc
class so that I could use flip
alongside Proc#curry
. Something like
f = lambda {|x, y| [x, y]}
g = f.flip.curry.(2)
to supply a value for y
.
I do not know how to reopen Proc
class to do that.
class Proc
def flip
lambda { |x, y| self.(y, x) }
end
end
f = lambda { |x, y| [x, y] }
f.flip.(1, 2)
#=> [2, 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With