我最近在一个宠物项目中尝试了 Scala,并注意到以下代码片段正在消耗内存(我经常调用此方法):
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
}
当我将“假”更改为“返回假”时,情况似乎已经自行解决:
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
}
当我考虑它时,片段 #2 对于我期望/希望该方法如何工作是有意义的。有人可以向我解释第一个片段在做什么吗?