Is there something like Java Stream's “peek” operation in Scala?

混江龙づ霸主 提交于 2019-12-04 04:20:30

One way to solve this is using implicits:

class Tappable[A](a: A) {
  def tap[U](action: (A) => U): A = {
    action(a)
    a
  }
}

implicit def any2Tappable[A](a: A): Tappable[A] = new Tappable[A](a)

Which can then be used natively:

connection.tap(_.connect()) match {
  case c if c.getResponseCode == 304 => None
  ...

Any other answers?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!