I have a method in Scala that checks if a game board is full or not. To speed this method up, I return inside the nested for loop to break out of both of them and stop execution. How would I achieve this in Scala in an idiomatic way?def isFull: Boolean = {  for (x <- getGrid.indices) {    for (y <- getGrid(x).indices) {      if (getGrid(x)(y).getState.isInstanceOf[Undecided]) {        return false      }    }  }  true}
Checking whether a game board is full
performance;scala
You can apply forall to most containers to test if every element in the container satisfies some property. forall will only evaluate x + 1 if x is true. This type of evaluation is known as short-circuit evaluation. So basically we can use this property to perform the same behavior as the if-then statement in the original code.Notice also that I've used pattern matching to get rid of the isInstanceOf[T]. def isFull: Boolean =  getGrid.indices.forall(x =>    getGrid(x).indices.forall(y =>      getGrid(x)(y).getState match {        case xyState: Undecided => False        case _ => True      }    )  )