Traditional assignment based programs read right to left. You pull something to a variable on the left. You do something more and pull to another variable on the next line, and then another assignment to pull to another variable.
i = 1
j = i + 1
k = j + 2
Functional programming that encourages the use of operators like map, flatMap, filter reads left to right
Seq(1,2,3)
.filter(_ % 2 == 0)
.map(_ * 2)
Data flows to right from the sequence to the filter, which comes out to the right with just the number 2, which comes out doubled.
Thrush operator
Thrush operator is designed to favor this natural left to right flow in functional programming. This is what it looks like
|>
It lets you push an argument into a function.
object MyClass {
def echo(x: String) = println(x) implicit class PipeEverything[A](val underlying: A) extends AnyVal {
def |>[B](f: A => B) = f(underlying)
} def main(args: Array[String]) {
"Can you hear me?" |> echo
}
}Can you hear me?
This is the usage.
"Can you hear me?" |> echo
The name “thrush” combinator apparently came from the book “To mock a mocking bird” which uses various bird names for various combinators.