I can use the following as a way to generate a 10x...
# getting-started
r
I can use the following as a way to generate a 10x10 2d grid in a nice 1liner
Copy code
fun main() {
    val grid: List<List<Boolean>> = List(size=10) { List(size=10) { false }}
}
However this does not seem supported for mutable lists
Copy code
fun main() {
    val grid: MutableList<MutableList<Boolean>> = MutableList(size=10) { MutableList(size=10) { false }}
}
Why? And is there an analogous way to build this grid in 1 single readable expression?
c
What is the use-case? Why do you want a matrix that can change size? You will have no guarantees that all rows are the same length.
j
r
The inner grid is all that needs to be mutatable technically
it is for conway's game of life
so the inner needs to be hcangable
a cell can change from true to false
c
You probably should have a
List<List<Cell>>
where
Cell
is a mutable class, to avoid rows changing sizes.
Or keep
List<List<Boolean>>
and copy the matrix each time, if you want time travel.
s
However this does not seem supported for mutable lists
What makes you say that? The code you posted in your question works fine for me...
c
But honestly, the best solution would be to use multik
s
Anyway I think I'd go with
Copy code
List(10) { BooleanArray(10) }
Then you get mutable cells without being able to accidentally change the size 👍
r
Oh I guess the code does work. Sworn I remmeber it not earlier
😅 1
e
a quadtree is a better representation for GoL anyway, extends to hashlife