In Java you can call peek(x -> println(x))
on a Stream and it will perform the action for each element and return the original stream, unlike foreach which is Unit. Is there something similar in Scala, ideally something which works on all Monady types, allowing you to "pass through" the original Monad while performing a side effect action? (Logging, e.g.)
It is of course easily implemented:
def tap[A, U](a: A)(action: (A) => U): A = {
action(a)
a
}
but I'm hoping for something a bit more elegant or idiomatic.