Edit: The fact is added that the list is sorted, and the implementation of "duplicate" is misleading, is replaced by "redundant" in the header.
I have a sorted list of records indicating the production value for a given interval. Entries indicating the same value later do not add any information and can be easily excluded.
case class Entry(minute:Int, production:Double)
val entries = List(Entry(0, 100.0), Entry(5, 100.0), Entry(10, 100.0), Entry(20, 120.0), Entry(30, 100.0), Entry(180, 0.0))
Experimenting with the functions of the scala 2.8 collection, so far I have this working implementation:
entries.foldRight(List[Entry]()) {
(entry, list) => list match {
case head :: tail if (entry.production == head.production) => entry :: tail
case head :: tail => entry :: list
case List() => entry :: List()
}
}
res0: List[Entry] = List(Entry(0,100.0), Entry(20,120.0), Entry(30,100.0), Entry(180,0.0))
Any comments? Did I miss some scala magic?
source
share