Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. implicit def seq2Distinct[T, C[T] <: Seq[T]](tees: C[T]) = new {
  2. import collection.generic.CanBuildFrom
  3. import collection.mutable.{HashSet => MutableHashSet}
  4.  
  5. def distinctBy[S](hash: T => S)(implicit cbf: CanBuildFrom[C[T],T,C[T]]): C[T] = {
  6. val builder = cbf()
  7. val seen = MutableHashSet[S]()
  8.  
  9. for (t <- tees) {
  10. if (!seen(hash(t))) {
  11. builder += t
  12. seen += hash(t)
  13. }
  14. }
  15.  
  16. builder.result
  17. }
  18. }
  19.  
  20.  
  21. case class Content(id: Int, body: String)
  22.  
  23. val test = List(Content(1,"1"), Content(1,"2"), Content(2,"2"), Content(2,"2"), Content(3,"3"), Content(4,"4"), Content(5,"5"))
  24.  
  25. test.distinct
  26. test distinctBy { _.id }
Add Comment
Please, Sign In to add comment