as opposed to having to assign part of the expression to a val, like so:
val part1 = getResults
.groupBy(...)
.filter(...)
println(part1)
part1
.map(...)
.foreach(...)
Basically it allows you to see the value of any expression in your program at any point, without having to do a lot of refactoring.
I find myself having to redefine this for each new project but also since it’s for debugging I don’t necessarily want to commit it either; maybe the standard library is the right place? I think many others use this pattern also.
And you might also say, “Shouldn’t you be using a debugger anyway?”, but in a debugger you can’t see the result of part of an expression, only the whole expression, so .tap works very nicely in those situations, which in Scala, happen quite often.
This feels like something that can become a small library with zero dependencies, if this feature doesn’t exist in one the existing logging libraries.
I think it’s better in a library because I can think of several features (positioning, argument name via reflection, etc.) that people may want to have and something that can be easily evolved, unlike the Scala standard library.
90% of the times when I want to use tap it’s temporary. Adding an import and having to clean it up afterwards is usually more annoying than introducing a local val… It should really be in scala or Predef IMO (“part of the language” in a sense).