如thefourtheye所述,您的函数返回fold
s 的结果.尾递归版(带模式匹配)看起来像:
@tailrec def loop(newInterests: Set[Interest], oldInterests: Set[Interest]): Set[Interest] = { newInterests.headOption match { case None => oldInterests case Some(ni) => oldInterests.find(oi => oi.keyword == ni.keyword) match { case None => loop(newInterests.tail, oldInterests + ni) case Some(k) => loop(newInterests.tail, oldInterests - k + k.copy(count = k.count + 1)) } } }