I use scala frames (2.12) to apply a division and conquest approach for a complex task. Here is some (simplified) context:
def solve(list: List[Int], constraints: Con): Future[Boolean] =
Future.unit.flatMap{ _ =>
if(list.isEmpty) Future.successful(true)
else if(someTest(constraints)) Future.successful(false)
else {
val components: List[List[Int]] = split(list,constraints)
val newConstr: Con = updateConstr(...)
val futureList = components.map(c => solve(c,newConstr))
allTrue(Future.successful(true), futureList)
}
}
This recursive function accepts a list of integer variables and an object Conrepresenting constraint problems, and generates many independent subtasks during each call.
The relevant part of my question is the challenge allTrue. If I solved the problem sequentially, I would write components.forall(c => solve(c,newConstr)). However, in the parallel version, I have something like this that does not stop the calculation in the first case encountered false.
def allTrue(acc: Future[Boolean], remaining: List[Future[Boolean]]):
Future[Boolean] = {
remaining match {
case Nil => acc
case r :: tail => acc.flatMap{ b =>
if(b) allTrue(r,tail)
else{
Future.successful(false)
}
}
}
}
, , scala, , , , .
, forall ?