Skip to content

Add code tabs in fun-function-variables.md #2623

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
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
48 changes: 48 additions & 0 deletions _overviews/scala3-book/fun-function-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,119 @@ next-page: fun-eta-expansion

Going back to this example from the previous section:

{% tabs fun-function-variables-1 %}
{% tab 'Scala 2 and 3' %}
```scala
val doubledInts = ints.map((i: Int) => i * 2)
```
{% endtab %}
{% endtabs %}

We noted that this part of the expression is an anonymous function:

{% tabs fun-function-variables-2 %}
{% tab 'Scala 2 and 3' %}
```scala
(i: Int) => i * 2
```
{% endtab %}
{% endtabs %}

The reason it’s called *anonymous* is because it’s not assigned to a variable, and therefore doesn’t have a name.

However, an anonymous function---also known as a *function literal*---can be assigned to a variable to create a *function variable*:

{% tabs fun-function-variables-3 %}
{% tab 'Scala 2 and 3' %}
```scala
val double = (i: Int) => i * 2
```
{% endtab %}
{% endtabs %}

This creates a function variable named `double`.
In this expression, the original function literal is on the right side of the `=` symbol:

{% tabs fun-function-variables-4 %}
{% tab 'Scala 2 and 3' %}
```scala
val double = (i: Int) => i * 2
-----------------
```
{% endtab %}
{% endtabs %}

the new variable name is on the left side:

{% tabs fun-function-variables-5 %}
{% tab 'Scala 2 and 3' %}
```scala
val double = (i: Int) => i * 2
------
```
{% endtab %}
{% endtabs %}

and the function’s parameter list is underlined here:

{% tabs fun-function-variables-6 %}
{% tab 'Scala 2 and 3' %}
```scala
val double = (i: Int) => i * 2
--------
```
{% endtab %}
{% endtabs %}

Like the parameter list for a method, this means that the `double` function takes one parameter, an `Int` named `i`.
You can see in the REPL that `double` has the type `Int => Int`, meaning that it takes a single `Int` parameter and returns an `Int`:

{% tabs fun-function-variables-7 %}
{% tab 'Scala 2 and 3' %}
```scala
scala> val double = (i: Int) => i * 2
val double: Int => Int = ...
```
{% endtab %}
{% endtabs %}


### Invoking the function

Now you can call the `double` function like this:

{% tabs fun-function-variables-8 %}
{% tab 'Scala 2 and 3' %}
```scala
val x = double(2) // 4
```
{% endtab %}
{% endtabs %}

You can also pass `double` into a `map` call:

{% tabs fun-function-variables-9 %}
{% tab 'Scala 2 and 3' %}
```scala
List(1, 2, 3).map(double) // List(2, 4, 6)
```
{% endtab %}
{% endtabs %}

Furthermore, when you have other functions of the `Int => Int` type:

{% tabs fun-function-variables-10 %}
{% tab 'Scala 2 and 3' %}
```scala
val triple = (i: Int) => i * 3
```
{% endtab %}
{% endtabs %}

you can store them in a `List` or `Map`:

{% tabs fun-function-variables-11 %}
{% tab 'Scala 2 and 3' %}
```scala
val functionList = List(double, triple)

Expand All @@ -91,9 +133,13 @@ val functionMap = Map(
"3x" -> triple
)
```
{% endtab %}
{% endtabs %}

If you paste those expressions into the REPL, you’ll see that they have these types:

{% tabs fun-function-variables-12 %}
{% tab 'Scala 2 and 3' %}
````
// a List that contains functions of the type `Int => Int`
functionList: List[Int => Int]
Expand All @@ -102,6 +148,8 @@ functionList: List[Int => Int]
// values have the type `Int => Int`
functionMap: Map[String, Int => Int]
````
{% endtab %}
{% endtabs %}



Expand Down