在Scala中将List [Try [T]]尝试为[List [T]] [英] List[Try[T]] to Try[List[T]] in Scala

查看:131
本文介绍了在Scala中将List [Try [T]]尝试为[List [T]]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Scala中将List [Try [T]]转换为Try [List [T]]?

I would like to know how to convert a List[Try[T]] to Try[List[T]] in Scala?

我尝试过使用累加器并向右折叠,但这似乎并不理想.

I have tried using an accumulator and folding right but it doesn't seem ideal.

推荐答案

使用猫就像这样简单:

import cats._
import cats.data._
import cats.implicits._
import scala.util.{Try, Success, Failure}

val tries: List[Try[Int]] = List(Success(1), Success(2), Success(3))
tries.sequence

遍历文档中的更多信息.

这篇关于在Scala中将List [Try [T]]尝试为[List [T]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆