Skip to content

Commit 5743cb4

Browse files
ZH11ZH11
ZH11
authored and
ZH11
committed
State monad
1 parent b9e912c commit 5743cb4

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

.idea/dictionaries/pabloperezgarcia.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ All the most common features of Observable
7272
* **[Functor](scala_features/src/main/scala/app/impl/scalaz/FunctorFeature.scala)**
7373
* **[Monad laws](scala_features/src/main/scala/app/impl/scalaz/MonadLaws.scala)**
7474
* **[Free monads](scala_features/src/main/scala/app/impl/scalaz/FreeMonad.scala)**
75+
* **[Writer monad](scala_features/src/main/scala/app/impl/scalaz/WriterMonad.scala)**
7576
* **[Reader monad](scala_features/src/main/scala/app/impl/scalaz/ReaderMonad.scala)**
7677
* **[State monad](scala_features/src/main/scala/app/impl/scalaz/StateMonad.scala)**
7778
* **[Monad transformer](scala_features/src/main/scala/app/impl/scalaz/MonadTransformer.scala)**

scala_features/src/main/scala/app/impl/scalaz/WriterMonad.scala

+36-20
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,83 @@ package app.impl.scalaz
33
import org.junit.Test
44

55
/**
6-
* Created by pabloperezgarcia on 09/11/2017.
6+
* Created by pabloperezgarcia on 09/11/2017..
7+
*
8+
* With Scalaz writer patter basically we can create our own builder patter.
9+
*
10+
* Applying the flatMap function we receive a type class which we append the old value with the new value, giving us
11+
* a last type class with all appended values.
12+
*
13+
* We create our monoids which using also type class pattern we can give the implementation depending the type class we use.
714
*/
815
class WriterMonad {
916

10-
case class Writer[A, D](value: A, diary: D)(implicit m: MonadId[D]) {
17+
case class Writer[A, D](value: A, appendValue: D)(implicit m: Monoid[D]) {
1118

1219
def flatMap[B](f: A => Writer[B, D]) = f(value) match {
13-
case Writer(result, d) => Writer(result, m.append(diary, d))
20+
case Writer(result, newAppendValue) =>
21+
Writer(result, m.append(appendValue, newAppendValue))
1422
}
1523

16-
def map[B](f: A => B) = Writer[B, D](f(value), diary)
24+
def map[B](f: A => B) = Writer[B, D](f(value), appendValue)
1725
}
1826

19-
trait MonadId[T] {
27+
trait Monoid[T] {
2028
def zero: T
2129

2230
def append(a: T, b: T): T
2331
}
2432

25-
object MonadId {
26-
implicit val StringMonoid = new MonadId[String] {
33+
object Monoid {
34+
implicit val StringMonoid = new Monoid[String] {
2735
override def zero = ""
2836

2937
override def append(s1: String, s2: String) = s1 + s2
3038
}
3139

32-
implicit val IntMonoid = new MonadId[Int] {
40+
implicit val IntMonoid = new Monoid[Int] {
3341
override def zero = 0
3442

35-
override def append(s1: Int, s2: Int) = s1 * s2
43+
override def append(s1: Int, s2: Int) = s1 + s2
3644
}
3745

38-
implicit val FooMonoid = new MonadId[Foo] {
46+
implicit val FooMonoid = new Monoid[Foo] {
3947
override def zero = Foo("")
4048

41-
override def append(s1: Foo, s2: Foo) = Foo(s1.value.concat(s2.value))
49+
override def append(s1: Foo, s2: Foo) = Foo(s1.value + s2.value)
4250
}
4351
}
4452

4553
@Test
4654
def stringWriter(): Unit = {
47-
val result = Writer(5, "")
48-
.flatMap(num => Writer(num + 3, "added three"))
49-
println(result.value)
50-
println(result.diary)
55+
val result = Writer(0, "")
56+
.flatMap(num => Writer(num + 1, " page 1"))
57+
.flatMap(num => Writer(num + 1, " page 2"))
58+
.flatMap(num => Writer(num + 1, " page 3"))
59+
println(s"Total number of pages:${result.value}")
60+
println(result.appendValue)
5161
}
5262

5363
@Test
5464
def intWriter(): Unit = {
55-
val result = Writer(5, 5)
56-
.flatMap(num => Writer(num + 5, 10))
65+
val result = Writer(1, 1)
66+
.flatMap(num => Writer(num + 1, 10))
67+
.flatMap(num => Writer(num + 1, 20))
68+
.flatMap(num => Writer(num + 1, 30))
69+
.flatMap(num => Writer(num + 1, 40))
5770
println(result.value)
58-
println(result.diary)
71+
println(result.appendValue)
5972
}
6073

6174
@Test
6275
def fooWriter(): Unit = {
63-
val result = Writer(Foo("hello"), Foo(" writer"))
76+
val result = Writer(Foo(""), Foo(""))
77+
.flatMap(foo => Writer(foo, Foo(" hello")))
78+
.flatMap(foo => Writer(foo, Foo(" writer")))
79+
.flatMap(foo => Writer(foo, Foo(" monad")))
6480
.flatMap(foo => Writer(foo, Foo(" world")))
6581
println(result.value)
66-
println(result.diary)
82+
println(result.appendValue)
6783
}
6884

6985
case class Foo(value: String)

0 commit comments

Comments
 (0)