Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 25, 2010 15:13
Show Gist options
  • Save kmizu/413243 to your computer and use it in GitHub Desktop.
Save kmizu/413243 to your computer and use it in GitHub Desktop.
def sublistN[T](list: List[T], n: Int): List[List[T]] = {
(1 until n).foldLeft((List[List[T]](), list)) {
case ((xs, y::ys), _) => (xs:::List(List(y)), ys)
case ((xs, Nil), _) => (xs:::List(List[T]()), Nil)
} match { case (xs, ys) => xs:::List(ys) }
}
println(sublistN(List(34, 11, 23, 1, 9, 83, 5), 3))
println(sublistN(List(32, 11), 3))
println(sublistN(List(32), 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment