diff --git a/_overviews/scala3-book/taste-collections.md b/_overviews/scala3-book/taste-collections.md index 477d59ad68..bcaca071d0 100644 --- a/_overviews/scala3-book/taste-collections.md +++ b/_overviews/scala3-book/taste-collections.md @@ -9,17 +9,17 @@ 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 %} +{% tab 'Scala 2 and 3' for=collection_1 %} + ```scala val a = List(1, 2, 3) // a: List[Int] = List(1, 2, 3) @@ -31,7 +31,8 @@ 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 +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 %} +{% 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,8 +65,14 @@ 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 %} +{% 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) @@ -70,35 +80,57 @@ firstTen.reduceLeft(_ + _) // 55 firstTen.foldLeft(100)(_ + _) // 155 (100 is a “seed” value) ``` -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]. - +{% 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 %} +{% 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 %} +{% 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 %} +{% 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 %} +{% tab 'Scala 2 and 3' for=collection_7 %} + ```scala val (num, str, person) = t @@ -108,12 +140,12 @@ val (num, str, person) = t // 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