Reputation: 1699
I have the following declaration of a function
def myfunc(l: List[RoseTree]): Option[RoseTree] = {
//Complex calculations
}
Now, I have to run this function on two huge different lists. So, I wish to run the same function, with different data, in parallel.
I have been taking a look at the Future module of Scala.
However, I also wish that when either of the functions returns a "Some(RoseTree)", then it tells the other call to stop and keep the result. Is this possible?
Kind regards.
Upvotes: 3
Views: 836
Reputation: 2048
Scala has built-in feature for this. You can use Future in combination of Future.firstCompletedOf()
.
Please have a check at the Scala doc and I would suggest to have a look at this post
As point in the comments section, the Future.firstCompletedOf
does not meet the requirement "cancel the futures after one is completed". You can use Monix Task (which is a replacement for Scala Futures), which support by default this. Please have a look at this.
Upvotes: 2