I see that Scala has a distinct
method which will take a sequence and return a new sequence without any duplicate elements.
Seq(1,1,2,3,2,5).distinct // returns Seq(1,2,3,5)
I would like to do something similar, but base the uniqueness on the results of a function. Is there a clean idiomatic way to do this in Scala?
As an example in another language, I know Groovy's analogous unique
method can take a closure to determine the uniqueness function:
[1,3,4,5].unique { it % 2 } // returns [1,4]
Is there a similarly short and descriptive approach in Scala, or am I best served with something like the following?
Seq(1,3,4,5).groupBy(_ % 2).values.map(_.head)