Idiomatic Way to Avoid Using a Filter Twice

You can define this scoped operator yourself as follows.

implicit class ScopedSyntax[A](private val a: A) extends AnyVal {
  def scoped[B](f: A => B): B = f(a)
}

Actually this is a pretty well known pattern that many people would like to have in the standard library. Most people call this operator pipe or |>.

2 Likes