Skip to content

Instantly share code, notes, and snippets.

@riffraff
Created May 19, 2009 16:59
Show Gist options
  • Save riffraff/114222 to your computer and use it in GitHub Desktop.
Save riffraff/114222 to your computer and use it in GitHub Desktop.
//this should be builtin in 2.8
def groupBy[T](list: List[T], eq: (T,T) => Boolean ): List[List[T]] = list match {
case Nil => Nil
case x::xs => {
val (ys:List[T],zs:List[T]) = xs.span(eq(_,x));
(x::ys)::groupBy(zs, eq)
}
}
def compress3[T](list: List[T]): List[T] =
groupBy(list,_==_).map(_.head)
println(compress3(List('a','a','a','a','b','c','c','a','a','d','e','e','e','e')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment