Is there a way to shorten this? A 2D range or some...
# announcements
l
Is there a way to shorten this? A 2D range or something…
Copy code
(0 until width).forEach { x ->
        (0 until height).forEach { y ->
                println("($x, $y)")
        }
}
I would like to write code similar to:
Copy code
(width rangeBy height).forEach { (x, y) -> println("($x, $y)") }
Note that
width
and
height
don’t come from an iterable object in my case.
1
s
You just written the code yourself already, basically 🙂 :
Copy code
fun Pair<Int, Int>.forEach(block: (Int,Int)-> Unit) {
    (0 until width).forEach { x ->
        (0 until height).forEach { y ->
            block(x, y)
        }
    }
}

...
(width to height).forEach { x, y-> println("$x, $y") }
l
Yes well I was asking in the case it’s already in the standard library
🚫 1