7得票2回答
Scala: 用于 future for-comprehension 的 ExecutionContext

当我创建一个 `future`,或者应用 `onSuccess` 和 `map` 等方法时,我可以为它们指定 `ExecutionContext`。 例如, val f = future { // code } executionContext f.map(someFunction)...

7得票2回答
一个更好的语法用于从for循环中进行恢复

我有许多功能,它们返回一个由for循环组合而成的Future作为结果,但我需要从可能出现的错误中恢复。标准语法似乎将for循环组合看作是一个中间结果,如下所示: def fooBar(): Future[String] = { val x = for { x <- foo(...

7得票1回答
Scala中的Either类型,以元组作为Right值

假设我有以下代码: val either: Either[String, (Int, Int)] = Right((1,2)) for { (a, b) <- either.right } yield a + b 当我在REPL中对它进行评估时,出现以下错误: :13: 错...

7得票1回答
Scala中for comprehension背后的可索引数据结构

我刚刚完成了在Coursera上Martin Odersky关于Scala的第六周讲座。在第五节课中,他说: “……for循环的翻译不仅限于列表、序列或集合;它仅基于map、flatMap和withFilter方法的存在。” 这使得你也可以将for语法用于自己的类型——你只需要为这些类型定...

7得票4回答
在for循环中如何对Either类型的值进行模式匹配?

我有一个类似于这样的for循环: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } ge...