Remove duplicates in entries, scala way
Posted
by andersbohn
on Stack Overflow
See other posts from Stack Overflow
or by andersbohn
Published on 2010-04-03T13:01:30Z
Indexed on
2010/04/03
13:03 UTC
Read the original article
Hit count: 197
scala
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?
© Stack Overflow or respective owner