Unlock the Power of Scala's Chaining Utils: A Guide to Tap and Pipe

Search for a command to run...

No comments yet. Be the first to comment.
Go's race detector samples a schedule and hopes. GALA makes a value that isn't safe to share across goroutines a build failure.
June's 0.56 -> 0.62 releases: bind/also monadic do-notation, applicative validation with Validated, and concurrent binds over Future.
Importing a third-party Go module through Bazel and rules_gala - with concrete types across the seam, verified hands-on.
From rules welded inside the language repo to a standalone, registry-published rules_gala with a real toolchain and Gazelle BUILD generation.
Eleven weeks, ~55 releases, and a new tagline: GALA hits 0.50.0 with cross-package generics, an LSP, and four new stdlib packages.
There is an amazing util in Scala that is available in scala.util.chaining._. It adds two implicit methods to any scala object that allow chain commands (pipe) and apply side effects (tap).
import scala.util.chaining._
trait StatsCounter {
def incr(counterName: String): Unit
}
object Solution extends App {
val value = 123
val counter: StatsCounter = ... // implementation
stringValue(value)
def stringValue(v: Int): String = {
// tap adds pure side effect without changing input value
v.toString.tap {
result => counter.incr(result)
}
// this code does the same without chaining
// val result = v.toString
// counter.incr(result)
// result
}
}
def calculate(value: Int) = {
value
.pipe(_ + 100)
.pipe(Some(_))
}
calculate(100) // returns Some(200)
def calculate(value: Int) = {
value
.tap(println)
.pipe(_ + 100)
.tap(println)
.pipe(Some(_))
.tap(println)
}
calculate(100)
/*
returns Some(200)
prints:
100
200
Some(200)
*/
Scala's chaining utils provide a powerful and efficient way to streamline code by enabling the use of tap and pipe methods. By leveraging these methods, developers can optimize their functional code, enhance readability, and improve overall productivity.