简体   繁体   中英

How to set initial value for foldLeft When Using EitherT[Future, Failure, Option[B]]

I am using EitherT[Future, Failure, Option[B]] in the following function which does foldLeft . How to set Initial value for foldLeft in the code below ?

    def doWork[A, B](seq: Set[A])(f: A => EitherT[Future, Failure,  Option[B]]): EitherT[Future, Failure, Set[Option[B]]] = 
        seq.foldLeft(????? how to set this initial value here ????) {
          case (acc, nxt) => acc.flatMap(bs => f(nxt).map(b => bs :+ b))
        }

Try

EitherT.rightT[Future, Failure](Set.empty[Option[B]])

like so

def doWork[A, B](seq: Set[A])(f: A => EitherT[Future, Failure,  Option[B]]): EitherT[Future, Failure, Set[Option[B]]] =
  seq.foldLeft(EitherT.rightT[Future, Failure](Set.empty[Option[B]])) {
    case (acc, nxt) => acc.flatMap(bs => f(nxt).map(b => bs + b))
  }

This is a slightly shorter version of Bogdan's suggestion

EitherT[Future, Failure, Set[Option[B]]](Future.successful(Right(Set())))
EitherT.rightT[Future, Failure](Set.empty[Option[B]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM