Scala Academy | Tutorials

The Pipe |> Operator

How to implement and why to use the pipe operator

There is a "val" missing in the video - here's the correct implementation of the operator:

implicit class Pipe[T](val v: T) extends AnyVal {
  def |>[U] (f: T => U) = f(v)
  // Additional suggestions:
  def $$[U](f: T => U): T = {f(v); v}
  def #!(str: String = ""): T = {println(str + v); v}
}
            
Adapted from: http://codereview.stackexchange.com/questions/26707/pipeline-operator-in-scala/26710#26710
Contact Us
Say Hey