Advertisement
Guest User

1

a guest
Jul 20th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.78 KB | None | 0 0
  1. import scala.annotation.tailrec
  2. import scala.util.Random
  3.  
  4. /**
  5.   * Created by levkovich.n on 20/07/2016.
  6.   */
  7. object WhileLoop extends App {
  8.  
  9.   var r = 0
  10.   val value = run {
  11.     r = Random.nextInt(10)
  12.     r
  13.   } until (r == 0)
  14.   println(value)
  15.  
  16.   @tailrec
  17.   def whileLoop(condition: => Boolean)(command: => Unit): Unit = {
  18.     if (condition) {
  19.       (command);
  20.       whileLoop(condition) {
  21.         command
  22.       }
  23.     }
  24.   }
  25.  
  26.   val array = Array(1, 2, 3)
  27.   var i = 0
  28.   whileLoop(i < array.length) {
  29.     println(i)
  30.     i += 1
  31.   }
  32. }
  33.  
  34. class run[A](body: => A) {
  35.   @tailrec
  36.   final def until(condition: => Boolean): A = {
  37.     val ans: A = body
  38.     if (!condition) until(condition) else ans
  39.   }
  40. }
  41.  
  42. object run {
  43.   def apply[A](body: => A): run[A] = new run(body)
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement