- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有一个 Future[Either[String, Int]]
表示可能的错误消息( String
)或成功的计算( Int
),移动 Future
很简单的潜在故障作为错误消息进入左侧:
def handleFailure(fe: Future[Either[String,Int]]) =
f.recover({ case e: Exception => Left(s"failed because ${e.getMessage}"))
我希望
EitherT
存在类似的东西,但也许我就是不知道它叫什么。它相对简单,但涉及拆箱和重新装箱这感觉笨拙的EitherT:
def handleFailureT(fe: EitherT[Future, String, Int]) =
EitherT(handleFailure(et.value)) // See above for handleFailure definition
猫确实添加了
MonadError
实例 a
while ago ,但它专门用于直接恢复到任何人的
right
, 不是为了替换Either 本身。
handleFailureT
实现它 Cats,如果是,它叫什么?
class EitherT[F, A, B] {
def handleFailureT[AA](f: PartialFunction[Throwable, AA]): EitherT[F, AA, B] = EitherT(value.recover(f))
}
// Then used like:
val et: EitherT[Future, String, Int] =
EitherT(Future(throw new Exception("an error message"))
et.handleFailure {
case e: Exception => s"Got error with message '${e.message}'"
}
// Now et is Left("Got error with message 'an error message'")
最佳答案
这一点都不明显,但我认为这就是 attempt
和 attemptT
是给。例如:
val myTry: Try[Int] = Try(2)
val myFuture: Future[String] = Future.failed(new Exception())
val myTryET: EitherT[Try, Throwable, Int] = myTry.attemptT
val myFutureET: EitherT[Future, Throwable, String] = myFuture.attemptT
// alternatively
val myFutureET: EitherT[Future, Throwable, String] = EitherT(myFuture.attempt)
有一个 PR 将此添加到文档中:
https://github.com/typelevel/cats/pull/3178 -- 但它目前没有出现在文档中。但是,您可以在这里看到它:
https://github.com/typelevel/cats/blob/master/docs/src/main/tut/datatypes/eithert.md#from-applicativeerrorf-e-to-eithertf-e-a
关于scala - 将潜在的 future 恢复到 Cats'EitherT's Left 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54931204/
如何从表中选择结果,其中 cat = '$cat',如果不相等,则从 cat 中选择所有结果,无论 $cat 是什么。这是我的查询 $sql = mysql_query("SELECT * FROM
如标题中的示例,我想要结果: 目录:home/tom/cat 第一次剪辑:tom/cat 第二次切割:/cat 最后一刀:/ 如何在 python 中实现? 最佳答案 这是您要找的吗: In [101
使用循环,我可以创建 My cat is: Cat1 ... My cat is: Cat1 但是,当我尝试使用 Cat ("cat"+i) = new Cat("Cat" + i); 我犯错了.
我目前正在用 C 和 unix 开发一个 cat 命令。这次我的任务是设计 cat -b 和 cat -s 命令。但是我已经创建了其他命令,例如 -t、-v、-n、-e。我卡在了命令 -b 中,因为每
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我有一个程序可以同时读取两个输入文件。我想从标准输入中读取这个程序。我以为我会使用这样的东西: $program1 <(cat) <($program2) 但我刚刚发现 cat <(cat) 产生 .
管道的正式定义指出左侧文件的 STDOUT 将立即通过管道传输到右侧文件的 STDIN。我有两个文件,hello.txt 和 human.txt 。 cat hello.txt 返回 Hello 并且
我想编写一个 shell 脚本,它接受来自标准输入的数据,将它写入一个文件,然后用它做一些事情。 出于这个问题的目的,让我们假设我的脚本应该接受标准输入,将其写入 in.txt ,然后 grep 一个
有没有办法让cat , less等打印制表符而不是制表符被转换为空格?当我将代码从终端复制到编辑器时,我对此很恼火。 最佳答案 我在这里看到两个问题。 首先,目标编辑器可以将 TAB 转换为空格数。一
我刚刚发现执行 find . 比执行 find 慢。 |猫。这是在我的主目录中执行 time find . 3 次的结果: First: real 0m4.385s user 0m0.54
我相信该程序适用于除一个以外的大多数情况。我在 indexOF() 中添加了空格,所以像 cathrine 和 dogsuit lammas 这样的词都会被认为是非亵渎的。我看到的唯一问题是用户是否以
解决方案: 不支持 data-* 属性。您可以定义自己的属性并使用 getAttribute 调用它们。就我而言, this.dataset.cat 变为 this.getAttribute("cat
这是让我“我该怎么办?”的问题之一 这是我遇到的编译器错误: 在“SKFootmanSprite”类型上使用实例成员“getAttackUPSequence_Frames”您是否打算改用“SKFoot
我想将 2 个不同的 cat 语句合并为一个 cat 语句 cat /dir1/file1.cfg | grep -q "linux" cat /dir1/file2.cfg | grep -q "l
我已经在 C 中使用系统调用(打开、读取和写入)来模拟 Linux 系统中的“猫”功能,并且它比真实的慢... 我正在使用与真正的“cat”相同的缓冲区大小,并使用“strace”我认为它进行相同数量
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我有一个包含列的表:(project_id, name) 这是一个人员列表,每个人都有其所属的项目。如果一个人在两个项目中,则重复。 我想提取一个包含以下列的表:(project_id, people
我想提取 git 存储库中保存的文件的最新版本的副本,并将其传递到脚本中进行某些处理。对于 svn 或 hg,我只使用“cat”命令: Print the specified files as the
我是一名优秀的程序员,十分优秀!