如何将两个过程合二为一? [英] How to combine two procs into one?

查看:50
本文介绍了如何将两个过程合二为一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有一个语法快捷方式来获取两个过程并将它们连接起来,以便将一个的输出传递给另一个,相当于:

Just wondering if there's a syntax shortcut for taking two procs and joining them so that output of one is passed to the other, equivalent to:

a = ->(x) { x + 1 }
b = ->(x) { x * 10 }
c = ->(x) { b.( a.( x ) ) }

这在处理诸如 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

这篇关于如何将两个过程合二为一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆