[英]Reasoning about `parMap`
Given: 鉴于:
Prelude Control.Parallel.Strategies> :t parMap
parMap :: Strategy b -> (a -> b) -> [a] -> [b]
Prelude Control.Parallel.Strategies> :i Strategy
type Strategy a = a -> Eval a
-- Defined in `Control.Parallel.Strategies'
My understanding then is that parMap
can be expanded, replacing b
with b -> Eval b
to: 然后,我的理解是parMap
可以扩展,将b
替换为b -> Eval b
可以:
parMap :: (a -> b -> Eval b) -> (a -> b -> Eval b) -> [b -> Eval b]
This seems more complicated than the example version in Parallel and Concurrent Haskell : 这似乎比Parallel和Concurrent Haskell中的示例版本更复杂:
parMap :: (a -> b) -> [a] -> Eval [b]
parMap f [] = return []
parMap f (a:as) = do
b <- rpar (f a)
bs <- parMap f as
return (b:bs)
In the standard library implementation of parMap
, what's the meaning of Strategy b
, namely in a -> b -> Eval b
? 在parMap
的标准库实现中, Strategy b
的含义是什么,即parMap
a -> b -> Eval b
?
There is a mistake in your interpretation, Strategy
is not a type class but a type alias. 您的解释有误, Strategy
不是类型类,而是类型别名。 If you check the signature of parMap
, Strategy b
is followed by ->
and not by =>
which means that parMap
expects a Strategy b
as first argument. 如果检查parMap
的签名,则Strategy b
后跟->
而不是=>
,这意味着parMap
将Strategy b
作为第一个参数。 Strategy b
is alias for b -> Eval b
which means that the signature of parMap
can be expanded to: Strategy b
是b -> Eval b
别名,这意味着parMap
的签名可以扩展为:
parMap :: (b -> Eval b) -> (a -> b) -> [a] -> [b]
The book parMap
is not the same as the one above because its signature is 书parMap
与上面的书parMap
,因为它的签名是
parMap :: (a -> b) -> [a] -> Eval [b]
The main difference is that the first version uses the given Strategy b
to compute a [b]
from the arguments (a -> b) -> [a]
while the second version doesn't compute a [b]
but a Eval [b]
. 主要的区别在于,第一版本使用给定Strategy b
来计算[b]
从所述参数(a -> b) -> [a]
而第二版本不计算[b]
但Eval [b]
。 Eval [b]
specifies how to produce a [b]
and you must call runEval
to use it. Eval [b]
指定如何产生[b]
,必须调用runEval
才能使用它。
The first version is easier to use in a program because you don't need to call runEval
and it's also better because the strategy to evaluate each element of the list is not predefined, like in the book where the strategy is rpar
, but it's an argument. 第一个版本在程序中更易于使用,因为您不需要调用runEval
,它也更好,因为未预先定义评估列表中每个元素的策略,例如在书中,该策略是rpar
,但是它是一个论点。 This means that the first version is more generic. 这意味着第一个版本更为通用。 Have a look at the basic strategies to better understand what you can pass to the standard parMap
. 看一下基本策略,以更好地理解可以传递给标准parMap
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.