Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save rkrzewski/9096755 to your computer and use it in GitHub Desktop.

Select an option

Save rkrzewski/9096755 to your computer and use it in GitHub Desktop.
def pack[T](ts: Seq[T]): Seq[Seq[T]] = {
@tailrec
def go(ts: Seq[T], acc: Seq[Seq[T]]): Seq[Seq[T]] = ts match {
case Seq() => acc
case head +: tail =>
val (same, different) = ts.span(_ == head) // ts.partition(_ == head)
go(different, acc :+ same)
}
go(ts, Seq()) // Seq(Seq())
}
def flatten(ls: Seq[Any]): Seq[Any] = ls.flatMap {
case l: Seq[_] => flatten(l)
case e => Seq(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment