Skip to content

Instantly share code, notes, and snippets.

@haakonn
Created April 21, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haakonn/e15a739f10bccb34e7ea to your computer and use it in GitHub Desktop.
Save haakonn/e15a739f10bccb34e7ea to your computer and use it in GitHub Desktop.
Implementing the repeat loop in Scala
/*
Enables loops such as this:
var i = 10
repeat {
print(s"i is $i\n")
i = i - 1
} until (i == 0)
*/
class Until(command: => Unit) {
def until(cond: => Boolean): Unit = {
command
if (cond) () else until(cond)
}
}
object repeat {
def apply(command: => Unit) = new Until(command)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment