Created
June 5, 2012 12:49
-
-
Save hpcx82/2874764 to your computer and use it in GitHub Desktop.
Create loops using scala function
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
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) | |
} |
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
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