Created
September 25, 2019 12:45
-
-
Save crakjie/b88a5cd257c3537aed927a99a23e96b7 to your computer and use it in GitHub Desktop.
Split a list into n list containing an even number of element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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