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
Applying the `List.map` ability with the following mapping function as parameter: `mapping: A => B` would result in a `List[B]`.
150
+
151
+
Now, applying the `List.map` ability with the following mapping function as parameter: `mapping: A => List[B]` would result in a `List[List[B]]`.
152
+
153
+
To avoid avoid managing lists of lists, we may want to "flatten" the values in a single list.
154
+
155
+
That's where `Monad` enter the party. A `Monad` for type `F[_]` is a `Functor[F]` with 2 more abilities:
156
+
* the flatten ability we just described: turning `F[A]` to `F[B]` when given a `mapping: A => F[B]` function
157
+
* the ability to create `F[A]` from a single value `A`
158
+
159
+
Here is the translation of this definition in Scala 3:
151
160
152
-
defpure[A](x: A):F[A]
161
+
```scala
162
+
traitMonad[F[_]] extendsFunctor[F] { // "A `Monad` for type `F[_]` is a `Functor[F]`" => thus has the `map` ability
163
+
defpure[A](x: A):F[A] // `pure` can construct F[A] from a single value A
164
+
def [A, B](x: F[A]).flatMap(f: A=>F[B]):F[B] // the flattening ability is named `flatMap`, using extension methods as previous examples
165
+
def [A, B](x: F[A]).map(f: A=>B) = x.flatMap(f `andThen` pure) // the `map(f)` ability is simply a combination of applying `f` then turning the result into an `F[A]` then applying `flatMap` to it
153
166
}
167
+
```
168
+
169
+
#### List
154
170
171
+
Let us declare the `Monad` ability for type `List`
xs.flatMap(f) // let's rely on the existing `flatMap` method of `Option`
196
+
}
197
+
```
198
+
199
+
#### The Reader Monad
161
200
201
+
Another example of a `Monad` is the Reader Monad. It no longer acts on a type like `List` or `Option`, but on a function.
202
+
It can be used for example for combining functions that all have need the same type of parameter, for instance, if multiple functions need to access some configuration, context, environment variables, etc.
203
+
204
+
The Reader monad allows to abstract over such a `Config` dependency (or context, environment, ...), named `Ctx` in the following examples. It is therefore _parameterized_ by `Ctx`:
@@ -173,4 +218,4 @@ The definition of a _typeclass_ is expressed in Scala 3 via a `trait`.
173
218
The main difference with other traits resides in how these traits are implemented.
174
219
In the case of a _typeclass_ the trait's implementations are expressed through `given ... as` type definitions, and not through classes that `extends` the trait linearly.
175
220
176
-
In addition to these given instances, extension methods and context bounds allow a concise and natural expression of _typeclasses_.
221
+
In addition to these given instances, other constructs like extension methods, context bounds and type lambdas allow a concise and natural expression of _typeclasses_.
0 commit comments