Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created June 5, 2012 12:49
Show Gist options
  • Save hpcx82/2874764 to your computer and use it in GitHub Desktop.
Save hpcx82/2874764 to your computer and use it in GitHub Desktop.
Create loops using scala function
object LoopUnlessTest extends App
{
def loop(body: => Unit): LoopUnlessCond =
new LoopUnlessCond(body)
protected class LoopUnlessCond(body: => Unit)
{
def unless(cond: => Boolean)
{
body
if(!cond) unless(cond)
}
}
var i = 10
loop
{
println("i = " + i)
i -= 1
} unless (i == 0)
}
object WhileLoopTest extends App
{
def whileloop(cond: => Boolean) (body: => Unit) : Unit =
{
if(cond)
{
body
whileloop(cond)(body)
}
}
var i = 10
whileloop(i > 0)
{
println(i)
i -= 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment