Skip to content

add code tabs in num13. #2586

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
Oct 7, 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
50 changes: 41 additions & 9 deletions _overviews/scala3-book/taste-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -31,14 +31,18 @@ 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

Once you have a populated list, the following examples show some of the methods you can call on it.
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)
Expand All @@ -61,44 +65,72 @@ 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)

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

Expand All @@ -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