You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/collections-methods.md
+15-17
Original file line number
Diff line number
Diff line change
@@ -51,9 +51,9 @@ a.takeRight(2) // List(40, 10)
51
51
```
52
52
53
53
54
-
### Lambdas and HOFs
54
+
### Higher-order functions and lambdas
55
55
56
-
Next, we’ll show some commonly used higher-order functions (HOFs) that accept _lambda_ methods. To get started, here are several variations of the lambda syntax, starting with the longest form, working in steps towards the most concise form:
56
+
Next, we’ll show some commonly used higher-order functions (HOFs) that accept lambdas (anonymous functions). To get started, here are several variations of the lambda syntax, starting with the longest form, working in steps towards the most concise form:
A great thing about the functional methods on collections is that you can chain them together to solve problems. For instance, this example shows how to chain `filter` and `map`:
175
+
176
+
```scala
177
+
oneToTen.filter(_ <4).map(_ *10)
178
+
```
179
+
180
+
The REPL shows the result:
181
+
182
+
```scala
183
+
scala> oneToTen.filter(_ <4).map(_ *10)
184
+
valres1:List[Int] =List(10, 20, 30)
185
+
```
186
+
174
187
175
188
176
189
## `foreach`
@@ -185,21 +198,6 @@ chris
185
198
david
186
199
```
187
200
188
-
A great thing about the functional methods on the collections is that you can chain them together to solve problems. For example, this is one way to print the first three elements from `oneToTen`:
Copy file name to clipboardExpand all lines: _overviews/scala3-book/domain-modeling-fp.md
+13-2
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,9 @@ This chapter provides an introduction to domain modeling using functional progra
14
14
- Case classes
15
15
- Traits
16
16
17
+
>If you’re not familiar with algebraic data types (ADTs) and their generalized version (GADTs), you may want to read the [Algebraic Data Types][adts] section before reading this section.
To compute the price of the crust we simultanously pattern match on both the size and the type of the crust.
162
+
To compute the price of the crust we simultaneously pattern match on both the size and the type of the crust.
160
163
161
164
> An important point about all functions shown above is that they are *pure functions*: they do not mutate any data or have other side-effects (like throwing exceptions or writing to a file). All they do is simply receive values and compute the result.
162
165
@@ -174,7 +177,11 @@ These different solutions are shown in the remainder of this section.
174
177
175
178
### Companion Object
176
179
177
-
A first approach is to define the behavior — the functions — in a companion object. With this approach, in addition to the enumeration or case class you also define an equally named companion object that contains the behavior.
180
+
A first approach is to define the behavior — the functions — in a companion object.
181
+
182
+
>As discussed in the Domain Modeling [Tools section][modeling-tools], a _companion object_ is an `object` that has the same name as a class, and is declared in the same file as the class.
183
+
184
+
With this approach, in addition to the enumeration or case class you also define an equally named companion object that contains the behavior.
178
185
179
186
```scala
180
187
caseclassPizza(
@@ -410,3 +417,7 @@ Then, to model the behavior define functions that operate on values of your data
410
417
- You can use a modular programming style, separating interface and implementation
411
418
- You can use a “functional objects” approach and store the methods on the defined data type
412
419
- You can use extension methods to equip your data model with functionality
420
+
421
+
422
+
[adts]: {% link _overviews/scala3-book/types-adts-gadts.md %}
423
+
[modeling-tools]: {% link _overviews/scala3-book/domain-modeling-tools.md %}
0 commit comments