Skip to content

Commit b9e912c

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

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

Diff for: scala_features/src/main/scala/app/impl/scala/FoldFunction.scala

+16-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class FoldFunction {
6969
}
7070

7171

72-
7372
/**
7473
* If someValue it´s empty fold will return the Value, otherwise it will execute the second function
74+
*
7575
* @param mainClass
7676
* @param someValue
7777
* @return
@@ -130,4 +130,19 @@ class FoldFunction {
130130
}
131131
println(formatPath)
132132
}
133+
134+
/**
135+
* Using foldRight we do the other way around of the previous test.
136+
*/
137+
@Test
138+
def foldLeftRightMap() = {
139+
val map: Map[String, String] = Map()
140+
141+
val newMap = keyValue.foldRight(map)((entry, map) => map ++ Map(entry._1 -> entry._2))
142+
println(newMap)
143+
144+
val newMap1 = keyValue.foldLeft(map)((map, entry) => map ++ Map(entry._1 -> entry._2))
145+
println(newMap1)
146+
147+
}
133148
}

Diff for: scala_features/src/main/scala/app/impl/scala/TypeClasses.scala

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class TypeClasses {
3737

3838
implicit val stringName: TypeClass[String] = TypeClass(clazz => clazz.getClass.getName)
3939

40+
// implicit val longName: TypeClass[Long] = TypeClass(clazz => clazz.getClass.getName)
41+
4042

4143
def getClassName[T](className: T)(implicit typeClass: TypeClass[T]): String = {
4244
typeClass.className(className)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package app.impl.scalaz
2+
3+
import org.junit.Test
4+
5+
/**
6+
* Created by pabloperezgarcia on 09/11/2017.
7+
*/
8+
class TraverseFeature {
9+
10+
val pf: Int => Option[Int] = PartialFunction.condOpt(_) { case 3 => 30; case 4 => 40 }
11+
12+
13+
@Test
14+
def main(): Unit = {
15+
// List(1, 2, 3, 4).traverse(pf)
16+
// List(3, 4).traverse(pf)
17+
}
18+
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package app.impl.scalaz
2+
3+
import org.junit.Test
4+
5+
/**
6+
* Created by pabloperezgarcia on 09/11/2017.
7+
*/
8+
class WriterMonad {
9+
10+
case class Writer[A, D](value: A, diary: D)(implicit m: MonadId[D]) {
11+
12+
def flatMap[B](f: A => Writer[B, D]) = f(value) match {
13+
case Writer(result, d) => Writer(result, m.append(diary, d))
14+
}
15+
16+
def map[B](f: A => B) = Writer[B, D](f(value), diary)
17+
}
18+
19+
trait MonadId[T] {
20+
def zero: T
21+
22+
def append(a: T, b: T): T
23+
}
24+
25+
object MonadId {
26+
implicit val StringMonoid = new MonadId[String] {
27+
override def zero = ""
28+
29+
override def append(s1: String, s2: String) = s1 + s2
30+
}
31+
32+
implicit val IntMonoid = new MonadId[Int] {
33+
override def zero = 0
34+
35+
override def append(s1: Int, s2: Int) = s1 * s2
36+
}
37+
38+
implicit val FooMonoid = new MonadId[Foo] {
39+
override def zero = Foo("")
40+
41+
override def append(s1: Foo, s2: Foo) = Foo(s1.value.concat(s2.value))
42+
}
43+
}
44+
45+
@Test
46+
def stringWriter(): Unit = {
47+
val result = Writer(5, "")
48+
.flatMap(num => Writer(num + 3, "added three"))
49+
println(result.value)
50+
println(result.diary)
51+
}
52+
53+
@Test
54+
def intWriter(): Unit = {
55+
val result = Writer(5, 5)
56+
.flatMap(num => Writer(num + 5, 10))
57+
println(result.value)
58+
println(result.diary)
59+
}
60+
61+
@Test
62+
def fooWriter(): Unit = {
63+
val result = Writer(Foo("hello"), Foo(" writer"))
64+
.flatMap(foo => Writer(foo, Foo(" world")))
65+
println(result.value)
66+
println(result.diary)
67+
}
68+
69+
case class Foo(value: String)
70+
71+
}

0 commit comments

Comments
 (0)