always the same pattern when calling parallel

62 views
Skip to first unread message

Jim Newton

unread,
Aug 11, 2016, 10:33:01 AM8/11/16
to scala-user
It is often the case that I want to call a function foo and pass it arguments which I'd like evaluated in parallel.
Is there an idiom for this?

E.g.,

val (v1, v2, v3, ..., vN) = parallel(calc1(),calc2(), calc3(), ... calcN())
foo(v1,v2,v3,...vN)

I'd like to do the following applyToParallelArgs(foo,calc1(),calc2(), calc3(), ... calcN())

Does such a applytoParallelArgs exist? Is there a better?

Jim

Seth Tisue

unread,
Aug 17, 2016, 12:16:44 PM8/17/16
to scala-user
I'm pretty sure there's nothing like this in the standard library.

It could be implemented on top of Future as a very repetitive series of overloads for the different arities; to define it for every arity at once you'd need Shapeless; see e.g. http://stackoverflow.com/questions/31225409/is-there-any-way-in-scala-to-have-a-dynamic-number-of-named-typed-parameters/31225749

Seth Tisue / Scala team / Lightbend, Inc.

Seth Tisue

unread,
Aug 17, 2016, 12:40:35 PM8/17/16
to scala-user
There's a SLIP out to add some new methods to Future and/or Promise: https://github.com/scala/slip/issues/7

I notice it proposes adding a "join" method to the Future companion object, with overloads for all the arities. It appears to me that it would reduce your example to:

Future.join(Future(calc1()), Future(calc2()), ...) map Function.tupled(foo)

Stephen Compall

unread,
Sep 10, 2016, 4:14:52 AM9/10/16
to Jim Newton, scala-user
If foo is a function, you can try foo.tupled(parallel(...)). If it isn't a function (perhaps a method), possibly (foo _).tupled will give the same effect as foo.tupled.

Greetings,

Stephen.


Reply all
Reply to author
Forward
0 new messages