gpt4 book ai didi

scala - View 可以与并行集合一起使用吗?

转载 作者:行者123 更新时间:2023-12-03 12:20:29 24 4
gpt4 key购买 nike

在集合映射中查找结果的惯用法是这样的:

list.view.map(f).find(p)

其中 listList[A]fA => BpB => Boolean

是否可以将 view与并行集合一起使用?我问是因为我得到了一些非常奇怪的结果:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val f : Int => Int = i => {println(i); i + 10}
f: Int => Int = <function1>

scala> val list = (1 to 10).toList
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> list.par.view.map(f).find(_ > 5)
1
res0: Option[Int] = Some(11)

scala> list.par.view.map(f).find(_ > 5)
res1: Option[Int] = None

最佳答案

请参阅"A Generic Parallel Collection Framework",这是Martin Odersky等人的论文,其中讨论了新的并行集合。第8页上的“并行 View ”部分讨论了如何将viewpar一起使用,以及如何给 View 和并行计算带来性能优势。

至于您的特定示例,那肯定是一个错误。 exists方法也会中断,并且在一个列表上中断会导致所有其他列表中断,因此我认为这是一个可能会部分中止的操作(一旦有答案,findexists可以停止)的问题以某种方式破坏线程池。它可能与the bug with exceptions being thrown inside functions passed to parallel collections有关。如果是这样,则应将其固定在2.10中。

关于scala - View 可以与并行集合一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8470490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com