Created
November 16, 2013 18:31
-
-
Save metasim/7503601 to your computer and use it in GitHub Desktop.
Example of creating a `repeat { ... } until (...)` like control structure.
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
import scala.annotation.tailrec | |
object controls { | |
def repeat(body: => Unit) = new { | |
@tailrec | |
def until(condition: => Boolean) { | |
body | |
if (condition) () else until(condition) | |
} | |
} | |
var i = 0; | |
repeat { | |
println("i = " + i) | |
i = i + 1 | |
} until (i > 3) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment