Created
May 4, 2021 15:08
-
-
Save mnicky/2e53dab16de3e18913705a4080adcaf5 to your computer and use it in GitHub Desktop.
Chaining implicits for Scala < 2.13
This file contains hidden or 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
package utils | |
object ChainingImplicits { | |
/** | |
* Defines chaining operators similar to pipe() and tap() from Scala 2.13. | |
* | |
* Example usage: | |
* <pre> | |
* import utils.ChainingImplicits._ | |
* aThing | |
* .pipe(x => transformation(x)) | |
* .tap(x => sideEffect(x)) | |
* .anotherOperation() | |
* </pre> | |
* | |
* Source: <a href="https://github.com/scala/scala/blob/v2.13.0/src/library/scala/util/ChainingOps.scala">Scala 2.13</a> | |
*/ | |
implicit class ChainingOps[A](value: A) { | |
def pipe[B](f: A => B): B = f(value) | |
def tap[B](f: A => B): A = { f(value); value } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment