Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implicit def seq2Distinct[T, C[T] <: Seq[T]](tees: C[T]) = new {
- import collection.generic.CanBuildFrom
- import collection.mutable.{HashSet => MutableHashSet}
- def distinctBy[S](hash: T => S)(implicit cbf: CanBuildFrom[C[T],T,C[T]]): C[T] = {
- val builder = cbf()
- val seen = MutableHashSet[S]()
- for (t <- tees) {
- if (!seen(hash(t))) {
- builder += t
- seen += hash(t)
- }
- }
- builder.result
- }
- }
- case class Content(id: Int, body: String)
- val test = List(Content(1,"1"), Content(1,"2"), Content(2,"2"), Content(2,"2"), Content(3,"3"), Content(4,"4"), Content(5,"5"))
- test.distinct
- test distinctBy { _.id }
Add Comment
Please, Sign In to add comment