Skip to content

Instantly share code, notes, and snippets.

@crakjie
Created September 25, 2019 12:45
Show Gist options
  • Save crakjie/b88a5cd257c3537aed927a99a23e96b7 to your computer and use it in GitHub Desktop.
Save crakjie/b88a5cd257c3537aed927a99a23e96b7 to your computer and use it in GitHub Desktop.
Split a list into n list containing an even number of element
/** Split a list in n List */
def splitIn[A](n: Int)(l: List[A]): List[List[A]] = {
l.zipWithIndex
.foldLeft(Array.fill(n)(List.empty[A])) {
case (b, a) =>
b(a._2 % n) = a._1 :: b(a._2 % n)
b
}
.filter(_.nonEmpty)
.map(_.reverse)(collection.breakOut)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment