Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created June 20, 2011 22:24
Show Gist options
  • Select an option

  • Save bdarfler/1036753 to your computer and use it in GitHub Desktop.

Select an option

Save bdarfler/1036753 to your computer and use it in GitHub Desktop.
Split Collection
@tailrec
def splitHelper[T](l : List[T], b: ListBuffer[List[T]])(c: T => Boolean) : List[List[T]] = {
l match {
case Nil => b.result()
case h :: t =>
val (h, t) = l.tail.span(c)
b += l.head :: h ::: Nil
splitHelper(t, b)(c)
}
}
def split[T](l : List[T], b : ListBuffer[List[T]] = ListBuffer[List[T]]())(c: T => Boolean) = splitHelper(l, b)(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment