22# chapter [Monads] <sec:monads> 
33
44
5- *Monads*  are one of the most common abstractions in Scala.
6- Many Scala programmers quickly become intuitively familiar with monads,
5+ *Monads*  are one of the best known abstractions in functional programming,
6+ but also the one that perhaps leads to the most confusion.
7+ Many programmers have used and are intuitively familiar with monads,
78even if we don't know them by name.
89
9- Informally, a  monad is  anything with a constructor and a `flatMap`  method.
10+ A  monad in Scala is, informally,  anything with a constructor and a `flatMap`  method.
1011All of the functors we saw in the last chapter are also monads,
1112including `Option` , `List` , and `Future` .
1213We even have special syntax to support monads: for comprehensions.
@@ -23,17 +24,15 @@ Finally, we'll tour some interesting monads that you may not have seen,
2324providing introductions and examples of their use.
2425
2526
26- == What is a Monad? 
2727
28+ == What is a Monad? 
2829
2930This is the question that has been posed in a thousand blog posts,
3031with explanations and analogies involving concepts as diverse as
3132cats, Mexican food, space suits full of toxic waste,
3233and monoids in the category of endofunctors (whatever that means).
3334We're going to solve the problem of explaining monads once and for all
34- by stating very simply:
35- 
36- > A monad is a mechanism for _sequencing computations_ .
35+ by stating very simply: a monad is a mechanism for _sequencing computations_ .
3736
3837That was easy! Problem solved, right?
3938But then again, last chapter we said functors
@@ -45,7 +44,7 @@ we said that functors allow us
4544to sequence computations ignoring some complication.
4645However, functors are limited in that
4746they only allow this complication
48- to occur once at the beginning of the sequence.
47+ to occur once,  at the beginning of the sequence.
4948They don't account for further complications
5049at each step in the sequence.
5150
@@ -61,8 +60,8 @@ allowing us to `flatMap` again.
6160Let's ground things by looking at some examples.
6261
6362
64- === Options as Monads 
6563
64+ === Options as Monads 
6665
6766`Option`  allows us to sequence computations
6867that may or may not return values.
@@ -121,7 +120,7 @@ stringDivideBy("bar", "2")
121120Every monad is also a functor (see below for proof),
122121so we can rely on both `flatMap`  and `map` 
123122to sequence computations
124- that do and don't introduce a new monad.
123+ that do and don't introduce a new monad respectively .
125124Plus, if we have both `flatMap`  and `map` 
126125we can use for comprehensions
127126to clarify the sequencing behaviour:
@@ -143,8 +142,8 @@ def stringDivideBy(aStr: String, bStr: String): Option[Int] =
143142``` 
144143
145144
146- === Lists as Monads 
147145
146+ === Lists as Monads 
148147
149148When we first encounter `flatMap`  as budding Scala developers,
150149we tend to think of it as a pattern for iterating over `Lists` .
@@ -174,23 +173,22 @@ which states the sequence of operations:
174173- get `y` 
175174- create a tuple `(x, y)` 
176175
177- <!--
178176The type chart in @fig:monads:list-type-chart 
179- illustrates this behaviour[^list-lengths].
177+ illustrates this behaviour# footnote [
178+ Although the result of `flatMap`  (`List[B]` )
179+ is the same type as the result of the user-supplied function,
180+ the end result is actually a larger list
181+ created from combinations of intermediate `As`  and `Bs` .
182+ ]. 
180183
181184# figure (
182185    image (" list-flatmap.svg"  , width : 80% ),
183186    caption : [Type chart: flatMap for List]
184187)<fig:monads:list-type-chart> 
185188
186- [^list-lengths]: Although the result of `flatMap`  (`List[B]` )
187- is the same type as the result of the user-supplied function,
188- the end result is actually a larger list
189- created from combinations of intermediate `As`  and `Bs` .
190- -->
191189
192- === Futures as Monads 
193190
191+ === Futures as Monads 
194192
195193`Future`  is a monad that sequences computations
196194without worrying that they may be asynchronous:
@@ -252,27 +250,29 @@ but that is another story and shall be told another time.
252250Monads are all about sequencing.
253251
254252
255- === Definition of a Monad 
256253
254+ === Definition of a Monad 
257255
258256While we have only talked about `flatMap`  above,
259257monadic behaviour is formally captured in two operations:
260258
261259- `pure` , of type `A => F[A]` ;
262- - `flatMap` [^bind], of type `(F[A], A => F[B]) => F[B]` .
263- 
264- [^bind]: In the programming literature and Haskell,
265- `pure`  is referred to as `point`  or `return`  and
266- `flatMap`  is referred to as `bind`  or `>>=` .
260+ - `flatMap` # footnote [
261+ In the programming literature and Haskell,
262+ `pure`  is often referred to as `point`  or `return`  and
263+ `flatMap`  is often referred to as `bind`  or `>>=` .
267264This is purely a difference in terminology.
268265We'll use the term `flatMap`  for compatibility
269266with Cats and the Scala standard library.
267+ ], of type `(F[A], A => F[B]) => F[B]` .
270268
271269`pure`  abstracts over constructors,
272270providing a way to create a new monadic context from a plain value.
273271`flatMap`  provides the sequencing step we have already discussed,
274272extracting the value from a context and generating
275273the next context in the sequence.
274+ 
275+ We can represent this as a type class.
276276Here is a simplified version of the `Monad`  type class in Cats:
277277
278278```scala mdoc:silent 
@@ -284,10 +284,7 @@ trait Monad[F[_]] {
284284} 
285285``` 
286286
287- # warning [
288- ==== Monad Laws {-} 
289- 
290- 
287+ # info (title : " Monad Laws"  )[
291288`pure`  and `flatMap`  must obey a set of laws
292289that allow us to sequence operations freely
293290without unintended glitches and side-effects:
@@ -319,7 +316,7 @@ m.flatMap(f).flatMap(g) == m.flatMap(x => f(x).flatMap(g))
319316
320317
321318Every monad is also a functor.
322- We  can define `map`  in the same way for every monad 
319+ For every monad we  can define `map`  in the same way
323320using the existing methods, `flatMap`  and `pure` :
324321
325322```scala mdoc:silent:reset-object 
0 commit comments