Last active
January 23, 2023 12:42
-
-
Save carlosedp/7a009a7e4b524ebe9b668a4d6684fef2 to your computer and use it in GitHub Desktop.
Pipeline operators in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S scala-cli shebang | |
//> using scala "3" | |
/** Test functions */ | |
val triple = (x: Int) => 3 * x | |
val half = (x: Int) => x / 2 | |
val sum = (x: Int) => (y: Int) => x + y | |
/** with extension infix */ | |
// extension [A, B](a: A) | |
// infix def |>(f: A => B): B = f(a) | |
/** or with inline */ | |
extension [A](a: A) | |
inline def |>[B](inline f: A => B): B = f(a) | |
/** This works on Scala 2 while `extension` is Scala 3 only */ | |
// import scala.language.implicitConversions | |
// implicit class Piper[A](val a: A) { | |
// import scala.util.chaining._ | |
// implicit def |>[B](f: (A) => B): B = a.pipe(f) | |
// } | |
/** Instead of */ | |
val classic = half(sum(triple(3))(2)) | |
println(s"Classic result: $classic") | |
/** Pipe the functions */ | |
val piped = 3 |> triple |> sum(2) |> half // Uses defined extension |> | |
println(s"Piped result: $piped") | |
/** or using scala.util.chaining */ | |
import scala.util.chaining._ | |
val utilpiped = 3 pipe triple pipe sum(2) pipe half // or | |
val chaining = 3.pipe(triple).pipe(sum(2)).pipe(half) | |
println(s"Scala util.piped result: $utilpiped") | |
println(s"Chaining result: $chaining") | |
/** finally aliasing pipe (just using ||> to avoid conflicts with above examples)*/ | |
extension [A,B](a: A) inline def ||>(inline f: (A) => B): B = a.pipe(f) | |
val aliased = 3 ||> triple ||> sum(2) ||> half | |
println(s"Scala aliased util.piped result: $aliased") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment