I have a list of possible input Values
val inputValues = List(1,2,3,4,5)
I have a really long to compute function that gives me a result
def reallyLongFunction( input: Int ) : Option[String] = { ..... }
Using scala parallel collections, I can easily do
inputValues.par.map( reallyLongFunction( _ ) )
To get what all the results are, in parallel. The problem is, I don’t really want all the results, I only want the FIRST result. As soon as one of my input is a success, I want my output, and want to move on with my life. This did a lot of extra work.
So how do I get the best of both worlds? I want to
- Get the first result that returns something from my long function
- Stop all my other threads from useless work.
Edit –
I solved it like a dumb java programmer by having
@volatile var done = false;
Which is set and checked inside my reallyLongFunction. This works, but does not feel very scala. Would like a better way to do this….
I took interpreted your question in the same way as huynhjl, but if you just want to search and discard
Nones, you could do something like this to avoid the need to repeat the computation when a suitable outcome is found:However
findfor parallel collections seems to do a lot of unnecessary work (see this question), so this might not work well, with current versions of Scala at least.Volatile flags are used in the parallel collections (take a look at the source for
find,exists, andforall), so I think your idea is a good one. It’s actually better if you can include the flag in the function itself. It kills referential transparency on your function (i.e. for certain inputs your function now sometimes returnsNonerather thanSome), but since you’re discarding the stopped computations, this shouldn’t matter.