forked from politrons/reactiveScala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReaderMonad.scala
51 lines (40 loc) · 1.26 KB
/
ReaderMonad.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
package app.impl.scalaz
import org.junit.Test
import scalaz.Reader
/**
* Created by pabloperezgarcia on 01/11/2017.
*
* Using Reader monad is a very cool way for DI, since you can always pass
* as the parameter between functions the element that you pass as argument in the run(Value)
* so if you have N functions that need that argument X(1981) you can just make that
* all your functions return a Reader where left type is the DI value, and the right type is the
* return type of the function
*/
class ReaderMonad {
@Test
def dependecyInjection(): Unit = {
println(runAllFunctions(1).run("1981"))
}
def runAllFunctions(value: Int) = {
for {
sumValue <- sumValues(value)
concatValue <- concatValues(sumValue)
sentence <- wrapValues(concatValue)
} yield sentence
}
def sumValues(value: Int) = Reader[String, Int] {
diValue => value + diValue.toInt
}
def concatValues(value: Int) = Reader[String, String] {
diValue => s"${value.toString} | ${diValue.toString}"
}
def wrapValues(value: String) = Reader[String, String] {
diValue => s"[[$diValue | ${value.toString}]]"
}
@Test
def main(): Unit = {
val f: Int => String = _.toString
val reader = Reader(f)
println(reader.run(123))
}
}