Remove duplicates in entries, scala way
- by andersbohn
I have a list of entries stating a production value in a given interval. Entries stating the exact same value at a later time adds no information and can safely be left out.
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 scala 2.8 collection functions, 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? Am I missing out on some scala magic?