Skip to content

Commit 1afc265

Browse files
committed
fiz 2 snippets
1 parent 2ce7288 commit 1afc265

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Diff for: _overviews/scala3-book/collections-methods.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,18 @@ val xs: List[String] = List(brandy, chris, david)
415415

416416
Pattern matching like this is useful in many situations, such as writing a `sum` method using recursion:
417417

418-
{% tabs tail-match-sum-example %}
418+
{% tabs tail-match-sum-example class=tabs-scala-version %}
419419

420-
{% tab 'Scala 2 and 3' %}
420+
{% tab 'Scala 2' %}
421+
```scala
422+
def sum(list: List[Int]): Int = list match {
423+
case Nil => 0
424+
case x :: xs => x + sum(xs)
425+
}
426+
```
427+
{% endtab %}
428+
429+
{% tab 'Scala 3' %}
421430
```scala
422431
def sum(list: List[Int]): Int = list match
423432
case Nil => 0
@@ -534,9 +543,19 @@ It takes a function (or anonymous function) and applies that function to success
534543
The best way to explain `reduce` is to create a little helper method you can pass into it.
535544
For example, this is an `add` method that adds two integers together, and also provides us some nice debug output:
536545

537-
{% tabs reduce-example %}
546+
{% tabs reduce-example class=tabs-scala-version %}
538547

539-
{% tab 'Scala 2 and 3' %}
548+
{% tab 'Scala 2' %}
549+
```scala
550+
def add(x: Int, y: Int): Int = {
551+
val theSum = x + y
552+
println(s"received $x and $y, their sum is $theSum")
553+
theSum
554+
}
555+
```
556+
{% endtab %}
557+
558+
{% tab 'Scala 3' %}
540559
```scala
541560
def add(x: Int, y: Int): Int =
542561
val theSum = x + y

0 commit comments

Comments
 (0)