I've been trying out Scala in a pet project recently and noticed that the following code snippet was eating up memory (I call this method a lot):
private[this] def isConsistent(startLang2: Int, lengthLang2: Int,
startLang1: Int, lengthLang1: Int,
lang2FinalAlignments: ArrayBuffer[ArrayBuffer[Int]]): Boolean = {
(startLang2 to (startLang2 + lengthLang2)) foreach {
i =>
val valueSeq = lang2FinalAlignments(i)
if (valueSeq.size == 0 || valueSeq.exists { value => value < startLang1 || value > (startLang1 + lengthLang1) })
false
}
true
}
When I changed the 'false' to 'return false' the situation seems to have resolved itself:
private[this] def isConsistent(startLang2: Int, lengthLang2: Int,
startLang1: Int, lengthLang1: Int,
lang2FinalAlignments: ArrayBuffer[ArrayBuffer[Int]]): Boolean = {
(startLang2 to (startLang2 + lengthLang2)) foreach {
i =>
val valueSeq = lang2FinalAlignments(i)
if (valueSeq.size == 0 || valueSeq.exists { value => value < startLang1 || value > (startLang1 + lengthLang1) })
return false
}
true
}
When I think about it snippet #2 makes sense with regards to how I expect/want the method to work. Could someone please explain to me what the first snippet is doing?