【发布时间】:2013-05-24 19:10:13
【问题描述】:
Ruby 有一个方法可以让我们观察值的管道,而无需修改底层值:
# Ruby
list.tap{|o| p o}.map{|o| 2*o}.tap{|o| p o}
Scala中有这样的方法吗?我相信这被称为 Kestrel Combinator,但不能确定。
【问题讨论】:
-
你谷歌 scala kestrel 组合器了吗?这有帮助吗? stackoverflow.com/a/9673294/1339987
-
@djechlin 不,在“模拟一只知更鸟”中搜索并写了 Kestrel,但忘了谷歌,呃。正是我想要的。
-
有also whole blogpost dedicated to the use of the kestrel in scala。不幸的是,scala 没有这种开箱即用的东西。
-
@FrançoisBeausoleil 我听不懂你的讽刺 - 有真的有帮助吗?
-
作为参考,2.10 中使用 Ruby 表示法的惯用版本为
implicit class TapAnyone[A](val value: A) extends AnyVal { def tap[B](f: A => B) = { f(value); value } }