Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.annotation.tailrec
- import scala.util.Random
- /**
- * Created by levkovich.n on 20/07/2016.
- */
- object WhileLoop extends App {
- var r = 0
- val value = run {
- r = Random.nextInt(10)
- r
- } until (r == 0)
- println(value)
- @tailrec
- def whileLoop(condition: => Boolean)(command: => Unit): Unit = {
- if (condition) {
- (command);
- whileLoop(condition) {
- command
- }
- }
- }
- val array = Array(1, 2, 3)
- var i = 0
- whileLoop(i < array.length) {
- println(i)
- i += 1
- }
- }
- class run[A](body: => A) {
- @tailrec
- final def until(condition: => Boolean): A = {
- val ans: A = body
- if (!condition) until(condition) else ans
- }
- }
- object run {
- def apply[A](body: => A): run[A] = new run(body)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement