I would like to convert a piece of Java code that looks like this: Scala:
for (Iterator<Task> it = tasks.iterator(); it.hasNext() && workflow.isAutoRun();) { Task task = it.next(); if (!runTask(task)) break; }
I'm not a fan of scala for- concepts (I don't know how I understand iteration anyway), and I came up with the following:
val completed = tasks.forall { task => workflow.isAutoRun && runTask(task) }
However, the scaladoc for the forall method is as follows (italics mine):
Apply the predicate p to all elements of this iterable object and return true if the predicate returns true for all elements
This is not equivalent to what I did (because it implies that the predicate will be evaluated for each element, regardless of whether the previous estimate returned false ) and (in fact) actually not equivalent to what forall actually does, which on Iterator as follows:
def forall(p: A => Boolean): Boolean = { var res = true while (res && hasNext) res = p(next) res }
Anyway, I'm distracted: does anyone have any better suggestions for what the scala code should look like? I want to see something that better conveys intent:
tasks.doUntil(t => !isAutoRun || !runTask(t))
source share