来自 Josh Suereth 的“Scala in Depth”:
“应用仿函数提供了一种方法来进行两个计算并使用函数将它们连接在一起。Traversable 示例强调了如何将两个集合并行化成对。应用仿函数和并行处理就像面包和黄油一样结合在一起。”
我对整个 functors/monads/applicative 的东西有一个模糊的概念,但并不是完全掌握它(整个 monad,functor 的东西都是新手)。我对 monad(flatten、flatMap)和 monadic 工作流程以及仿函数(maps)的概念有所了解。
任何人都可以详细说明它是如何完成的、示例和/或它与“传统”并行化相比的好处吗?
最佳答案
我将问题转发给了 Josh Suereth。这是他的回复:
迈克-
I don't have a lot of time to respond, but I'll offer to examples of what I mean:
Example #1 - Form Validation.
I want to run some validation against input and aggregate all the errors, i.e. detect them in parallel. With applicative functions I can do so.
So, given a set of "processing" functions, like so:
def processUser(data: Data): Validation[User] = { if (data get "username" isEmpty) Failure("username must not be empty") else { val Some(user) = data get "username" if (user contains badCharacterRegex) Failure(s"username must not contain one of ${badchars}") else Success(user) } } def processCreditCard(data: Data): Validation[CreditCard] = ... def processAddress(data: Data): Validation[Address] = ... def handleForm(data: Data): ??? = { (processUser(data), processCreditCard(data), processAddress(data)) map { (user, card, address) => postPayment(user, address, card) } recover { (errors) => errors foreach println }
Now handle form will print out errors with CreditCard/username + address all at the same time, since you've combined them using an applicative functor. That's parallel error reporting (although testing isn't actually done in parallel).
(2) Futures
I want to do a few things in parallel and combine results. Future's "zip" method is actually an applicative functor in disguise. I can do this:
Future(computation1) zip Future(computation2) map { case (one,two) => .... }
I've just used Applicative Functors to "join" parallel computations.
It's exactly the same as the Form validation example.Hope that helps! - Josh
(请注意,这些代码片段是不可编译的示例;我将 SBT 的应用语法与 Scalaz 中的概念一起使用,因此您需要选择一个库来使用应用程序及其应用对象)
关于scala - 应用仿函数如何与并行算法联系起来? (斯卡拉和斯卡拉兹),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184373/