https://kotlinlang.org logo
#announcements
Title
# announcements
l

Luke

09/25/2019, 1:11 PM
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

streetsofboston

09/25/2019, 1:14 PM
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

Luke

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