I have a collection of objects
case class Record(value: Whatever)
val list: List[Record]
and want to select the best ranked
list.foldLeft(list.head) { (best, current) =>
if (rank(current.value) > rank(best.value)) {
current
} else {
best
}
}
Let's suppose that rank is expensive and better not be called twice on the same object. What are my options?
I can fold to tuple (rank, record) but this probably means creating auxiliary objects during iteration. Should I worry about the overhead? Or rather
How can this be implemented efficiently in Scala?
What's the proper 'functional' view of the problem?