From a43124f7e74b00989a984330918d924f27e61223 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Thu, 6 Oct 2022 19:43:58 +0800 Subject: [PATCH 1/3] add code tabs in num13. --- _overviews/scala3-book/taste-collections.md | 38 +++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/_overviews/scala3-book/taste-collections.md b/_overviews/scala3-book/taste-collections.md index 477d59ad68..b1ed865206 100644 --- a/_overviews/scala3-book/taste-collections.md +++ b/_overviews/scala3-book/taste-collections.md @@ -9,17 +9,16 @@ next-page: taste-contextual-abstractions --- - The Scala library has a rich set of collection classes, and those classes have a rich set of methods. Collections classes are available in both immutable and mutable forms. - - ## Creating lists To give you a taste of how these work, here are some examples that use the `List` class, which is an immutable, linked-list class. These examples show different ways to create a populated `List`: +{% tabs collection_1 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_1 %} ```scala val a = List(1, 2, 3) // a: List[Int] = List(1, 2, 3) @@ -30,8 +29,8 @@ val e = (1 until 5).toList // e: List[Int] = List(1, 2, 3, 4) val f = List.range(1, 5) // f: List[Int] = List(1, 2, 3, 4) val g = List.range(1, 10, 3) // g: List[Int] = List(1, 4, 7) ``` - - +{% endtab %} +{% endtabs %} ## `List` methods @@ -39,6 +38,8 @@ Once you have a populated list, the following examples show some of the methods Notice that these are all functional methods, meaning that they don’t mutate the collection they’re called on, but instead return a new collection with the updated elements. The result that’s returned by each expression is shown in the comment on each line: +{% tabs collection_2 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_2 %} ```scala // a sample list val a = List(10, 20, 30, 40, 10) // List(10, 20, 30, 40, 10) @@ -60,45 +61,63 @@ val nums = List("one", "two") nums.map(_.toUpperCase) // List("ONE", "TWO") nums.flatMap(_.toUpperCase) // List('O', 'N', 'E', 'T', 'W', 'O') ``` +{% endtab %} +{% endtabs %} These examples show how the “foldLeft” and “reduceLeft” methods are used to sum the values in a sequence of integers: +{% tabs collection_3 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_3 %} ```scala val firstTen = (1 to 10).toList // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) firstTen.reduceLeft(_ + _) // 55 firstTen.foldLeft(100)(_ + _) // 155 (100 is a “seed” value) ``` +{% endtab %} +{% endtabs %} There are many more methods available to Scala collections classes, and they’re demonstrated in the [Collections chapter][collections], and in the [API Documentation][api]. - - ## Tuples The Scala _tuple_ is a type that lets you easily put a collection of different types in the same container. For example, given this `Person` case class: +{% tabs collection_4 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_4 %} ```scala case class Person(name: String) ``` +{% endtab %} +{% endtabs %} This is how you create a tuple that contains an `Int`, a `String`, and a custom `Person` value: +{% tabs collection_5 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_5 %} ```scala val t = (11, "eleven", Person("Eleven")) ``` +{% endtab %} +{% endtabs %} Once you have a tuple, you can access its values by binding them to variables, or access them by number: +{% tabs collection_6 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_6 %} ```scala t(0) // 11 t(1) // "eleven" t(2) // Person("Eleven") ``` +{% endtab %} +{% endtabs %} You can also use this _extractor_ approach to assign the tuple fields to variable names: +{% tabs collection_7 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=collection_7 %} ```scala val (num, str, person) = t @@ -107,13 +126,12 @@ val (num, str, person) = t // val str: String = eleven // val person: Person = Person(Eleven) ``` +{% endtab %} +{% endtabs %} Tuples are nice for those times when you want to put a collection of heterogeneous types in a little collection-like structure. See the [Reference documentation][reference] for more tuple details. - - - [collections]: {% link _overviews/scala3-book/collections-intro.md %} [api]: https://scala-lang.org/api/3.x/ [reference]: {{ site.scala3ref }}/overview.html From c6b1220db9f7320a954aa75efe1817c35e6d1522 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:35:00 +0800 Subject: [PATCH 2/3] Update _overviews/scala3-book/taste-collections.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-collections.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-collections.md b/_overviews/scala3-book/taste-collections.md index b1ed865206..fd8549b5d7 100644 --- a/_overviews/scala3-book/taste-collections.md +++ b/_overviews/scala3-book/taste-collections.md @@ -17,7 +17,7 @@ Collections classes are available in both immutable and mutable forms. To give you a taste of how these work, here are some examples that use the `List` class, which is an immutable, linked-list class. These examples show different ways to create a populated `List`: -{% tabs collection_1 class=tabs-scala-version %} +{% tabs collection_1 %} {% tab 'Scala 2 and 3' for=collection_1 %} ```scala val a = List(1, 2, 3) // a: List[Int] = List(1, 2, 3) From bb8f701f1e4a4402e845b9c5c525fb7db9b5adfa Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:37:55 +0800 Subject: [PATCH 3/3] correct 2&3 and 3 only tabs. --- _overviews/scala3-book/taste-collections.md | 26 ++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/_overviews/scala3-book/taste-collections.md b/_overviews/scala3-book/taste-collections.md index fd8549b5d7..bcaca071d0 100644 --- a/_overviews/scala3-book/taste-collections.md +++ b/_overviews/scala3-book/taste-collections.md @@ -19,6 +19,7 @@ These examples show different ways to create a populated `List`: {% tabs collection_1 %} {% tab 'Scala 2 and 3' for=collection_1 %} + ```scala val a = List(1, 2, 3) // a: List[Int] = List(1, 2, 3) @@ -29,6 +30,7 @@ val e = (1 until 5).toList // e: List[Int] = List(1, 2, 3, 4) val f = List.range(1, 5) // f: List[Int] = List(1, 2, 3, 4) val g = List.range(1, 10, 3) // g: List[Int] = List(1, 4, 7) ``` + {% endtab %} {% endtabs %} @@ -38,8 +40,9 @@ Once you have a populated list, the following examples show some of the methods Notice that these are all functional methods, meaning that they don’t mutate the collection they’re called on, but instead return a new collection with the updated elements. The result that’s returned by each expression is shown in the comment on each line: -{% tabs collection_2 class=tabs-scala-version %} +{% tabs collection_2 %} {% tab 'Scala 2 and 3' for=collection_2 %} + ```scala // a sample list val a = List(10, 20, 30, 40, 10) // List(10, 20, 30, 40, 10) @@ -61,19 +64,22 @@ val nums = List("one", "two") nums.map(_.toUpperCase) // List("ONE", "TWO") nums.flatMap(_.toUpperCase) // List('O', 'N', 'E', 'T', 'W', 'O') ``` + {% endtab %} {% endtabs %} These examples show how the “foldLeft” and “reduceLeft” methods are used to sum the values in a sequence of integers: -{% tabs collection_3 class=tabs-scala-version %} +{% tabs collection_3 %} {% tab 'Scala 2 and 3' for=collection_3 %} + ```scala val firstTen = (1 to 10).toList // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) firstTen.reduceLeft(_ + _) // 55 firstTen.foldLeft(100)(_ + _) // 155 (100 is a “seed” value) ``` + {% endtab %} {% endtabs %} @@ -84,40 +90,47 @@ There are many more methods available to Scala collections classes, and they’r The Scala _tuple_ is a type that lets you easily put a collection of different types in the same container. For example, given this `Person` case class: -{% tabs collection_4 class=tabs-scala-version %} +{% tabs collection_4 %} {% tab 'Scala 2 and 3' for=collection_4 %} + ```scala case class Person(name: String) ``` + {% endtab %} {% endtabs %} This is how you create a tuple that contains an `Int`, a `String`, and a custom `Person` value: -{% tabs collection_5 class=tabs-scala-version %} +{% tabs collection_5 %} {% tab 'Scala 2 and 3' for=collection_5 %} + ```scala val t = (11, "eleven", Person("Eleven")) ``` + {% endtab %} {% endtabs %} Once you have a tuple, you can access its values by binding them to variables, or access them by number: -{% tabs collection_6 class=tabs-scala-version %} +{% tabs collection_6 %} {% tab 'Scala 2 and 3' for=collection_6 %} + ```scala t(0) // 11 t(1) // "eleven" t(2) // Person("Eleven") ``` + {% endtab %} {% endtabs %} You can also use this _extractor_ approach to assign the tuple fields to variable names: -{% tabs collection_7 class=tabs-scala-version %} +{% tabs collection_7 %} {% tab 'Scala 2 and 3' for=collection_7 %} + ```scala val (num, str, person) = t @@ -126,6 +139,7 @@ val (num, str, person) = t // val str: String = eleven // val person: Person = Person(Eleven) ``` + {% endtab %} {% endtabs %}