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....