Skip to content

Instantly share code, notes, and snippets.

@afazio
Last active August 29, 2015 14:25
Show Gist options
  • Save afazio/4a30e05d5ec8aaab0b7b to your computer and use it in GitHub Desktop.
Save afazio/4a30e05d5ec8aaab0b7b to your computer and use it in GitHub Desktop.
Test whyle loop with example run and output
> 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
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