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:
Contact Us
Say Hey