Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 8
    Be careful that vals is a strict collection -- if it's lazy (and in Scala 2.7 this includes the Range class), the futures won't be created until each one is needed by foreach, and nothing will happen in parallel.
    – Ken Bloom
    Commented Nov 18, 2009 at 5:34
  • I suppose we could solve that problem by injecting another foreach call between the map and the current foreach. Thus: vals map { x => future { f(x) } } foreach { x => x } foreach { _() } Commented Nov 18, 2009 at 15:43
  • That would be a map we have to inject, not another foreach? And it is not clear to me that the map of a lazy collection is strict. The safest way may be to call toArray. Commented Nov 20, 2009 at 22:50
  • You're right, foreach was (obviously) the wrong thing to inject since it returns Unit. My bad! :-) The map function on lazy collections is almost always non-strict, so we can either call toList (or toArray), or we can project and then force: (vals map { x => future { f(x) } } projection).force foreach { _() }. I don't know whether that's better than simply toList, but it is certainly different. Commented Nov 22, 2009 at 2:01
  • What does it mean when you say it "returns asynchronously"? Does it imply that it is non-blocking? (and why would that be a problem?)
    – Jus12
    Commented Aug 5, 2014 at 14:24