slice() returns a list of the collection elements with given indices. Range or Integer values, both can be used for passing Indices.
val games = listOf("unity", "unreal", "godot", "bevy", "monogame", "supe
rgame" )
println(games.slice(1..3))
println(games.slice(0..4 step 2))
println(games.slice(setOf(3, 5, 0)))
output:
[unreal, godot, bevy]
[unity, godot, monogame]
[bevy, supergame, unity]
chunked() function breaks a collection into parts of a given size. chunked() accept a single argument – the size of the chunk – and returns a List collection,where each list is of specified size.
val games = (0..7).toList()
println(games.chunked(2))
output[[0, 1], [2, 3], [4, 5], [6, 7]]