forked from markhibberd/introduction-to-fp-in-scala
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWriterT.scala
86 lines (75 loc) · 2.08 KB
/
WriterT.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package patterns
import intro._, EqualSyntax._
/*
* A writer data type in transformer form that represents the pair of some
* writer content with the production of a value in some effect M.
*/
case class WriterT[M[_], W, A](run: M[(W, A)]) {
/*
* Exercise 6.1:
*
* Implement map for WriterT[M, W, A].
*
* The following laws must hold:
* 1) r.map(z => z) == r
* 2) r.map(z => f(g(z))) == r.map(g).map(f)
*
*/
def map[B](f: A => B)(implicit W: Monoid[W], M: Monad[M]): WriterT[M, W, B] =
???
/*
* Exercise 6.2:
*
* Implement flatMap (a.k.a. bind, a.k.a. >>=).
*
* The following law must hold:
* r.flatMap(f).flatMap(g) == r.flatMap(z => f(z).flatMap(g))
*
*/
def flatMap[B](f: A => WriterT[M, W, B])(implicit W: Monoid[W], M: Monad[M]): WriterT[M, W, B] =
???
}
object WriterT {
/*
* Exercise 6.3:
*
* Implement a convenience for constructing a WriterT with
* value and writer content.
*
*/
def writer[M[_]: Monad, W, A](a: A)(w: W): WriterT[M, W, A] =
???
/*
* Exercise 6.4:
*
* Implement value (a.k.a. return, point, pure) given a
* Monoid for W.
*/
def value[M[_]: Monad, W: Monoid, A](a: => A): WriterT[M, W, A] =
???
/*
* Exercise 6.5:
*
* Implement tell.
*
* Tell appends the writer content w and produces no value.
*/
def tell[M[_]: Monad, W](w: W): WriterT[M, W, Unit] =
???
implicit def WriterTMonad[F[_], W](implicit F: Monad[F], W: Monoid[W]): Monad[WriterT[F, W, ?]] =
new Monad[WriterT[F, W, ?]] {
def point[A](a: => A) = WriterT(F.point((W.identity, a)))
def bind[A, B](a: WriterT[F, W, A])(f: A => WriterT[F, W, B]): WriterT[F, W, B] = a flatMap f
}
implicit def WriterTEqual[F[_], W, A](implicit E: Equal[F[(W, A)]]): Equal[WriterT[F, W, A]] =
Equal.from[WriterT[F, W, A]]((a, b) => a.run === b.run)
/*
* Exercise 6.6:
*
* Implement monad trans instance.
*
* Hint: Try using WriterT constructor and Monad[M].map(ga).
*/
implicit def WriterTMonadTrans[W:Monoid]: MonadTrans[WriterT[?[_], W, ?]] =
???
}