scala中有快速并发语法糖的实现吗?例如。映射减少

发布于 2024-08-30 18:32:16 字数 389 浏览 6 评论 0原文

与演员传递信息很棒。但我想要更简单的代码。

示例(伪代码)

val splicedList:List[List[Int]]=biglist.partition(100)
val sum:Int=ActorPool.numberOfActors(5).getAllResults(splicedList,foldLeft(_+_))

spliceIntoParts 将一个大列表变成 100 个小列表的 numberofactors 部分,创建一个使用 5 个参与者的池,并在作业完成后接收新作业 getallresults 使用列表上的方法。所有这一切都是通过在后台传递消息来完成的。其中可能是 getFirstResult,计算第一个结果,并停止所有其他线程(例如破解密码)

Passing messages around with actors is great. But I would like to have even easier code.

Examples (Pseudo-code)

val splicedList:List[List[Int]]=biglist.partition(100)
val sum:Int=ActorPool.numberOfActors(5).getAllResults(splicedList,foldLeft(_+_))

where spliceIntoParts turns one big list into 100 small lists
the numberofactors part, creates a pool which uses 5 actors and receives new jobs after a job is finished
and getallresults uses a method on a list. all this done with messages passing in the background. where maybe getFirstResult, calculates the first result, and stops all other threads (like cracking a password)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青瓷清茶倾城歌 2024-09-06 18:32:16

使用 2.8.1 中包含的 Scala Parallel 集合,您将能够执行以下操作:

val spliced = myList.par // obtain a parallel version of your collection (all operations are parallel)
spliced.map(process _)   // maps each entry into a corresponding entry using `process`
spliced.find(check _)    // searches the collection until it finds an element for which
                         // `check` returns true, at which point the search stops, and the element is returned

并且代码将自动并行完成。常规集合库中的其他方法也正在并行化。

目前,2.8.RC2 已经非常接近了(本周或下周),我猜 2.8 最终版本将在几周后发布。如果您使用 2.8.1 nightlies,您将能够尝试并行收集。

With Scala Parallel collections that will be included in 2.8.1 you will be able to do things like this:

val spliced = myList.par // obtain a parallel version of your collection (all operations are parallel)
spliced.map(process _)   // maps each entry into a corresponding entry using `process`
spliced.find(check _)    // searches the collection until it finds an element for which
                         // `check` returns true, at which point the search stops, and the element is returned

and the code will automatically be done in parallel. Other methods found in the regular collections library are being parallelized as well.

Currently, 2.8.RC2 is very close (this or next week), and 2.8 final will come in a few weeks after, I guess. You will be able to try parallel collections if you use 2.8.1 nightlies.

鹿童谣 2024-09-06 18:32:16

您可以使用 Scalaz 的并发功能来实现您想要的目的。

import scalaz._
import Scalaz._
import concurrent.strategy.Executor
import java.util.concurrent.Executors

implicit val s = Executor.strategy[Unit](Executors.newFixedThreadPool(5))

val splicedList = biglist.grouped(100).toList
val sum = splicedList.parMap(_.sum).map(_.sum).get

让这个变得更漂亮是很容易的(即编写一个函数mapReduce,将分割和折叠全部合并在一起)。此外,List 上的 parMap 不必要地严格。您需要在整个列表准备好之前开始折叠。更像是:

val splicedList = biglist.grouped(100).toList
val sum = splicedList.map(promise(_.sum)).toStream.traverse(_.sum).get

You can use Scalaz's concurrency features to achieve what you want.

import scalaz._
import Scalaz._
import concurrent.strategy.Executor
import java.util.concurrent.Executors

implicit val s = Executor.strategy[Unit](Executors.newFixedThreadPool(5))

val splicedList = biglist.grouped(100).toList
val sum = splicedList.parMap(_.sum).map(_.sum).get

It would be pretty easy to make this prettier (i.e. write a function mapReduce that does the splitting and folding all in one). Also, parMap over a List is unnecessarily strict. You will want to start folding before the whole list is ready. More like:

val splicedList = biglist.grouped(100).toList
val sum = splicedList.map(promise(_.sum)).toStream.traverse(_.sum).get
他是夢罘是命 2024-09-06 18:32:16

与使用 future 创建 actor 相比,您可以用更少的开销来完成此操作:

import scala.actors.Futures._
val nums = (1 to 1000).grouped(100).toList
val parts = nums.map(n => future { n.reduceLeft(_ + _) })
val whole = (0 /: parts)(_ + _())

您必须处理分解问题并编写“future”块并将其重新组合成最终答案,但它确实需要并行执行一堆小代码块很容易做到。

(请注意,左侧折叠中的 _() 是 future 的 apply 函数,这意味着“给我你正在并行计算的答案!”,并且它会阻塞,直到答案可用为止.)

并行集合库会自动分解问题并为您重新组合答案(就像 Clojure 中的 pmap 一样);这还不是主要 API 的一部分。

You can do this with less overhead than creating actors by using futures:

import scala.actors.Futures._
val nums = (1 to 1000).grouped(100).toList
val parts = nums.map(n => future { n.reduceLeft(_ + _) })
val whole = (0 /: parts)(_ + _())

You have to handle decomposing the problem and writing the "future" block and recomposing it in to a final answer, but it does make executing a bunch of small code blocks in parallel easy to do.

(Note that the _() in the fold left is the apply function of the future, which means, "Give me the answer you were computing in parallel!", and it blocks until the answer is available.)

A parallel collections library would automatically decompose the problem and recompose the answer for you (as with pmap in Clojure); that's not part of the main API yet.

一袭水袖舞倾城 2024-09-06 18:32:16

我不等待 Scala 2.8.1 或 2.9,最好编写自己的库或使用另一个库,所以我做了更多谷歌搜索并发现了这个:akka
http://doc.akkasource.org/actors

它有一个带有方法的对象 future

awaitAll(futures: List[Future]): Unit
awaitOne(futures: List[Future]): Future

,但是 http://scalablesolutions.se/akka/api/akka-core-0.8.1/
根本没有文档。那很糟糕。

但好的一点是 akka 的 actor 比 scala 的原生 actor 更精简
有了所有这些库(包括 scalaz),如果 scala 本身最终能够正式合并它们,那就太好了

I'm not waiting for Scala 2.8.1 or 2.9, it would rather be better to write my own library or use another, so I did more googling and found this: akka
http://doc.akkasource.org/actors

which has an object futures with methods

awaitAll(futures: List[Future]): Unit
awaitOne(futures: List[Future]): Future

but http://scalablesolutions.se/akka/api/akka-core-0.8.1/
has no documentation at all. That's bad.

But the good part is that akka's actors are leaner than scala's native ones
With all of these libraries (including scalaz) around, it would be really great if scala itself could eventually merge them officially

荒芜了季节 2024-09-06 18:32:16

在 2010 年 Scala Days 上,Aleksandar Prokopec(他在 EPFL 从事 Scala 工作)进行了一次非常有趣的演讲,主题是 并行集合。这可能会在 2.8.1 中出现,但您可能需要等待更长的时间。我看看是否能得到演示文稿本身。链接到这里。

我们的想法是拥有一个集合框架,它通过完全按照您的建议进行集合处理,但对用户透明。理论上您所要做的就是将导入从 scala.collections 更改为 scala.parallel.collections。显然,您仍然需要做一些工作来看看您正在做的事情是否真的可以并行化。

At Scala Days 2010, there was a very interesting talk by Aleksandar Prokopec (who is working on Scala at EPFL) about Parallel Collections. This will probably be in 2.8.1, but you may have to wait a little longer. I'll lsee if I can get the presentation itself. to link here.

The idea is to have a collections framework which parallelizes the processing of the collections by doing exactly as you suggest, but transparently to the user. All you theoretically have to do is change the import from scala.collections to scala.parallel.collections. You obviously still have to do the work to see if what you're doing can actually be parallelized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文