复合函数的意思就是:
有函数:
f(x)=x + 1;
g(x)=x * x
则g(f(x)) = (x + 1) * (x + 1)
我们给Ruby的打开Proc类:
有函数:
f(x)=x + 1;
g(x)=x * x
则g(f(x)) = (x + 1) * (x + 1)
我们给Ruby的打开Proc类:
class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
def *(g)
Proc.compose(self, g)
end
end
inc = lambda { |x| x + 1 }
square = lambda{ |x| x * x}
square_inc = square * inc
square_inc(2) #=> 9