-
Notifications
You must be signed in to change notification settings - Fork 34
/
Result.scala
237 lines (212 loc) · 5.92 KB
/
Result.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package challenge1
import core._, Syntax._
/*
* Handling errors without exceptions....
* ======================================
*/
/*
* A well-typed set of errors that can occur.
*/
sealed trait Error
case class Explosion(exception: Throwable) extends Error
case object NotFound extends Error
case object InvalidRequest extends Error
case object InvalidMethod extends Error
case object Unauthorized extends Error
object Error {
implicit def ErrorEqual =
Equal.derived[Error]
}
/*
* A result type that represents one of our errors or a success.
*/
case class Fail[A](error: Error) extends Result[A]
case class Ok[A](value: A) extends Result[A]
sealed trait Result[A] {
/*
* Exercise 1.1:
*
* Implement the catamorphism for Result[A].
*
* Hint: Try using pattern matching.
*
* scala> Ok(1).fold(_ => 0, x => x)
* = 1
*
* scala> Fail(NotFound).fold(_ => 0, x => x)
* = 0
*/
def fold[X](
fail: Error => X,
ok: A => X
): X = this match {
case Fail(error) => fail(error)
case Ok(value) => ok(value)
}
/*
* Exercise 1.2:
*
* Implement map for Result[A].
*
* The following laws must hold:
* 1) r.map(z => z) == r
* 2) r.map(z => f(g(z))) == r.map(g).map(f)
*
* scala> Ok(1).map(x => x + 10)
* = Ok(11)
*
* scala> Fail(NotFound).map(x => x + 10)
* = Fail(NotFound)
*
* Advanced: Try using flatMap.
*/
def map[B](f: A => B): Result[B] =
flatMap(f andThen Result.ok)
/*
* Exercise 1.3:
*
* Implement flatMap (a.k.a. bind, a.k.a. >>=).
*
* The following law must hold:
* r.flatMap(f).flatMap(g) == r.flatMap(z => f(z).flatMap(g))
*
* scala> Ok(1).flatMap(x => Ok(x + 10))
* = Ok(11)
*
* scala> Ok(1).flatMap(x => Fail[Int](Unauthorized))
* = Fail(Unauthorized)
*
* scala> Fail(NotFound).map(x => Ok(x + 10))
* = Fail(NotFound)
*
* scala> Fail(NotFound).map(x => Fail(Unauthorized))
* = Fail(NotFound)
*
* Advanced: Try using fold.
*/
def flatMap[B](f: A => Result[B]): Result[B] =
fold(Result.fail, f)
/*
* Exercise 1.4:
*
* Extract the value if it is success case otherwise use default value.
*
*
* scala> Ok(1).getOrElse(10)
* = 1
*
* scala> Fail(NotFound).getOrElse(10)
* = 10
*/
def getOrElse(otherwise: => A): A =
fold(_ => otherwise, identity)
/*
* Exercise 1.4:
*
* Implement choice, take this result if successful otherwise take
* the alternative.
*
* scala> Ok(1) ||| Ok(10)
* = Ok(1)
*
* scala> Ok(1) ||| Fail[Int](Unauthorized)
* = Ok(1)
*
* scala> Fail[Int](NotFound) ||| Ok(10)
* = Ok(10)
*
* scala> Fail[Int](NotFound) ||| Fail[Int](Unauthorized)
* = Fail(Unauthorized)
*/
def |||(alternative: => Result[A]): Result[A] =
fold(_ => alternative, _ => this)
}
object Result {
def fail[A](error: Error): Result[A] =
Fail(error)
def ok[A](value: A): Result[A] =
Ok(value)
implicit def ResultMonad: Monad[Result] = new Monad[Result] {
def point[A](a: => A) = ok(a)
def bind[A, B](a: Result[A])(f: A => Result[B]) = a flatMap f
}
implicit def ResultEqual[A: Equal] =
Equal.from[Result[A]]((a, b) => a.fold(
e => b.fold(_ === e, _ => false),
a => b.fold(_ => false, _ === a)
))
}
/*
* *Challenge* Exercise 1.5: The worlds most trivial HTTP calculator.
*
* We are implementing a way to compute a number via a HTTP like
* API.
* - We will send a method which is one of GET|POST|PUT|DELETE.
* - We want to send an integer as a request body.
* - We will send a path of what calculation to use.
*
* Complete the implementation, some of the methods are provided
* with type signatures to get started.
*/
object Example {
/** Simplified method data type. */
sealed trait Method
case object Get extends Method
case object Post extends Method
case object Put extends Method
case object Delete extends Method
/* Parse the method if it is valid, otherwise fail with InvalidMethod. */
def method(method: String): Result[Method] = method match {
case "GET" => Result.ok(Get)
case "POST" => Result.ok(Post)
case "PUT" => Result.ok(Put)
case "DELETE" => Result.ok(Delete)
case _ => Result.fail(InvalidMethod)
}
/*
* Parse the method if it is valid, otherwise fail with InvalidRequest.
*
* Hint: Scala defines String#toInt, but warning it throws exceptions if it is not a valid Int :|
*/
def request(body: String): Result[Int] =
if (body.matches("""^\d+$""")) Result.ok(body.toInt) else Result.fail(InvalidRequest)
/*
* Route method and path to an implementation.
*
* A minimal implementation is:
* GET /single -> n * 1
* GET /double -> n * 2
* GET /triple -> n * 3
* PUT * -> Unauthorized
* POST * -> Unauthorized
* DELETE * -> Unauthorized
* * -> NotFound
*/
def route(method: Method, path: String): Result[Int => Int] = (method, path) match {
case (Get, "/single") => Result.ok((n: Int) => n)
case (Get, "/double") => Result.ok((n: Int) => n * 2)
case (Get, "/triple") => Result.ok((n: Int) => n * 3)
case (Get, _) => Result.fail(NotFound)
case (Delete, _) => Result.fail(Unauthorized)
case (Put, _) => Result.fail(Unauthorized)
case (Post, _) => Result.fail(Unauthorized)
}
/*
* Attempt to compute an `answer`, by:
* - determining method
* - selecting implementation
* - determing request value
* - using the implementation and request value to compute an answer.
*/
def service(path: String, methodx: String, body: String): Result[Int] = for {
m <- method(methodx)
f <- route(m, path)
n <- request(body)
} yield f(n)
/*
* Sometimes we always an `answer`, so default to 0 if
* our request failed in any way.
*/
def run(path: String, method: String, body: String): Int =
service(path, method, body).getOrElse(0)
}