开发者

Scala equivalent of Google Collections Lists.partition

开发者 https://www.devze.com 2023-01-13 14:36 出处:网络
I am looking for a function that will partition a list into fixed size sublists, exactly what Lists.partition from Google Collections library does.

I am looking for a function that will partition a list into fixed size sublists, exactly what Lists.partition from Google Collections library does. I couldn't find such method in the Scala Collections API. Am I missing 开发者_如何学运维something?


The method you are looking for is "grouped". A slight difference from the partition function is that it returns an Iterator of Lists rather than a List of Lists. That may be fine, 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))
0

精彩评论

暂无评论...
验证码 换一张
取 消