Skip to content

Instantly share code, notes, and snippets.

@ghasemdev
Last active February 1, 2022 09:59
Show Gist options
  • Save ghasemdev/aa6b6c20e3424f36fccb873d47a9c1ff to your computer and use it in GitHub Desktop.
Save ghasemdev/aa6b6c20e3424f36fccb873d47a9c1ff to your computer and use it in GitHub Desktop.
Repeat loop
import Jumps.BREAK
import Jumps.CONTINUE
enum class Jumps { BREAK, CONTINUE }
inline fun repeat(action: (index: Int) -> Any) {
var counter = 0
loop@ while (true) {
val result = action(counter)
if (result == BREAK) break@loop
counter++
}
}
fun main() {
repeat {
if (it == 2) return@repeat CONTINUE
if (it == 5) return@repeat BREAK
println(it)
Thread.sleep(100)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment