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.
peek can get the smallest item directly compared with stream. foreach().
peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack.
Java Stream peek() Java Stream peek() method returns a new Stream consisting of all the elements from the original Stream after applying a given Consumer action. Note that the peek() method is an intermediate Stream operation so, to process the Stream elements through peek() , we must use a terminal operation.
This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline. Since Java 9, if the number of elements is known in advance and unchanged in the stream, the . peek () statement will not be executed due to performance optimization.
One way to solve this is using implicits:
class Tappable[A](a: A) {
def tap[U](action: (A) => U): A = {
action(a)
a
}
}
implicit def any2Tappable[A](a: A): Tappable[A] = new Tappable[A](a)
Which can then be used natively:
connection.tap(_.connect()) match {
case c if c.getResponseCode == 304 => None
...
Any other answers?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With