-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.scala
47 lines (37 loc) · 1.32 KB
/
package.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
package com.experiments.calvin
import com.experiments.calvin.domain._
import scalaz.concurrent.Task
import scalaz.~>
package object interpreters {
trait LogInterpreter {
def run[A](logInstruction: LogInstruction[A]): Task[A]
}
class PrintlnLogInterpreter extends LogInterpreter {
def run[A](logInstruction: LogInstruction[A]): Task[A] = logInstruction match {
case Log(info) => Task(println(info))
}
}
trait DatabaseInterpreter {
def run[A](dbInstruction: DatabaseInstruction[A]): Task[A]
}
class InMemoryDbInterpreter extends DatabaseInterpreter {
var map = Map.empty[String, Person]
override def run[A](dbInstruction: DatabaseInstruction[A]): Task[A] = dbInstruction match {
case GetById(id) =>
Task(map.get(id))
case Save(person) =>
map = map + (person.username -> person)
Task(())
}
}
class AppInstructionInterpreter(logInterpreter: LogInterpreter, dbInterpreter: DatabaseInterpreter) {
val interpreter: AppInstruction ~> Task = new (AppInstruction ~> Task) {
override def apply[A](fa: AppInstruction[A]): Task[A] = fa match {
case logInstruction: LogInstruction[A] =>
logInterpreter.run(logInstruction)
case dbInstruction: DatabaseInstruction[A] =>
dbInterpreter.run(dbInstruction)
}
}
}
}