M
matt
I searched google groups and couldn't find a decent currying
implementation, so I took a few minutes and wrote one. Here it is,
for anyone who wants it. Any comments (ie if I messed up something I
didn't realize) are welcome
class Proc
alias oldcall call
def curry(&callTest)
@curry, @args, @callTest = true, [], callTest
self
end
def ncurry(n)
curry {|*args| args.length > n}
end
def call(*args)
return oldcall(*args) if @curry.nil?
res, me, pass = @args + args, self, @callTest
return oldcall(*res) if @callTest.call(self, *res)
# the me is just to make things read easier...
p = Proc.new {|*args| me.sendoldcall, *args)}
p.curry(&pass)
p.instance_eval {@args = res}
p
end
def[](*args)
self.call(*args)
end
end
implementation, so I took a few minutes and wrote one. Here it is,
for anyone who wants it. Any comments (ie if I messed up something I
didn't realize) are welcome
class Proc
alias oldcall call
def curry(&callTest)
@curry, @args, @callTest = true, [], callTest
self
end
def ncurry(n)
curry {|*args| args.length > n}
end
def call(*args)
return oldcall(*args) if @curry.nil?
res, me, pass = @args + args, self, @callTest
return oldcall(*res) if @callTest.call(self, *res)
# the me is just to make things read easier...
p = Proc.new {|*args| me.sendoldcall, *args)}
p.curry(&pass)
p.instance_eval {@args = res}
p
end
def[](*args)
self.call(*args)
end
end