Advertisement
mitrakov

Scala: Cond Guards

Feb 7th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.00 KB | None | 0 0
  1. // 1. Let's test Condition Guards on Lists:
  2. object Main extends App {
  3.   val lst1 = List(1, 2, 3)
  4.   val lst2 = List(10, 20, 30)
  5.  
  6.   val result: List[Int] = for {
  7.     x <- lst1
  8.     y <- lst2
  9.   } yield x + y
  10.  
  11.   println(result)
  12. }
  13. // Output: List(11, 21, 31, 12, 22, 32, 13, 23, 33)
  14.  
  15.  
  16.  
  17. // 1.1. Now add a guard:
  18. val result: List[Int] = for {
  19.   x <- lst1 if x >= 2
  20.   y <- lst2 if y >= 30
  21. } yield x + y
  22.  
  23. // Output: List(32, 33)
  24.  
  25.  
  26.  
  27. // 2. Let's test Condition Guards on Options:
  28. object Main extends App {
  29.   def div(x: Int, y: Int): Option[Int] = if (y != 0) Some(x/y) else None
  30.   def maxElement(list: Int*): Option[Int] = if (list.nonEmpty) Some(list.max) else None
  31.  
  32.   val result = for {
  33.     max1 <- maxElement(1, 2, 3)
  34.     max2 <- maxElement(4, 5, 6)
  35.     res <- div(max2, max1)
  36.   } yield res
  37.  
  38.   println(result)
  39. }
  40. // Output: Some(2)
  41.  
  42.  
  43.  
  44. // 2.1. Now add a guard:
  45. val result = for {
  46.   max1 <- maxElement(1, 2, 3)
  47.   max2 <- maxElement(4, 5, 6) if max2 > 100
  48.   res <- div(max2, max1)
  49. } yield res
  50. // Output: None
  51.  
  52.  
  53.  
  54. // 3. Let's test Condition Guards on Futures:
  55. object Main extends App {
  56.   def print(x: Any): Unit = {println(s"${LocalDateTime.now()}: $x")}
  57.  
  58.   val future5: Future[Int] = Future {
  59.     print("Future5 started")
  60.     Thread.sleep(5000)
  61.     print("Future5 finished after 5 sec")
  62.     5
  63.   }
  64.   val future6: Future[Int] = Future {
  65.     print("Future6 started")
  66.     Thread.sleep(6000)
  67.     print("Future6 finished after 6 sec")
  68.     6
  69.   }
  70.   val future7: Future[Int] = Future {
  71.     print("Future7 started")
  72.     Thread.sleep(7000)
  73.     print("Future7 finished after 7 sec")
  74.     7
  75.   }
  76.  
  77.   val result: Future[Int] = for {
  78.     r5 <- future5
  79.     r6 <- future6
  80.     r7 <- future7
  81.   } yield r5 + r6 + r7
  82.  
  83.   print(Await.result(result, 20 seconds))
  84. }
  85.  
  86. // Output:
  87. // 2018-02-07T23:26:00.037: Future5 started
  88. // 2018-02-07T23:26:00.037: Future6 started
  89. // 2018-02-07T23:26:00.037: Future7 started
  90. // 2018-02-07T23:26:05.049: Future5 finished after 5 sec
  91. // 2018-02-07T23:26:06.049: Future6 finished after 6 sec
  92. // 2018-02-07T23:26:07.049: Future7 finished after 7 sec
  93. // 2018-02-07T23:26:07.051: 18
  94.  
  95.  
  96.  
  97. // 3.1. Now add a guard:
  98. val result: Future[Int] = for {
  99.   r5 <- future5
  100.   r6 <- future6 if r6 > 100
  101.   r7 <- future7
  102. } yield r5 + r6 + r7
  103.  
  104. // Output:
  105. // 2018-02-07T23:26:45.790: Future5 started
  106. // 2018-02-07T23:26:45.790: Future7 started
  107. // 2018-02-07T23:26:45.790: Future6 started
  108. // 2018-02-07T23:26:50.799: Future5 finished after 5 sec
  109. // 2018-02-07T23:26:51.799: Future6 finished after 6 sec
  110. // Exception in thread "main" java.util.NoSuchElementException: Future.filter predicate is not satisfied
  111. //  at scala.concurrent.Future.$anonfun$filter$1(Future.scala:335)
  112. //  at scala.util.Success.$anonfun$map$1(Try.scala:251)
  113. //  at scala.util.Success.map(Try.scala:209)
  114. //  at scala.concurrent.Future.$anonfun$map$1(Future.scala:287)
  115. //  at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
  116. //  at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement