0

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))
        }
Rakshith
  • 644
  • 1
  • 8
  • 24
Tom
  • 83
  • 8

1 Answers1

1

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]])
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • Which package defined EitherT.rightT ? I use import cats.data.EitherT, but does not have rightT – Tom Jul 16 '19 at 06:50
  • Yes that should be the correct import: https://typelevel.org/cats/datatypes/eithert.html#from-a-or-b-to-eithertf-a-b You will also have to follow Bogdan’s suggestion and change the operator. – Mario Galic Jul 16 '19 at 07:40