Skip to content

Instantly share code, notes, and snippets.

@mnicky
Created May 4, 2021 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnicky/2e53dab16de3e18913705a4080adcaf5 to your computer and use it in GitHub Desktop.
Save mnicky/2e53dab16de3e18913705a4080adcaf5 to your computer and use it in GitHub Desktop.
Chaining implicits for Scala < 2.13
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