author | Christian Urban <christian.urban@kcl.ac.uk> |
Wed, 02 Dec 2020 01:15:14 +0000 | |
changeset 382 | 1bd800376e0c |
parent 381 | 116fa3c8584f |
child 383 | c02929f2647c |
permissions | -rw-r--r-- |
222 | 1 |
// Scala Lecture 4 |
2 |
//================= |
|
3 |
||
4 |
||
325 | 5 |
// expressions (essentially trees) |
6 |
||
7 |
abstract class Exp |
|
8 |
case class N(n: Int) extends Exp // for numbers |
|
9 |
case class Plus(e1: Exp, e2: Exp) extends Exp |
|
10 |
case class Times(e1: Exp, e2: Exp) extends Exp |
|
11 |
||
12 |
def string(e: Exp) : String = e match { |
|
13 |
case N(n) => s"$n" |
|
14 |
case Plus(e1, e2) => s"(${string(e1)} + ${string(e2)})" |
|
15 |
case Times(e1, e2) => s"(${string(e1)} * ${string(e2)})" |
|
16 |
} |
|
17 |
||
18 |
val e = Plus(N(9), Times(N(3), N(4))) |
|
19 |
println(string(e)) |
|
20 |
||
21 |
def eval(e: Exp) : Int = e match { |
|
22 |
case N(n) => n |
|
23 |
case Plus(e1, e2) => eval(e1) + eval(e2) |
|
24 |
case Times(e1, e2) => eval(e1) * eval(e2) |
|
25 |
} |
|
26 |
||
27 |
println(eval(e)) |
|
28 |
||
29 |
// simplification rules: |
|
30 |
// e + 0, 0 + e => e |
|
31 |
// e * 0, 0 * e => 0 |
|
32 |
// e * 1, 1 * e => e |
|
326 | 33 |
// |
34 |
// (....0 ....) |
|
325 | 35 |
|
36 |
def simp(e: Exp) : Exp = e match { |
|
37 |
case N(n) => N(n) |
|
38 |
case Plus(e1, e2) => (simp(e1), simp(e2)) match { |
|
39 |
case (N(0), e2s) => e2s |
|
40 |
case (e1s, N(0)) => e1s |
|
41 |
case (e1s, e2s) => Plus(e1s, e2s) |
|
42 |
} |
|
43 |
case Times(e1, e2) => (simp(e1), simp(e2)) match { |
|
44 |
case (N(0), _) => N(0) |
|
45 |
case (_, N(0)) => N(0) |
|
46 |
case (N(1), e2s) => e2s |
|
47 |
case (e1s, N(1)) => e1s |
|
48 |
case (e1s, e2s) => Times(e1s, e2s) |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
||
53 |
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9))) |
|
54 |
println(string(e2)) |
|
55 |
println(string(simp(e2))) |
|
56 |
||
57 |
||
58 |
// Tokens and Reverse Polish Notation |
|
59 |
abstract class Token |
|
60 |
case class T(n: Int) extends Token |
|
61 |
case object PL extends Token |
|
62 |
case object TI extends Token |
|
63 |
||
64 |
// transfroming an Exp into a list of tokens |
|
65 |
def rp(e: Exp) : List[Token] = e match { |
|
66 |
case N(n) => List(T(n)) |
|
67 |
case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) |
|
68 |
case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) |
|
69 |
} |
|
70 |
println(string(e2)) |
|
71 |
println(rp(e2)) |
|
72 |
||
326 | 73 |
def comp(ls: List[Token], st: List[Int] = Nil) : Int = (ls, st) match { |
325 | 74 |
case (Nil, st) => st.head |
75 |
case (T(n)::rest, st) => comp(rest, n::st) |
|
76 |
case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st) |
|
77 |
case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st) |
|
78 |
} |
|
79 |
||
326 | 80 |
comp(rp(e)) |
325 | 81 |
|
82 |
def proc(s: String) : Token = s match { |
|
83 |
case "+" => PL |
|
84 |
case "*" => TI |
|
85 |
case _ => T(s.toInt) |
|
86 |
} |
|
87 |
||
88 |
comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil) |
|
89 |
||
90 |
||
380 | 91 |
// Polymorphic Types |
92 |
//=================== |
|
93 |
||
94 |
// You do not want to write functions like contains, first, |
|
95 |
// length and so on for every type of lists. |
|
96 |
||
97 |
def length_int_list(lst: List[Int]): Int = lst match { |
|
98 |
case Nil => 0 |
|
99 |
case x::xs => 1 + length_int_list(xs) |
|
100 |
} |
|
101 |
||
102 |
length_int_list(List(1, 2, 3, 4)) |
|
103 |
||
104 |
def length_string_list(lst: List[String]): Int = lst match { |
|
105 |
case Nil => 0 |
|
106 |
case _::xs => 1 + length_string_list(xs) |
|
107 |
} |
|
108 |
||
109 |
length_string_list(List("1", "2", "3", "4")) |
|
110 |
||
111 |
||
112 |
// you can make the function parametric in type(s) |
|
113 |
||
114 |
def length[A](lst: List[A]): Int = lst match { |
|
115 |
case Nil => 0 |
|
116 |
case x::xs => 1 + length(xs) |
|
117 |
} |
|
118 |
length(List("1", "2", "3", "4")) |
|
119 |
length(List(1, 2, 3, 4)) |
|
120 |
||
121 |
length[Int](List(1, 2, 3, 4)) |
|
122 |
||
123 |
||
124 |
def map[A, B](lst: List[A], f: A => B): List[B] = lst match { |
|
125 |
case Nil => Nil |
|
126 |
case x::xs => f(x)::map(xs, f) |
|
127 |
} |
|
128 |
||
129 |
map(List(1, 2, 3, 4), (x: Int) => x.toString) |
|
130 |
||
131 |
||
132 |
// from knight1.scala |
|
133 |
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ??? |
|
134 |
||
135 |
// should be |
|
136 |
def first[A, B](xs: List[A], f: A => Option[B]) : Option[B] = ??? |
|
137 |
||
138 |
// Type inference is local in Scala |
|
139 |
||
140 |
def id[T](x: T) : T = x |
|
141 |
||
142 |
val x = id(322) // Int |
|
143 |
val y = id("hey") // String |
|
144 |
val z = id(Set(1,2,3,4)) // Set[Int] |
|
145 |
||
146 |
||
147 |
// The type variable concept in Scala can get really complicated. |
|
148 |
// |
|
149 |
// - variance (OO) |
|
150 |
// - bounds (subtyping) |
|
151 |
// - quantification |
|
152 |
||
153 |
// Java has issues with this too: Java allows |
|
154 |
// to write the following incorrect code, and |
|
155 |
// only recovers by raising an exception |
|
156 |
// at runtime. |
|
157 |
||
158 |
// Object[] arr = new Integer[10]; |
|
159 |
// arr[0] = "Hello World"; |
|
160 |
||
161 |
||
162 |
// Scala gives you a compile-time error, which |
|
163 |
// is much better. |
|
164 |
||
165 |
var arr = Array[Int]() |
|
166 |
arr(0) = "Hello World" |
|
167 |
||
168 |
||
169 |
||
170 |
||
171 |
// Function definitions again |
|
172 |
//============================ |
|
173 |
||
174 |
// variable arguments |
|
175 |
||
176 |
def printAll(strings: String*) = { |
|
177 |
strings.foreach(println) |
|
178 |
} |
|
179 |
||
180 |
printAll() |
|
181 |
printAll("foo") |
|
182 |
printAll("foo", "bar") |
|
183 |
printAll("foo", "bar", "baz") |
|
184 |
||
185 |
// pass a list to the varargs field |
|
186 |
val fruits = List("apple", "banana", "cherry") |
|
187 |
||
188 |
printAll(fruits: _*) |
|
189 |
||
190 |
||
191 |
// you can also implement your own string interpolations |
|
192 |
import scala.language.implicitConversions |
|
193 |
import scala.language.reflectiveCalls |
|
194 |
||
195 |
implicit def sring_inters(sc: StringContext) = new { |
|
196 |
def i(args: Any*): String = s"${sc.s(args:_*)}\n" |
|
197 |
} |
|
198 |
||
199 |
i"add ${3+2} ${3 * 3}" |
|
200 |
||
201 |
||
202 |
// default arguments |
|
203 |
||
204 |
def length[A](xs: List[A]) : Int = xs match { |
|
205 |
case Nil => 0 |
|
206 |
case _ :: tail => 1 + length(tail) |
|
207 |
} |
|
208 |
||
209 |
def lengthT[A](xs: List[A], acc : Int = 0) : Int = xs match { |
|
210 |
case Nil => acc |
|
211 |
case _ :: tail => lengthT(tail, 1 + acc) |
|
212 |
} |
|
213 |
||
214 |
lengthT(List.fill(100000)(1)) |
|
215 |
||
216 |
||
217 |
def fact(n: BigInt, acc: BigInt = 1): BigInt = |
|
218 |
if (n == 0) acc else fact(n - 1, n * acc) |
|
219 |
||
220 |
fact(10) |
|
221 |
||
222 |
||
223 |
||
224 |
// currying (Haskell Curry) |
|
225 |
||
226 |
def add(x: Int, y: Int) = x + y |
|
227 |
||
228 |
List(1,2,3,4,5).map(x => add(3, x)) |
|
229 |
||
230 |
def add2(x: Int)(y: Int) = x + y |
|
231 |
||
232 |
List(1,2,3,4,5).map(add2(3)) |
|
233 |
||
234 |
val a3 : Int => Int = add2(3) |
|
235 |
||
236 |
// currying helps sometimes with type inference |
|
237 |
||
238 |
def find[A](xs: List[A])(pred: A => Boolean): Option[A] = { |
|
239 |
xs match { |
|
240 |
case Nil => None |
|
241 |
case hd :: tl => |
|
242 |
if (pred(hd)) Some(hd) else find(tl)(pred) |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
find(List(1, 2, 3))(x => x % 2 == 0) |
|
247 |
||
248 |
// Source.fromURL(url)(encoding) |
|
249 |
// Source.fromFile(name)(encoding) |
|
325 | 250 |
|
251 |
||
382 | 252 |
|
253 |
||
254 |
||
255 |
||
325 | 256 |
// Sudoku |
257 |
//======== |
|
258 |
||
259 |
// THE POINT OF THIS CODE IS NOT TO BE SUPER |
|
260 |
// EFFICIENT AND FAST, just explaining exhaustive |
|
261 |
// depth-first search |
|
262 |
||
263 |
||
264 |
val game0 = """.14.6.3.. |
|
265 |
|62...4..9 |
|
266 |
|.8..5.6.. |
|
267 |
|.6.2....3 |
|
268 |
|.7..1..5. |
|
269 |
|5....9.6. |
|
270 |
|..6.2..3. |
|
271 |
|1..5...92 |
|
272 |
|..7.9.41.""".stripMargin.replaceAll("\\n", "") |
|
273 |
||
326 | 274 |
candidates(game0, (0, 0)) |
275 |
||
325 | 276 |
type Pos = (Int, Int) |
277 |
val EmptyValue = '.' |
|
278 |
val MaxValue = 9 |
|
279 |
||
280 |
val allValues = "123456789".toList |
|
281 |
val indexes = (0 to 8).toList |
|
282 |
||
283 |
||
284 |
def empty(game: String) = game.indexOf(EmptyValue) |
|
285 |
def isDone(game: String) = empty(game) == -1 |
|
286 |
def emptyPosition(game: String) = |
|
287 |
(empty(game) % MaxValue, empty(game) / MaxValue) |
|
288 |
||
289 |
||
290 |
def get_row(game: String, y: Int) = |
|
291 |
indexes.map(col => game(y * MaxValue + col)) |
|
292 |
def get_col(game: String, x: Int) = |
|
293 |
indexes.map(row => game(x + row * MaxValue)) |
|
294 |
||
326 | 295 |
get_row(game0, 0) |
296 |
||
325 | 297 |
def get_box(game: String, pos: Pos): List[Char] = { |
298 |
def base(p: Int): Int = (p / 3) * 3 |
|
299 |
val x0 = base(pos._1) |
|
300 |
val y0 = base(pos._2) |
|
301 |
val ys = (y0 until y0 + 3).toList |
|
302 |
(x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue))) |
|
303 |
} |
|
304 |
||
305 |
//get_row(game0, 0) |
|
306 |
//get_row(game0, 1) |
|
307 |
//get_col(game0, 0) |
|
308 |
//get_box(game0, (3, 1)) |
|
309 |
||
310 |
||
311 |
// this is not mutable!! |
|
312 |
def update(game: String, pos: Int, value: Char): String = |
|
313 |
game.updated(pos, value) |
|
314 |
||
315 |
def toAvoid(game: String, pos: Pos): List[Char] = |
|
316 |
(get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos)) |
|
317 |
||
318 |
def candidates(game: String, pos: Pos): List[Char] = |
|
319 |
allValues.diff(toAvoid(game, pos)) |
|
320 |
||
321 |
//candidates(game0, (0,0)) |
|
322 |
||
323 |
def pretty(game: String): String = |
|
324 |
"\n" + (game.sliding(MaxValue, MaxValue).mkString("\n")) |
|
325 |
||
326 |
def search(game: String): List[String] = { |
|
327 |
if (isDone(game)) List(game) |
|
328 |
else { |
|
329 |
val cs = candidates(game, emptyPosition(game)) |
|
330 |
cs.map(c => search(update(game, empty(game), c))).toList.flatten |
|
331 |
} |
|
332 |
} |
|
333 |
||
326 | 334 |
List(List("sol1"), List("sol2", "sol3")).flatten |
335 |
||
325 | 336 |
search(game0).map(pretty) |
337 |
||
338 |
val game1 = """23.915... |
|
339 |
|...2..54. |
|
340 |
|6.7...... |
|
341 |
|..1.....9 |
|
342 |
|89.5.3.17 |
|
343 |
|5.....6.. |
|
344 |
|......9.5 |
|
345 |
|.16..7... |
|
346 |
|...329..1""".stripMargin.replaceAll("\\n", "") |
|
347 |
||
348 |
search(game1).map(pretty) |
|
349 |
||
350 |
// a game that is in the hard category |
|
351 |
val game2 = """8........ |
|
352 |
|..36..... |
|
353 |
|.7..9.2.. |
|
354 |
|.5...7... |
|
355 |
|....457.. |
|
356 |
|...1...3. |
|
357 |
|..1....68 |
|
358 |
|..85...1. |
|
359 |
|.9....4..""".stripMargin.replaceAll("\\n", "") |
|
360 |
||
361 |
search(game2).map(pretty) |
|
362 |
||
363 |
// game with multiple solutions |
|
364 |
val game3 = """.8...9743 |
|
365 |
|.5...8.1. |
|
366 |
|.1....... |
|
367 |
|8....5... |
|
368 |
|...8.4... |
|
369 |
|...3....6 |
|
370 |
|.......7. |
|
371 |
|.3.5...8. |
|
372 |
|9724...5.""".stripMargin.replaceAll("\\n", "") |
|
373 |
||
374 |
search(game3).map(pretty).foreach(println) |
|
375 |
||
376 |
// for measuring time |
|
377 |
def time_needed[T](i: Int, code: => T) = { |
|
378 |
val start = System.nanoTime() |
|
379 |
for (j <- 1 to i) code |
|
380 |
val end = System.nanoTime() |
|
381 |
s"${(end - start) / 1.0e9} secs" |
|
382 |
} |
|
383 |
||
384 |
time_needed(1, search(game2)) |
|
385 |
||
386 |
||
387 |
||
388 |
// Tail recursion |
|
389 |
//================ |
|
390 |
||
326 | 391 |
@tailrec |
392 |
def fact(n: BigInt): BigInt = |
|
325 | 393 |
if (n == 0) 1 else n * fact(n - 1) |
394 |
||
395 |
||
326 | 396 |
fact(10) |
397 |
fact(1000) |
|
398 |
fact(100000) |
|
325 | 399 |
|
400 |
def factB(n: BigInt): BigInt = |
|
401 |
if (n == 0) 1 else n * factB(n - 1) |
|
402 |
||
326 | 403 |
def factT(n: BigInt, acc: BigInt): BigInt = |
404 |
if (n == 0) acc else factT(n - 1, n * acc) |
|
405 |
||
406 |
||
325 | 407 |
factB(1000) |
408 |
||
409 |
||
326 | 410 |
|
325 | 411 |
|
412 |
factT(10, 1) |
|
326 | 413 |
println(factT(500000, 1)) |
414 |
||
415 |
||
416 |
||
417 |
||
325 | 418 |
|
419 |
// there is a flag for ensuring a function is tail recursive |
|
420 |
import scala.annotation.tailrec |
|
421 |
||
422 |
@tailrec |
|
423 |
def factT(n: BigInt, acc: BigInt): BigInt = |
|
424 |
if (n == 0) acc else factT(n - 1, n * acc) |
|
425 |
||
426 |
factT(100000, 1) |
|
427 |
||
428 |
// for tail-recursive functions the Scala compiler |
|
429 |
// generates loop-like code, which does not need |
|
430 |
// to allocate stack-space in each recursive |
|
431 |
// call; Scala can do this only for tail-recursive |
|
432 |
// functions |
|
433 |
||
434 |
// tail recursive version that searches |
|
435 |
// for all Sudoku solutions |
|
436 |
||
326 | 437 |
@tailrec |
325 | 438 |
def searchT(games: List[String], sols: List[String]): List[String] = games match { |
439 |
case Nil => sols |
|
440 |
case game::rest => { |
|
441 |
if (isDone(game)) searchT(rest, game::sols) |
|
442 |
else { |
|
443 |
val cs = candidates(game, emptyPosition(game)) |
|
444 |
searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols) |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
searchT(List(game3), List()).map(pretty) |
|
450 |
||
451 |
||
452 |
// tail recursive version that searches |
|
453 |
// for a single solution |
|
454 |
||
455 |
def search1T(games: List[String]): Option[String] = games match { |
|
456 |
case Nil => None |
|
457 |
case game::rest => { |
|
458 |
if (isDone(game)) Some(game) |
|
459 |
else { |
|
460 |
val cs = candidates(game, emptyPosition(game)) |
|
461 |
search1T(cs.map(c => update(game, empty(game), c)) ::: rest) |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
465 |
||
466 |
search1T(List(game3)).map(pretty) |
|
467 |
time_needed(1, search1T(List(game3))) |
|
468 |
time_needed(1, search1T(List(game2))) |
|
469 |
||
470 |
// game with multiple solutions |
|
471 |
val game3 = """.8...9743 |
|
472 |
|.5...8.1. |
|
473 |
|.1....... |
|
474 |
|8....5... |
|
475 |
|...8.4... |
|
476 |
|...3....6 |
|
477 |
|.......7. |
|
478 |
|.3.5...8. |
|
479 |
|9724...5.""".stripMargin.replaceAll("\\n", "") |
|
480 |
||
481 |
searchT(List(game3), Nil).map(pretty) |
|
482 |
search1T(List(game3)).map(pretty) |
|
483 |
||
484 |
// Moral: Whenever a recursive function is resource-critical |
|
326 | 485 |
// (i.e. works with a large recursion depth), then you need to |
325 | 486 |
// write it in tail-recursive fashion. |
487 |
// |
|
488 |
// Unfortuantely, Scala because of current limitations in |
|
489 |
// the JVM is not as clever as other functional languages. It can |
|
490 |
// only optimise "self-tail calls". This excludes the cases of |
|
491 |
// multiple functions making tail calls to each other. Well, |
|
492 |
// nothing is perfect. |
|
493 |
||
494 |
||
495 |
||
496 |
||
222 | 497 |
|
498 |
||
499 |
||
325 | 500 |
// Cool Stuff in Scala |
501 |
//===================== |
|
502 |
||
503 |
||
504 |
// Implicits or How to Pimp your Library |
|
505 |
//====================================== |
|
506 |
// |
|
507 |
// For example adding your own methods to Strings: |
|
508 |
// Imagine you want to increment strings, like |
|
509 |
// |
|
510 |
// "HAL".increment |
|
511 |
// |
|
512 |
// you can avoid ugly fudges, like a MyString, by |
|
513 |
// using implicit conversions. |
|
514 |
||
515 |
||
516 |
implicit class MyString(s: String) { |
|
517 |
def increment = s.map(c => (c + 1).toChar) |
|
518 |
} |
|
519 |
||
520 |
"HAL".increment |
|
521 |
||
522 |
||
523 |
// Abstract idea: |
|
524 |
// In that version implicit conversions were used to solve the |
|
525 |
// late extension problem; namely, given a class C and a class T, |
|
526 |
// how to have C extend T without touching or recompiling C. |
|
527 |
// Conversions add a wrapper when a member of T is requested |
|
528 |
// from an instance of C. |
|
529 |
||
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
530 |
|
325 | 531 |
|
532 |
import scala.concurrent.duration.{TimeUnit,SECONDS,MINUTES} |
|
533 |
||
534 |
case class Duration(time: Long, unit: TimeUnit) { |
|
535 |
def +(o: Duration) = |
|
536 |
Duration(time + unit.convert(o.time, o.unit), unit) |
|
537 |
} |
|
538 |
||
539 |
implicit class Int2Duration(that: Int) { |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
540 |
def seconds = Duration(that, SECONDS) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
541 |
def minutes = Duration(that, MINUTES) |
325 | 542 |
} |
543 |
||
544 |
5.seconds + 2.minutes //Duration(125L, SECONDS ) |
|
545 |
2.minutes + 60.seconds |
|
546 |
||
547 |
||
548 |
||
549 |
||
550 |
// Regular expressions - the power of DSLs in Scala |
|
551 |
//================================================== |
|
552 |
||
553 |
abstract class Rexp |
|
554 |
case object ZERO extends Rexp // nothing |
|
555 |
case object ONE extends Rexp // the empty string |
|
556 |
case class CHAR(c: Char) extends Rexp // a character c |
|
557 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative r1 + r2 |
|
558 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence r1 . r2 |
|
559 |
case class STAR(r: Rexp) extends Rexp // star r* |
|
560 |
||
561 |
||
562 |
||
563 |
// writing (ab)* in the format above is |
|
564 |
// tedious |
|
565 |
val r0 = STAR(SEQ(CHAR('a'), CHAR('b'))) |
|
566 |
||
567 |
||
568 |
// some convenience for typing in regular expressions |
|
569 |
import scala.language.implicitConversions |
|
570 |
import scala.language.reflectiveCalls |
|
571 |
||
572 |
def charlist2rexp(s: List[Char]): Rexp = s match { |
|
573 |
case Nil => ONE |
|
574 |
case c::Nil => CHAR(c) |
|
575 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
576 |
} |
|
326 | 577 |
|
325 | 578 |
implicit def string2rexp(s: String): Rexp = |
579 |
charlist2rexp(s.toList) |
|
580 |
||
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
581 |
val r1 = STAR("hello") |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
582 |
val r2 = STAR("hello") | STAR("world") |
325 | 583 |
|
584 |
||
585 |
implicit def RexpOps (r: Rexp) = new { |
|
586 |
def | (s: Rexp) = ALT(r, s) |
|
587 |
def % = STAR(r) |
|
588 |
def ~ (s: Rexp) = SEQ(r, s) |
|
589 |
} |
|
590 |
||
591 |
implicit def stringOps (s: String) = new { |
|
592 |
def | (r: Rexp) = ALT(s, r) |
|
593 |
def | (r: String) = ALT(s, r) |
|
594 |
def % = STAR(s) |
|
595 |
def ~ (r: Rexp) = SEQ(s, r) |
|
596 |
def ~ (r: String) = SEQ(s, r) |
|
597 |
} |
|
598 |
||
599 |
//example regular expressions |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
600 |
|
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
601 |
|
326 | 602 |
val digit = ("0" | "1" | "2" | "3" | "4" | |
603 |
"5" | "6" | "7" | "8" | "9") |
|
325 | 604 |
val sign = "+" | "-" | "" |
605 |
val number = sign ~ digit ~ digit.% |
|
606 |
||
607 |
||
608 |
||
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
609 |
|
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
610 |
// In mandelbrot.scala I used complex (imaginary) numbers |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
611 |
// and implemented the usual arithmetic operations for complex |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
612 |
// numbers. |
325 | 613 |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
614 |
case class Complex(re: Double, im: Double) { |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
615 |
// represents the complex number re + im * i |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
616 |
def +(that: Complex) = Complex(this.re + that.re, this.im + that.im) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
617 |
def -(that: Complex) = Complex(this.re - that.re, this.im - that.im) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
618 |
def *(that: Complex) = Complex(this.re * that.re - this.im * that.im, |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
619 |
this.re * that.im + that.re * this.im) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
620 |
def *(that: Double) = Complex(this.re * that, this.im * that) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
621 |
def abs = Math.sqrt(this.re * this.re + this.im * this.im) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
622 |
} |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
623 |
|
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
624 |
val test = Complex(1, 2) + Complex (3, 4) |
222 | 625 |
|
325 | 626 |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
627 |
// ...to allow the notation n + m * i |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
628 |
import scala.language.implicitConversions |
325 | 629 |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
630 |
val i = Complex(0, 1) |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
631 |
implicit def double2complex(re: Double) = Complex(re, 0) |
222 | 632 |
|
381
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
633 |
val inum1 = -2.0 + -1.5 * i |
116fa3c8584f
updated duration class
Christian Urban <christian.urban@kcl.ac.uk>
parents:
380
diff
changeset
|
634 |
val inum2 = 1.0 + 1.5 * i |