@@ -12,32 +12,32 @@ package ch09
12
12
despite the fact that we haven’t directly implemented flatMap or map on Tree.
13
13
14
14
Don't feel you have to make tailRecM tail-recursive. Doing so is quite difficult.
15
- */
16
- enum Tree [A ]:
17
- case Leaf (value : A )
18
- case Branch (left : Tree [A ], right : Tree [A ])
15
+ */
16
+ enum Tree [A ]:
17
+ case Leaf (value : A )
18
+ case Branch (left : Tree [A ], right : Tree [A ])
19
19
20
- object Tree :
21
- import Tree .*
20
+ object Tree :
21
+ import Tree .*
22
22
23
- given cats .Monad [Tree ]:
24
- override def pure [A ](x : A ): Tree [A ] =
25
- Leaf (x)
23
+ given cats .Monad [Tree ]:
24
+ override def pure [A ](x : A ): Tree [A ] =
25
+ Leaf (x)
26
26
27
- override def flatMap [A , B ](t : Tree [A ])(f : A => Tree [B ]): Tree [B ] =
27
+ override def flatMap [A , B ](t : Tree [A ])(f : A => Tree [B ]): Tree [B ] =
28
28
t match
29
- case Leaf (x) => f(x)
30
- case Branch (l, r) => Branch (flatMap(l)(f), flatMap(r)(f))
29
+ case Leaf (x) => f(x)
30
+ case Branch (l, r) => Branch (flatMap(l)(f), flatMap(r)(f))
31
31
32
- // Not stack-safe!
33
- override def tailRecM [A , B ](a : A )(f : A => Tree [Either [A , B ]]): Tree [B ] =
32
+ // Not stack-safe!
33
+ override def tailRecM [A , B ](a : A )(f : A => Tree [Either [A , B ]]): Tree [B ] =
34
34
flatMap(f(a)):
35
- case Left (value) => tailRecM(value)(f)
36
- case Right (value) => Leaf (value)
35
+ case Left (value) => tailRecM(value)(f)
36
+ case Right (value) => Leaf (value)
37
37
38
- // Smart constructors to help the compiler.
39
- def branch [A ](left : Tree [A ], right : Tree [A ]): Tree [A ] =
40
- Branch (left, right)
38
+ // Smart constructors to help the compiler.
39
+ def branch [A ](left : Tree [A ], right : Tree [A ]): Tree [A ] =
40
+ Branch (left, right)
41
41
42
- def leaf [A ](value : A ): Tree [A ] =
43
- Leaf (value)
42
+ def leaf [A ](value : A ): Tree [A ] =
43
+ Leaf (value)
0 commit comments