Skip to content

Instantly share code, notes, and snippets.

View mogproject's full-sized avatar

Yosuke Mizutani mogproject

View GitHub Profile
@daithiocrualaoich
daithiocrualaoich / gist:1189097
Created September 2, 2011 16:37
Scala Collections Pimp to add distinctBy(hash: ... => ...) for hash filterings
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))) {