Created
April 21, 2015 20:38
-
-
Save haakonn/e15a739f10bccb34e7ea to your computer and use it in GitHub Desktop.
Implementing the repeat loop in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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