I would like to know how to convert a List[Try[T]] into Try[List[T]] in Scala?
I have tried using an accumulator and folding right but it do
I would also recommend just using Cats...
But, if you do not want to add another (big) dependency to your project, just for one function.
You can implement it your self! - (cats implementation may be better)
import scala.util.{Try, Success, Failure}
def sequence[A](list: List[Try[A]]): Try[List[A]] = {
@annotation.tailrec
def loop(remaining: List[Try[A]], acc: List[A]): Try[List[A]] =
remaining match {
case Nil => Success(acc.reverse)
case Success(a) :: tail => loop(remaining = tail, acc = a :: acc)
case Failure(e) :: _ => Failure(e)
}
loop(remaining = list, acc = List.empty)
}
Also, if you may use traverse instead of sequence if you did a map just before.
def traverse[A, B](list: List[A])(f: A => Try[B]): Try[List[B]] = {
@annotation.tailrec
def loop(remaining: List[A], acc: List[B]): Try[List[B]] =
remaining match {
case Nil => Success(acc.reverse)
case head :: tail => f(head) match {
case Success(b) => loop(remaining = tail, acc = b :: acc)
case Failure(e) => Failure(e)
}
}
loop(remaining = list, acc = List.empty)
}
Anyways, Cats (and FP in general) is very useful (as you have just seen).
Thus, I would recommend you to give it a try.