https://kotlinlang.org logo
Title
l

Luke

09/25/2019, 1:11 PM
Is there a way to shorten this? A 2D range or something…
(0 until width).forEach { x ->
        (0 until height).forEach { y ->
                println("($x, $y)")
        }
}
I would like to write code similar to:
(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

streetsofboston

09/25/2019, 1:14 PM
You just written the code yourself already, basically 🙂 :
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

Luke

09/25/2019, 1:34 PM
Yes well I was asking in the case it’s already in the standard library
🇳🇴 1