From ae6b0f03d0708efac6d96431ffba56d831d271ed Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Sat, 17 Sep 2022 19:01:38 +0200 Subject: [PATCH 1/3] Add code tabs for _tour/for-comprehensions --- _tour/for-comprehensions.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/_tour/for-comprehensions.md b/_tour/for-comprehensions.md index 24e92b033e..79298cf84f 100644 --- a/_tour/for-comprehensions.md +++ b/_tour/for-comprehensions.md @@ -71,7 +71,6 @@ foo(10, 10).foreach { case (i, j) => println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) (6, 4) (7, 3) (8, 2) (9, 1) } - ``` {% endtab %} @@ -79,15 +78,13 @@ foo(10, 10).foreach { {% tab 'Scala 3' for=for-comprehensions-02 %} ```scala def foo(n: Int, v: Int) = - for i <- 0 until n - j <- 0 until n if i + j == v + for i <- 0 until n + j <- 0 until n if i + j == v yield (i, j) foo(10, 10).foreach { - case (i, j) => - println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) (6, 4) (7, 3) (8, 2) (9, 1) + (i, j) => println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) (6, 4) (7, 3) (8, 2) (9, 1) } - ``` {% endtab %} {% endtabs %} From f0a6693078e96bf725e708c3fde125ceaadf56f6 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Sat, 17 Sep 2022 19:32:16 +0200 Subject: [PATCH 2/3] Update for-comprehensions.md --- _tour/for-comprehensions.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/_tour/for-comprehensions.md b/_tour/for-comprehensions.md index 79298cf84f..d50aacbcdb 100644 --- a/_tour/for-comprehensions.md +++ b/_tour/for-comprehensions.md @@ -54,7 +54,6 @@ twentySomethings.foreach(println) // prints Travis Dennis {% endtab %} {% endtabs %} - A `for` loop with a `yield` statement returns a result, the container type of which is determined by the first generator. `user <- userBase` is a `List`, and because we said `yield user.name` where `user.name` is a `String`, the overall result is a `List[String]`. And `if user.age >=20 && user.age < 30` is a guard that filters out users who are not in their twenties. Here is a more complicated example using two generators. It computes all pairs of numbers between `0` and `n-1` whose sum is equal to a given value `v`: @@ -113,8 +112,8 @@ foo(10, 10) {% tab 'Scala 3' for=for-comprehensions-03 %} ```scala def foo(n: Int, v: Int) = - for i <- 0 until n - j <- 0 until n if i + j == v + for i <- 0 until n + j <- 0 until n if i + j == v do println(s"($i, $j)") foo(10, 10) @@ -122,7 +121,6 @@ foo(10, 10) {% endtab %} {% endtabs %} - ## More resources * Other examples of "For comprehension" in the [Scala Book](/overviews/scala-book/for-expressions.html) From 8052bb49b831525dcda32f08cf53fe82169d4c52 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Tue, 20 Sep 2022 10:55:27 +0200 Subject: [PATCH 3/3] Update for-comprehensions.md --- _tour/for-comprehensions.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/_tour/for-comprehensions.md b/_tour/for-comprehensions.md index d50aacbcdb..c945055083 100644 --- a/_tour/for-comprehensions.md +++ b/_tour/for-comprehensions.md @@ -34,7 +34,6 @@ val twentySomethings = twentySomethings.foreach(println) // prints Travis Dennis ``` {% endtab %} - {% tab 'Scala 3' for=for-comprehensions-01 %} ```scala case class User(name: String, age: Int) @@ -73,7 +72,6 @@ foo(10, 10).foreach { ``` {% endtab %} - {% tab 'Scala 3' for=for-comprehensions-02 %} ```scala def foo(n: Int, v: Int) =