Scala is the equivalent of Google collection lists.

I am looking for a function that splits a list into fixed-size sublists , namely Lists.partition from the Google Collections library. I could not find such a method in the Scala Collections API. Did I miss something?

+7
scala guava
source share
1 answer

The method you are looking for is "grouped." A slight difference from the split function is that it returns an Iterator of lists, not a list of lists. This may be good, or you may need to convert it using the Iterator.toList function

val list = List(1, 2, 3, 4, 5) println(list.grouped(2).toList) //prints List(List(1, 2), List(3, 4), List(5)) 
+15
source share

All Articles