-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathKVSLoggingApp.scala
53 lines (45 loc) · 1.31 KB
/
KVSLoggingApp.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
import cats.Monad
import cats.implicits._
import scala.language.higherKinds
@SuppressWarnings(Array("org.wartremover.warts.Any"))
object KVSLoggingApp extends App {
import Maths.ops._
import Logger.ops._
import KVStore.ops._
/**
* Example of composing multiple languages.
*
* Here we have Logging and KVStore algebras mixed together and using a for-comprehension!
*/
def program1[F[_]: Monad: KVStore: Logger] = {
for {
_ <- KVStore.put("wild-cats", 2)
_ <- KVStore.update[Int, Int]("wild-cats", _ + 12)
_ <- KVStore.put("tame-cats", 5)
n <- KVStore.get[Int]("wild-cats")
_ <- Logger.info(n.toString)
_ <- KVStore.delete("tame-cats")
} yield n
}
/**
* Here we compose another DSL (MathOps) into our original composed program1
*/
def program2[F[_]: Monad: KVStore: Logger: Maths] = {
for {
maybeX <- program1[F]
x = maybeX.getOrElse(1)
maybeY <- program1[F]
y = maybeY.getOrElse(2)
z <- Maths.add(Maths.int(x), Maths.int(y))
_ <- KVStore.put("wild-cats", z)
} yield z
}
import KVStore.KVStoreState
val prog = for {
r1 <- program1[KVStoreState]
_ = println(s"Result 1: $r1")
r2 <- program2[KVStoreState]
_ = println(s"Result 2: $r2")
} yield ()
val _ = prog.run(Map.empty).value
}