Skip to content

Add code tabs for _tour/for-comprehensions #2536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions _tour/for-comprehensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -54,7 +53,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`:
Expand All @@ -71,23 +69,19 @@ 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 %}

{% 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 %}
Expand Down Expand Up @@ -116,16 +110,15 @@ 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)
```
{% endtab %}
{% endtabs %}


## More resources

* Other examples of "For comprehension" in the [Scala Book](/overviews/scala-book/for-expressions.html)