Last active
August 29, 2015 14:25
-
-
Save afazio/4a30e05d5ec8aaab0b7b to your computer and use it in GitHub Desktop.
Test whyle loop with example run and output
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
> scala whyle_test.scala Testing the whyle loop | |
Testing | |
the | |
whyle | |
loop | |
Predicate is being evaluated | |
Body is being evaluated | |
Testing | |
Predicate is being evaluated | |
Body is being evaluated | |
the | |
Predicate is being evaluated | |
Body is being evaluated | |
whyle | |
Predicate is being evaluated | |
Body is being evaluated | |
loop | |
Predicate is being evaluated |
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 Main { | |
def main (args: Array[String]) { | |
var i = 0 | |
whyle(i < args.length) { | |
println(args(i)) | |
i += 1 | |
} | |
i = 0 // reset i to 0 | |
whyle { | |
println("Predicate is being evaluated") | |
i < args.length | |
} { | |
println("Body is being evaluated") | |
println(args(i)) | |
i += 1 | |
} | |
} | |
def whyle(predicate: => Boolean)(block: => Unit) { | |
@tailrec | |
def whyle_internal { | |
if (predicate) { | |
block | |
whyle_internal | |
} | |
} | |
whyle_internal | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment