Gun*_*ars 8 ruby function-composition
只是想知道是否有一个语法快捷方式,用于获取两个proc并加入它们,以便将一个的输出传递给另一个,相当于:
a = ->(x) { x + 1 }
b = ->(x) { x * 10 }
c = ->(x) { b.( a.( x ) ) }
Run Code Online (Sandbox Code Playgroud)
有喜欢的东西时,这会派上用场method(:abc).to_proc,并:xyz.to_proc
更多的糖,不是生产代码中真正推荐的
class Proc
def *(other)
->(*args) { self[*other[*args]] }
end
end
a = ->(x){x+1}
b = ->(x){x*10}
c = b*a
c.call(1) #=> 20
Run Code Online (Sandbox Code Playgroud)