Skip to content

TapEach For Futures and Try  #44

Open
@sinanspd

Description

@sinanspd

Reviving this old idea that we closed under scala/scala-dev#661.

Motivation

  1. Currently andThen defined for Futures, doesn't preserve the error behavior. We could argue that if the side-effecting code throws an error, the Future is ought to fail, as @NthPortal previously pointed out, preserving the behavior in Iterator and LazyList

  2. 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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestlibrary:concurrentstatus:pendingThis enhancement request lacks consensus on whether or not to include it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions