-
Notifications
You must be signed in to change notification settings - Fork 7
/
FibApp.scala
150 lines (141 loc) · 5.54 KB
/
FibApp.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import cats.Monad
import cats.implicits._
import scala.language.higherKinds
@SuppressWarnings(Array("org.wartremover.warts.Any"))
object FibApp extends App {
/**
* An example of composing 3 DSLs: Maths, Caching, Logging.
*
* Recursively calls itself as a bonus
*/
def cachedFib[F[_]: Monad: KVStore: Maths: Logger](i: Int): F[Int] = {
// Import the auto-generated methods
import KVStore.ops._, Logger.ops._, Maths.ops._
i match {
case _ if i <= 0 => Monad[F].pure(0)
case 1 => Monad[F].pure(1)
case _ => {
for {
fromCache <- KVStore.get[Int](i.toString)
r <- fromCache match {
case None =>
for {
_ <- Logger.warn(
s"Not found in cache, trying to find fib of ${i - 1} and ${i - 2}")
a <- cachedFib[F](i - 1) /* Recursion! */
b <- cachedFib[F](i - 2)
s <- Maths.add(Maths.int(a), Maths.int(b))
_ <- Logger.info(s"Calculated fib of $i to be $s, caching")
_ <- KVStore.put(i.toString, s)
} yield s
case Some(f) =>
for {
_ <- Logger.info(s"Found fib of $i in cache: $f")
} yield f
}
} yield r
}
}
}
import KVStore.KVStoreState
val prog = for {
r1 <- cachedFib[KVStoreState](10)
_ = println(r1)
r2 <- cachedFib[KVStoreState](30)
_ = println(r2)
r3 <- cachedFib[KVStoreState](20)
} yield ()
val _ = prog.run(Map.empty).value
/*
[info] Running FibApp
WARN: Not found in cache, trying to find fib of 9 and 8
WARN: Not found in cache, trying to find fib of 8 and 7
WARN: Not found in cache, trying to find fib of 7 and 6
WARN: Not found in cache, trying to find fib of 6 and 5
WARN: Not found in cache, trying to find fib of 5 and 4
WARN: Not found in cache, trying to find fib of 4 and 3
WARN: Not found in cache, trying to find fib of 3 and 2
WARN: Not found in cache, trying to find fib of 2 and 1
WARN: Not found in cache, trying to find fib of 1 and 0
INFO: Calculated fib of 2 to be 1, caching
INFO: Calculated fib of 3 to be 2, caching
INFO: Found fib of 2 in cache: 1
INFO: Calculated fib of 4 to be 3, caching
INFO: Found fib of 3 in cache: 2
INFO: Calculated fib of 5 to be 5, caching
INFO: Found fib of 4 in cache: 3
INFO: Calculated fib of 6 to be 8, caching
INFO: Found fib of 5 in cache: 5
INFO: Calculated fib of 7 to be 13, caching
INFO: Found fib of 6 in cache: 8
INFO: Calculated fib of 8 to be 21, caching
INFO: Found fib of 7 in cache: 13
INFO: Calculated fib of 9 to be 34, caching
INFO: Found fib of 8 in cache: 21
INFO: Calculated fib of 10 to be 55, caching
55
WARN: Not found in cache, trying to find fib of 29 and 28
WARN: Not found in cache, trying to find fib of 28 and 27
WARN: Not found in cache, trying to find fib of 27 and 26
WARN: Not found in cache, trying to find fib of 26 and 25
WARN: Not found in cache, trying to find fib of 25 and 24
WARN: Not found in cache, trying to find fib of 24 and 23
WARN: Not found in cache, trying to find fib of 23 and 22
WARN: Not found in cache, trying to find fib of 22 and 21
WARN: Not found in cache, trying to find fib of 21 and 20
WARN: Not found in cache, trying to find fib of 20 and 19
WARN: Not found in cache, trying to find fib of 19 and 18
WARN: Not found in cache, trying to find fib of 18 and 17
WARN: Not found in cache, trying to find fib of 17 and 16
WARN: Not found in cache, trying to find fib of 16 and 15
WARN: Not found in cache, trying to find fib of 15 and 14
WARN: Not found in cache, trying to find fib of 14 and 13
WARN: Not found in cache, trying to find fib of 13 and 12
WARN: Not found in cache, trying to find fib of 12 and 11
WARN: Not found in cache, trying to find fib of 11 and 10
WARN: Not found in cache, trying to find fib of 10 and 9
INFO: Found fib of 10 in cache: 55
INFO: Found fib of 9 in cache: 34
INFO: Calculated fib of 11 to be 89, caching
INFO: Found fib of 10 in cache: 55
INFO: Calculated fib of 12 to be 144, caching
INFO: Found fib of 11 in cache: 89
INFO: Calculated fib of 13 to be 233, caching
INFO: Found fib of 12 in cache: 144
INFO: Calculated fib of 14 to be 377, caching
INFO: Found fib of 13 in cache: 233
INFO: Calculated fib of 15 to be 610, caching
INFO: Found fib of 14 in cache: 377
INFO: Calculated fib of 16 to be 987, caching
INFO: Found fib of 15 in cache: 610
INFO: Calculated fib of 17 to be 1597, caching
INFO: Found fib of 16 in cache: 987
INFO: Calculated fib of 18 to be 2584, caching
INFO: Found fib of 17 in cache: 1597
INFO: Calculated fib of 19 to be 4181, caching
INFO: Found fib of 18 in cache: 2584
INFO: Calculated fib of 20 to be 6765, caching
INFO: Found fib of 19 in cache: 4181
INFO: Calculated fib of 21 to be 10946, caching
INFO: Found fib of 20 in cache: 6765
INFO: Calculated fib of 22 to be 17711, caching
INFO: Found fib of 21 in cache: 10946
INFO: Calculated fib of 23 to be 28657, caching
INFO: Found fib of 22 in cache: 17711
INFO: Calculated fib of 24 to be 46368, caching
INFO: Found fib of 23 in cache: 28657
INFO: Calculated fib of 25 to be 75025, caching
INFO: Found fib of 24 in cache: 46368
INFO: Calculated fib of 26 to be 121393, caching
INFO: Found fib of 25 in cache: 75025
INFO: Calculated fib of 27 to be 196418, caching
INFO: Found fib of 26 in cache: 121393
INFO: Calculated fib of 28 to be 317811, caching
INFO: Found fib of 27 in cache: 196418
INFO: Calculated fib of 29 to be 514229, caching
INFO: Found fib of 28 in cache: 317811
INFO: Calculated fib of 30 to be 832040, caching
832040
INFO: Found fib of 20 in cache: 6765
*/
}