Ruby lambda version
class Proc def curry(&block) lambda {|*args| call(*block[*args]) } endendmurry = lambda {|i, j, *k| i * j}# p murry[5, 2]multiply = murry.curry {|*k| k + [2]}# => Proc, lambda {|*args| call(*block[*args])}; block# => Proc, lambda {|*k| k + [2]}; k.class is Array.p multiply[5]# proc[] is synonym for proc.callp multiply[5, 3]# output 5 * 3 = 15, not 5 * 2# step explain double[5]p lambda {|*k| k + [2]}[5]p murry.call(*[5, 2])p lambda {|*k| k + [2]}[5, 4, 3]p murry.call(*[5, 4, 3, 2]) # 5 * 4 |
javascript version
console.log(function() { return arguments[0] * arguments[1];}.apply(null, function() { return [arguments[0]].concat(2);}(5))); |
Ruby version
class Object def curry(new_name, old_name, &args_munger) ([Class, Module].include?(self.class) ? self : self.class).class_eval do define_method(new_name) { |*args| send(old_name, *args_munger[args]) } end endendclass Value def initialize(value) @value = value end def *(other) @value * other end curry(:double, :*) { [2] } curry(:triple, :*) { |args| args.unshift(3) }endfive, howdy = Value.new(5), Value.new("Howdy ")puts five * 2 # => 10puts five.double # => 10puts five.triple # => 15puts howdy.triple # => "Howdy Howdy Howdy " |