val l = List(1,2,3,4) val i = l.iterator var cont = true while (i.hasNext) { val k = i.next() if (isValid(k)) { val result = doStuff(k) if (result.stopProcessing) { cont = false } } }
On Mon, Jun 24, 2013 at 10:57 AM, Andreas Joseph Krogh and...@officenet.no wrote:
What is the most idiomatic way to do this in Scala?
Here's another version:
@tailrec
def loop(l: List[Int]) {
val cont = l match {
case head :: tail if isValid(head) =>
!doStuff(head).stopProcessing
case head :: tail =>
true
case Nil =>
false
}
if (cont) loop(l.tail)
}
—
Alexandru Nedelcu
https://bionicspirit.com
--
list.iterator takeWhile { x =>
isValid(x) && !doStuff(x).stopProcessing
}
Also, technically the original loop (as written) just goes through everything in the iterator, since done doesn't show up in a condition.