Skip to content

Instantly share code, notes, and snippets.

@sgabber
Last active September 20, 2019 08:22
Show Gist options
  • Save sgabber/f264c9770a0ef4a7345c514560b988b4 to your computer and use it in GitHub Desktop.
Save sgabber/f264c9770a0ef4a7345c514560b988b4 to your computer and use it in GitHub Desktop.
implementing the repeat until (like do while) only with basic scala constructs
object RepeatTest {
class RepeatHelper(cmd: => Unit) {
def until(cond: => Boolean): Unit = if (cond) repeat(cmd)
}
def repeat(cmd: => Unit): RepeatHelper = {
cmd
new RepeatHelper(cmd)
}
def main(args: Array[String]): Unit = {
var x = 3
repeat {x = x - 1; println(x)} until (x > 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment