|> Operator in Scala
Nicolas A Perez
9510

Please do not be misled by this article. The terminology is wrong and implementation is poor. Here is a better and simpler implementation that transforms Scala’s syntax at compile time with no runtime overhead:

implicit class Pipe[A](val it: A) extends AnyVal
{ def |>[B](f: A => B) = f(it) }

The pipeline operator is not function composition, it is function application. The difference between .map and |>, is that .map applies a function to the elements of a collection, while |> applies a function to the collection itself.

You can use it as follows:

Array(1,2,3,4,5) |> Sorting.quickSort

instead of:

Sorting.quickSort(Array(1, 2, 3, 4, 5))

For mapping in Scala, you can just write this anyway without |>:

Seq(1, 2, 3, 4, 5) map (_ + 1)
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.