Description
Reviving this old idea that we closed under scala/scala-dev#661.
Motivation
-
Currently
andThen
defined for Futures, doesn't preserve the error behavior. We could argue that if the side-effecting code throws an error, theFuture
is ought to fail, as @NthPortal previously pointed out, preserving the behavior inIterator
andLazyList
-
More consistent interface for all Monadic Product types (Option, List, all other Iterables etc.). If you want to do something like
Future|Option|Try .log(s"logging the value $_").map(...).log(...)
You either need to put F|O|T.map(r => { log(....) r})
every where, which is rather ugly,
Or you need to define extension methods on all them
implicit class MonadicLog[+B <: Product, A] (val u: B){
def log(l: String): B = {
log(l)
u
}
}
implicit class FutureLog[T, A](val u: Future[T]){
def log(l: String) : Future[T] = {
println(l)
u
}
}
And so on ....
with a consistent tapEach
operation, you can do the following, which is much better IMO
Future.successful(List(Some(1), Some(2), Some(3), Some(4)))
.tapEach(println)
.map(_.flatten
.tapEach(println)
.filter(_ > 1)
).tapEach(println)
I have a draft PR for this, I will push that for everyone to see what this would look like.
Open to comments :)