For log tracing inside a for
comprehension, I\'ve used dummy assignment like this:
val ll = List(List(1,2),List(1))
for {
outer <
Starting Scala 2.13
, the chaining operation tap, has been included in the standard library, and can be used with minimum intrusiveness wherever we need to print some intermediate state of a pipeline:
import util.chaining._
// val lists = List(List(1, 2), List(1))
for {
outer <- lists
inner <- outer.tap(println)
} yield inner
// List(2, 4, 6)
// List(4, 8, 12)
// ls: List[Int] = List(4, 8, 12)
The tap chaining operation applies a side effect (in this case println
) on a value (in this case the outer
list) while returning this value untouched:
def tap[U](f: (A) => U): A