Scala ParStream emulation

The extraordinary template in Project Euler tasks seems somewhat equivalent:

Stream.from(1).map(f).takeWhile((_>0)).foldLeft(0L)(_+_)

where f is some expensive function that returns positive values ​​to some unknown point and returns zeros after that.

I like parallelizing things, especially when Scala has parallel collections and .parmakes it so simple. But in the absence of ParStream, the best I came up with is:

val BATCH=1024
Stream.from(1,BATCH).flatMap(
  i=>(i until i+BATCH).par.map(f)
).takeWhile((_>0)).foldLeft(0L)(_+_)

which does not seem very elegant and sensitive to the choice of value BATCH(but can give x4 speed improvements on my quad-core processor).

Any suggestions for cleaner ways to achieve the same result?

+5
source share
1 answer

, - Stream from 1 grouped BATCH map (_.par), Iterator - , Stream, REPL .

+5

All Articles