Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 为什么这份未来清单要向未来清单转换编译并运行?_Scala_Future_For Comprehension - Fatal编程技术网

Scala 为什么这份未来清单要向未来清单转换编译并运行?

Scala 为什么这份未来清单要向未来清单转换编译并运行?,scala,future,for-comprehension,Scala,Future,For Comprehension,免责声明:下面的代码片段与正在进行的Coursera课程之一有关。 让我们考虑它只是为了学习目的而张贴,不应该用来作为一个家庭作业的解决方案提交。 正如下面的评论所述,我们需要将未来列表转换为列表的单一未来。更重要的是,如果至少有一个输入期货失败,那么最终的期货就会失败 我遇到了以下实现,我不完全理解它 /** Given a list of futures `fs`, returns the future holding the list of values of all the future

免责声明:下面的代码片段与正在进行的Coursera课程之一有关。 让我们考虑它只是为了学习目的而张贴,不应该用来作为一个家庭作业的解决方案提交。 正如下面的评论所述,我们需要将未来列表转换为列表的单一未来。更重要的是,如果至少有一个输入期货失败,那么最终的期货就会失败

我遇到了以下实现,我不完全理解它

/** Given a list of futures `fs`, returns the future holding the list of values of all the futures from `fs`.
 *  The returned future is completed only once all of the futures in `fs` have been completed.
 *  The values in the list are in the same order as corresponding futures `fs`.
 *  If any of the futures `fs` fails, the resulting future also fails.
 */
def all[T](fs: List[Future[T]]): Future[List[T]] = 
             fs.foldRight(Future(Nil:List[T]))((f, fs2) =>
  for {
    x <- f
    xs <- fs2
  } yield (x::xs))
/**给定一个期货“fs”列表,返回包含“fs”中所有期货值列表的期货。
*只有在“fs”中的所有期货完成后,才能完成返回的期货。
*列表中的值的顺序与相应的未来“fs”的顺序相同。
*如果任何一个期货“fs”失败,那么由此产生的期货也会失败。
*/
def all[T](fs:List[Future[T]]):Future[List[T]=
未来(无:列表[T])((f,fs2)=>
为了{
x1)假设f是未来[T]
,然后写

for {
 t <- f
}  yield List(t)
2) 如果你的未来f包含一个失败,那么理解的未来f将只返回这个失败的未来,而不是执行屈服


一般来说,Scala中的理解只是一种糖分,可以用
map、flatMap、filter、foreach
重写。我是一个说英语的右撇子,所以通常我会折叠起来,但折叠的每一步看起来都像:

Fn flatMap ((x: T) => Fs map (xs => x :: xs))
您的值是
x

该功能在成功时应用,这解释了为什么失败会使您停止工作:

scala> timed(Await.ready(all(List(Future{Thread sleep 5*1000; 1},Future(2),Future{Thread sleep 10*1000; 3})), Duration.Inf))
res0: (Long, scala.concurrent.Awaitable[List[Int]]) = (10002419021,scala.concurrent.impl.Promise$DefaultPromise@2a8025a0)

scala> timed(Await.ready(all(List(Future{Thread sleep 5*1000; 1},Future(???),Future{Thread sleep 10*1000; 3})), Duration.Inf))
res1: (Long, scala.concurrent.Awaitable[List[Int]]) = (5000880298,scala.concurrent.impl.Promise$DefaultPromise@3750d517)
请注意,出现故障的版本会短路

请参阅ScalaDoc for flatMap以了解这两位信息

编辑:我说得很谨慎,因为这是Coursera的工作,但更明确地说,这一要求没有得到满足:“只有在
fs
中的所有期货都完成后,返回的期货才会完成。”

这里已经给出了答案:
scala> timed(Await.ready(all(List(Future{Thread sleep 5*1000; 1},Future(2),Future{Thread sleep 10*1000; 3})), Duration.Inf))
res0: (Long, scala.concurrent.Awaitable[List[Int]]) = (10002419021,scala.concurrent.impl.Promise$DefaultPromise@2a8025a0)

scala> timed(Await.ready(all(List(Future{Thread sleep 5*1000; 1},Future(???),Future{Thread sleep 10*1000; 3})), Duration.Inf))
res1: (Long, scala.concurrent.Awaitable[List[Int]]) = (5000880298,scala.concurrent.impl.Promise$DefaultPromise@3750d517)