Use substring iterator directly

This commit is contained in:
Sven Weidauer 2024-12-07 13:10:21 +01:00
parent e1f0159403
commit 6f7fc2d1a8

View file

@ -13,11 +13,9 @@ class Grid(val rows: List<String>) {
fun chars(x: Int, y: Int, dx: Int, dy: Int) = Line(x, y, dx, dy) fun chars(x: Int, y: Int, dx: Int, dy: Int) = Line(x, y, dx, dy)
inner class Line(val x: Int, val y: Int, val dx: Int, val dy: Int) : Sequence<Char> { inner class Line(val x: Int, val y: Int, val dx: Int, val dy: Int) : Sequence<Char> {
override fun iterator(): Iterator<Char> = iterator { override fun iterator(): Iterator<Char> = if (dx == 1 && dy == 0) {
if (dx == 1 && dy == 0) { rows[y].substring(startIndex = x).iterator()
yieldAll(rows[y].substring(startIndex = x).asSequence()) } else iterator {
} else {
var x = x var x = x
var y = y var y = y
@ -28,7 +26,7 @@ class Grid(val rows: List<String>) {
} }
} }
} }
}
fun rows(): Sequence<Line> = sequence { fun rows(): Sequence<Line> = sequence {
for (y in 0..<height) { for (y in 0..<height) {