3 ms·
Ah, I understand what you mean now. You are looking indeed for the thrush operator. I think it should be built into Scala's standard library, but until that hap
by valenterry 6y ago
Ah, I understand what you mean now. You are looking indeed for the thrush operator. I think it should be built into Scala's standard library, but until that happens, you can use the mouse library or build it yourself. Here is an example:
// Need to define this once somewhere in your project
implicit class TrushExtension[A](anything: A) {
def |>[B](function: A => B) = function(anything)
}
// Your application code
def fun(arr: Iterable[Int]) = arr.map(_.toDouble / Math.PI)
val arr = 0 to 100
val newArr = arr.map(_*2) |> fun
newArr.foreach(println) // prints 0, 0.63662, 1.27324, ...
Execute or change the code here: https://scalafiddle.io/sf/WAKhZtJ/0 https://scalafiddle.io/sf/WAKhZtJ/0
This is maybe not exactly as convenient, but it comes pretty close.
"someA.someB.someC" becomes "someA |> someB |> someC".
- tastyminerals2 6y agoLooks good! Thanks for the tip.
- scns 6y agoIf you would call it pipe operator, like it's called in many (Elm, Elixir, F#, OCaml) other languages that have it, people would understand you faster IMHO. It is even discussed for inclusion into JavaScript.
- dwijnand 6y agohttps://www.scala-lang.org/api/current/scala/util/ChainingOps.html#pipe[B](f:A=%3EB):B https://www.scala-lang.org/api/current/scala/util/ChainingOp...
- valenterry 6y agoNice I didn't know that! I still like to add |> on top of it. "foo |> bar |> baz" just visually parses easier than "foo pipe bar pipe baz"