Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Let's test Condition Guards on Lists:
- object Main extends App {
- val lst1 = List(1, 2, 3)
- val lst2 = List(10, 20, 30)
- val result: List[Int] = for {
- x <- lst1
- y <- lst2
- } yield x + y
- println(result)
- }
- // Output: List(11, 21, 31, 12, 22, 32, 13, 23, 33)
- // 1.1. Now add a guard:
- val result: List[Int] = for {
- x <- lst1 if x >= 2
- y <- lst2 if y >= 30
- } yield x + y
- // Output: List(32, 33)
- // 2. Let's test Condition Guards on Options:
- object Main extends App {
- def div(x: Int, y: Int): Option[Int] = if (y != 0) Some(x/y) else None
- def maxElement(list: Int*): Option[Int] = if (list.nonEmpty) Some(list.max) else None
- val result = for {
- max1 <- maxElement(1, 2, 3)
- max2 <- maxElement(4, 5, 6)
- res <- div(max2, max1)
- } yield res
- println(result)
- }
- // Output: Some(2)
- // 2.1. Now add a guard:
- val result = for {
- max1 <- maxElement(1, 2, 3)
- max2 <- maxElement(4, 5, 6) if max2 > 100
- res <- div(max2, max1)
- } yield res
- // Output: None
- // 3. Let's test Condition Guards on Futures:
- object Main extends App {
- def print(x: Any): Unit = {println(s"${LocalDateTime.now()}: $x")}
- val future5: Future[Int] = Future {
- print("Future5 started")
- Thread.sleep(5000)
- print("Future5 finished after 5 sec")
- 5
- }
- val future6: Future[Int] = Future {
- print("Future6 started")
- Thread.sleep(6000)
- print("Future6 finished after 6 sec")
- 6
- }
- val future7: Future[Int] = Future {
- print("Future7 started")
- Thread.sleep(7000)
- print("Future7 finished after 7 sec")
- 7
- }
- val result: Future[Int] = for {
- r5 <- future5
- r6 <- future6
- r7 <- future7
- } yield r5 + r6 + r7
- print(Await.result(result, 20 seconds))
- }
- // Output:
- // 2018-02-07T23:26:00.037: Future5 started
- // 2018-02-07T23:26:00.037: Future6 started
- // 2018-02-07T23:26:00.037: Future7 started
- // 2018-02-07T23:26:05.049: Future5 finished after 5 sec
- // 2018-02-07T23:26:06.049: Future6 finished after 6 sec
- // 2018-02-07T23:26:07.049: Future7 finished after 7 sec
- // 2018-02-07T23:26:07.051: 18
- // 3.1. Now add a guard:
- val result: Future[Int] = for {
- r5 <- future5
- r6 <- future6 if r6 > 100
- r7 <- future7
- } yield r5 + r6 + r7
- // Output:
- // 2018-02-07T23:26:45.790: Future5 started
- // 2018-02-07T23:26:45.790: Future7 started
- // 2018-02-07T23:26:45.790: Future6 started
- // 2018-02-07T23:26:50.799: Future5 finished after 5 sec
- // 2018-02-07T23:26:51.799: Future6 finished after 6 sec
- // Exception in thread "main" java.util.NoSuchElementException: Future.filter predicate is not satisfied
- // at scala.concurrent.Future.$anonfun$filter$1(Future.scala:335)
- // at scala.util.Success.$anonfun$map$1(Try.scala:251)
- // at scala.util.Success.map(Try.scala:209)
- // at scala.concurrent.Future.$anonfun$map$1(Future.scala:287)
- // at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
- // at scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement