-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.scala
193 lines (156 loc) · 6.11 KB
/
Demo.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import cats.syntax.option._
import org.scalacheck.Gen
import org.scalacheck.Gen.Parameters.default
import org.scalacheck.rng.Seed
object Demo extends App {
sealed trait Contractor extends Product
object Contractor {
case object Alice extends Contractor
case object Bob extends Contractor
case object Charlie extends Contractor
val all = List(Alice, Bob, Charlie)
//assumptions: static
implicit val likelyPerformance: Map[Contractor, Gen[Rep]] = Map(
Alice -> Gen.chooseNum(-10, 20).map(Rep.apply),
Bob -> Gen.chooseNum(-15, 15).map(Rep.apply),
Charlie -> Gen.chooseNum(-20, 10).map(Rep.apply),
)
}
case class DailyRate(value: Double)
case class Rep(value: Int)
case class Balance(value: Double)
trait Cooperation {
def hireContractor(dailyRate: DailyRate): Option[Contractor]
def retireContractor(contractor: Contractor): Unit
def incrementRating(contractor: Contractor): Unit
def decrementRating(contractor: Contractor): Unit
def payForService(contractor: Contractor, dailyRate: DailyRate): Unit
def submitPaperWork(): Unit
}
object Cooperation {
implicit class CooperationOps(coop: Cooperation) {
private def submitReview(rep: Rep, contractor: Contractor) =
rep.value match {
case i if i > 0 =>
for (_ <- Range(1, i + 1)) coop.incrementRating(contractor)
case i =>
for (_ <- Range(1, -i + 1)) coop.decrementRating(contractor)
}
def recruitForProject(
budget: DailyRate
)(implicit likelyPerformance: Map[Contractor, Gen[Rep]]): Option[Unit] =
for {
contractor <- coop.hireContractor(budget)
rep = likelyPerformance(contractor).pureApply(default, Seed.random)
_ = submitReview(rep, contractor)
_ = coop.payForService(contractor, budget)
_ = coop.retireContractor(contractor)
} yield ()
}
trait AdminWork extends Cooperation {
protected val availableForHire: ListBuffer[Contractor] =
ListBuffer.from(Contractor.all)
import Demo.Contractor.{Alice, Bob, Charlie}
protected val reps: mutable.Map[Contractor, Rep] =
mutable.Map(Alice -> Rep(30), Bob -> Rep(30), Charlie -> Rep(30))
protected val balances: mutable.Map[Contractor, Balance] = mutable.Map(
Alice -> Balance(0),
Bob -> Balance(0),
Charlie -> Balance(0)
)
override def incrementRating(contractor: Contractor): Unit =
reps.updateWith(contractor)(_.map(r => Rep(r.value + 1)))
override def decrementRating(contractor: Contractor): Unit =
reps.updateWith(contractor)(_.map(r => Rep(r.value - 1)))
//poc. should reps be normalised?
private def rep(dailyRate: DailyRate): Option[Rep] =
dailyRate.value match {
case i if i < 0 => none
case i if i < 100 => Rep(10).some
case i if i < 200 => Rep(20).some
case _ => Rep(30).some
}
private def chooseAcceptableRep(dailyRate: DailyRate,
reps: List[Rep]): Rep =
reps
.find(r => rep(dailyRate).exists(_.value <= r.value))
.getOrElse(reps.maxBy(_.value))
override def hireContractor(dailyRate: DailyRate): Option[Contractor] = {
val availableReps = availableForHire.map(reps.apply)
val acceptableRep: Rep =
chooseAcceptableRep(dailyRate, availableReps.toList)
val potentialHire: Option[Contractor] =
availableForHire.find(c => acceptableRep == reps(c))
potentialHire.map { c =>
availableForHire -= c; c
}
}
override def retireContractor(contractor: Contractor): Unit =
availableForHire += contractor
override def submitPaperWork(): Unit = {
balances.foreach(println)
reps.foreach(println)
}
}
def minimal: Cooperation = new Cooperation with AdminWork {
override def payForService(contractor: Contractor,
dailyRate: DailyRate): Unit =
balances.updateWith(contractor)(
_.map(b => Balance(b.value + dailyRate.value))
)
}
def ubiPercentage: Cooperation = new Cooperation with AdminWork {
override def payForService(contractor: Contractor,
dailyRate: DailyRate): Unit = {
val tax = Balance(dailyRate.value / 5)
val remaining = Balance(dailyRate.value - tax.value)
Contractor.all.foreach(
c =>
balances.updateWith(c)(
_.map(b => Balance(b.value + tax.value / balances.size))
)
)
balances.updateWith(contractor)(
_.map(b => Balance(b.value + remaining.value))
)
}
}
def ubiFixed: Cooperation = new Cooperation with AdminWork {
override def payForService(contractor: Contractor,
dailyRate: DailyRate): Unit = {
val tax = if (dailyRate.value > 100) Balance(100) else Balance(0)
val remaining = Balance(dailyRate.value - tax.value)
Contractor.all.foreach(
c =>
balances.updateWith(c)(
_.map(b => Balance(b.value + tax.value / balances.size))
)
)
balances.updateWith(contractor)(
_.map(b => Balance(b.value + remaining.value))
)
}
}
def communism: Cooperation = new Cooperation with AdminWork {
override def payForService(contractor: Contractor,
dailyRate: DailyRate): Unit =
Contractor.all.foreach(
c =>
balances.updateWith(c)(
_.map(b => Balance(b.value + dailyRate.value / balances.size))
)
)
}
}
import Contractor._
val coop = Cooperation.ubiPercentage
coop.recruitForProject(DailyRate(400))
coop.recruitForProject(DailyRate(500))
coop.recruitForProject(DailyRate(300))
coop.recruitForProject(DailyRate(400))
coop.recruitForProject(DailyRate(500))
coop.recruitForProject(DailyRate(300))
coop.submitPaperWork()
}