51
|
1 |
// Scala Lecture 2
|
|
2 |
//=================
|
363
|
3 |
|
317
|
4 |
|
|
5 |
// String Interpolations
|
|
6 |
//=======================
|
|
7 |
|
318
|
8 |
def cube(n: Int) : Int = n * n * n
|
|
9 |
|
317
|
10 |
val n = 3
|
318
|
11 |
println("The cube of " + n + " is " + cube(n) + ".")
|
317
|
12 |
|
361
|
13 |
println(s"The cube of $n is ${cube(n)}.")
|
317
|
14 |
|
318
|
15 |
// or even
|
|
16 |
|
361
|
17 |
println(s"The cube of $n is ${n * n * n}.")
|
317
|
18 |
|
|
19 |
// helpful for debugging purposes
|
|
20 |
//
|
361
|
21 |
// "The most effective debugging tool is still careful
|
|
22 |
// thought, coupled with judiciously placed print
|
|
23 |
// statements."
|
|
24 |
// — Brian W. Kernighan, in Unix for Beginners (1979)
|
317
|
25 |
|
|
26 |
|
|
27 |
def gcd_db(a: Int, b: Int) : Int = {
|
361
|
28 |
println(s"Function called with $a and $b.")
|
317
|
29 |
if (b == 0) a else gcd_db(b, a % b)
|
|
30 |
}
|
|
31 |
|
|
32 |
gcd_db(48, 18)
|
204
|
33 |
|
|
34 |
|
361
|
35 |
|
|
36 |
// you can also implement your own string interpolations
|
|
37 |
|
|
38 |
import scala.language.implicitConversions
|
|
39 |
import scala.language.reflectiveCalls
|
|
40 |
|
|
41 |
implicit def sring_inters(sc: StringContext) = new {
|
|
42 |
def i(args: Any*): String = s"\t${sc.s(args:_*)}\n"
|
|
43 |
def l(args: Any*): String = s"${sc.s(args:_*)}:\n"
|
|
44 |
}
|
|
45 |
|
|
46 |
// this allows you to write things like
|
|
47 |
|
|
48 |
i"add ${3+2}"
|
|
49 |
l"some_fresh_name"
|
|
50 |
|
|
51 |
|
|
52 |
|
316
|
53 |
// The Option Type
|
|
54 |
//=================
|
|
55 |
|
361
|
56 |
// in Java, if something unusually happens, you return null
|
|
57 |
// or raise an exception
|
316
|
58 |
//
|
|
59 |
//in Scala you use Options instead
|
|
60 |
// - if the value is present, you use Some(value)
|
|
61 |
// - if no value is present, you use None
|
204
|
62 |
|
|
63 |
|
316
|
64 |
List(7,2,3,4,5,6).find(_ < 4)
|
|
65 |
List(5,6,7,8,9).find(_ < 4)
|
212
|
66 |
|
361
|
67 |
// Int: ..., 0, 1, 2,...
|
|
68 |
// Boolean: true false
|
|
69 |
//
|
|
70 |
// List[Int]: Nil, List(_)
|
|
71 |
//
|
|
72 |
// Option[Int]: None, Some(0), Some(1), ...
|
|
73 |
// Option[...]: None, Some(_)
|
|
74 |
|
|
75 |
def safe_div(x: Int, y: Int) : Option[Int] =
|
|
76 |
if (y == 0) None else Some(x / y)
|
|
77 |
|
|
78 |
List(1,2,3,4,5,6).indexOf(7)
|
|
79 |
|
|
80 |
def my_min(ls: List[Int]) : Option[Int] = ls.minOption
|
|
81 |
|
|
82 |
my_min(List(1,2,3,4))
|
|
83 |
|
310
|
84 |
|
316
|
85 |
// better error handling with Options (no exceptions)
|
|
86 |
//
|
|
87 |
// Try(something).getOrElse(what_to_do_in_case_of_an_exception)
|
204
|
88 |
//
|
316
|
89 |
|
|
90 |
import scala.util._
|
|
91 |
import io.Source
|
|
92 |
|
361
|
93 |
val my_url = "https://nms.kcl.ac.uk/christian.urban2/"
|
316
|
94 |
|
361
|
95 |
Source.fromURL(my_url)("ISO-8859-1").mkString
|
316
|
96 |
|
361
|
97 |
Try(Source.fromURL(my_url)("ISO-8859-1").mkString).getOrElse("")
|
316
|
98 |
|
361
|
99 |
Try(Some(Source.fromURL(my_url)("ISO-8859-1").mkString)).getOrElse(None)
|
204
|
100 |
|
|
101 |
|
316
|
102 |
// the same for files
|
361
|
103 |
Try(Some(Source.fromFile("test.txt")("ISO-8859-1").mkString)).getOrElse(None)
|
316
|
104 |
|
204
|
105 |
|
319
|
106 |
// how to implement a function for reading
|
|
107 |
// (lines) something from files...
|
|
108 |
//
|
316
|
109 |
def get_contents(name: String) : List[String] =
|
361
|
110 |
Source.fromFile(name)("ISO-8859-1").getLines.toList
|
204
|
111 |
|
319
|
112 |
get_contents("text.txt")
|
316
|
113 |
get_contents("test.txt")
|
204
|
114 |
|
316
|
115 |
// slightly better - return Nil
|
|
116 |
def get_contents(name: String) : List[String] =
|
361
|
117 |
Try(Source.fromFile(name)("ISO-8859-1").getLines.toList).getOrElse(List())
|
204
|
118 |
|
316
|
119 |
get_contents("text.txt")
|
204
|
120 |
|
316
|
121 |
// much better - you record in the type that things can go wrong
|
|
122 |
def get_contents(name: String) : Option[List[String]] =
|
361
|
123 |
Try(Some(Source.fromFile(name)("ISO-8859-1").getLines.toList)).getOrElse(None)
|
204
|
124 |
|
316
|
125 |
get_contents("text.txt")
|
|
126 |
get_contents("test.txt")
|
204
|
127 |
|
|
128 |
|
317
|
129 |
// operations on options
|
204
|
130 |
|
317
|
131 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
204
|
132 |
|
317
|
133 |
lst.flatten
|
204
|
134 |
|
317
|
135 |
Some(1).get
|
|
136 |
None.get
|
310
|
137 |
|
317
|
138 |
Some(1).isDefined
|
|
139 |
None.isDefined
|
310
|
140 |
|
361
|
141 |
for (x <- lst) yield x.getOrElse(0)
|
310
|
142 |
|
361
|
143 |
|
|
144 |
|
|
145 |
val ps = List((3, 0), (4, 2), (6, 2),
|
|
146 |
(2, 0), (1, 0), (1, 1))
|
317
|
147 |
|
|
148 |
// division where possible
|
|
149 |
|
|
150 |
for ((x, y) <- ps) yield {
|
|
151 |
if (y == 0) None else Some(x / y)
|
|
152 |
}
|
|
153 |
|
361
|
154 |
|
|
155 |
|
317
|
156 |
// getOrElse is for setting a default value
|
|
157 |
|
|
158 |
val lst = List(None, Some(1), Some(2), None, Some(3))
|
|
159 |
|
361
|
160 |
|
317
|
161 |
|
|
162 |
|
361
|
163 |
// a function that turns strings into numbers
|
|
164 |
// (similar to .toInt)
|
|
165 |
Integer.parseInt("1234")
|
318
|
166 |
|
|
167 |
|
|
168 |
def get_me_an_int(s: String) : Option[Int] =
|
|
169 |
Try(Some(Integer.parseInt(s))).getOrElse(None)
|
310
|
170 |
|
|
171 |
|
317
|
172 |
// This may not look any better than working with null in Java, but to
|
|
173 |
// see the value, you have to put yourself in the shoes of the
|
|
174 |
// consumer of the get_me_an_int function, and imagine you didn't
|
|
175 |
// write that function.
|
|
176 |
//
|
|
177 |
// In Java, if you didn't write this function, you'd have to depend on
|
|
178 |
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc,
|
318
|
179 |
// you might not know that get_me_an_int could return null, and your
|
317
|
180 |
// code could potentially throw a NullPointerException.
|
310
|
181 |
|
|
182 |
|
317
|
183 |
// even Scala is not immune to problems like this:
|
310
|
184 |
|
317
|
185 |
List(5,6,7,8,9).indexOf(7)
|
|
186 |
List(5,6,7,8,9).indexOf(10)
|
|
187 |
List(5,6,7,8,9)(-1)
|
310
|
188 |
|
|
189 |
|
320
|
190 |
Try({
|
|
191 |
val x = 3
|
|
192 |
val y = 0
|
|
193 |
Some(x / y)
|
|
194 |
}).getOrElse(None)
|
204
|
195 |
|
323
|
196 |
|
|
197 |
// minOption
|
|
198 |
// maxOption
|
|
199 |
// minByOption
|
|
200 |
// maxByOption
|
|
201 |
|
204
|
202 |
// Higher-Order Functions
|
|
203 |
//========================
|
|
204 |
|
|
205 |
// functions can take functions as arguments
|
319
|
206 |
// and produce functions as result
|
204
|
207 |
|
|
208 |
def even(x: Int) : Boolean = x % 2 == 0
|
|
209 |
def odd(x: Int) : Boolean = x % 2 == 1
|
|
210 |
|
|
211 |
val lst = (1 to 10).toList
|
|
212 |
|
|
213 |
lst.filter(even)
|
320
|
214 |
lst.count(odd)
|
212
|
215 |
lst.find(even)
|
320
|
216 |
lst.exists(even)
|
212
|
217 |
|
362
|
218 |
lst.find(_ < 4)
|
320
|
219 |
lst.filter(_ < 4)
|
362
|
220 |
|
|
221 |
def less4(x: Int) : Boolean = x < 4
|
|
222 |
lst.find(less4)
|
|
223 |
lst.find(_ < 4)
|
|
224 |
|
|
225 |
lst.filter(x => x % 2 == 0)
|
318
|
226 |
lst.filter(_ % 2 == 0)
|
204
|
227 |
|
320
|
228 |
|
362
|
229 |
lst.sortWith((x, y) => x < y)
|
|
230 |
lst.sortWith(_ > _)
|
204
|
231 |
|
318
|
232 |
// but this only works when the arguments are clear, but
|
|
233 |
// not with multiple occurences
|
|
234 |
lst.find(n => odd(n) && n > 2)
|
|
235 |
|
|
236 |
|
362
|
237 |
|
|
238 |
val ps = List((3, 0), (3, 2), (4, 2), (2, 2),
|
|
239 |
(2, 0), (1, 1), (1, 0))
|
318
|
240 |
|
212
|
241 |
def lex(x: (Int, Int), y: (Int, Int)) : Boolean =
|
|
242 |
if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
|
|
243 |
|
|
244 |
ps.sortWith(lex)
|
204
|
245 |
|
320
|
246 |
ps.sortBy(x => x._1)
|
204
|
247 |
ps.sortBy(_._2)
|
|
248 |
|
|
249 |
ps.maxBy(_._1)
|
|
250 |
ps.maxBy(_._2)
|
|
251 |
|
|
252 |
|
212
|
253 |
// maps (lower-case)
|
|
254 |
//===================
|
204
|
255 |
|
212
|
256 |
def double(x: Int): Int = x + x
|
204
|
257 |
def square(x: Int): Int = x * x
|
|
258 |
|
|
259 |
val lst = (1 to 10).toList
|
|
260 |
|
362
|
261 |
lst.map(square)
|
212
|
262 |
lst.map(x => (double(x), square(x)))
|
|
263 |
|
362
|
264 |
// works also for strings
|
|
265 |
def tweet(c: Char) = c.toUpper
|
|
266 |
|
|
267 |
"Hello World".map(tweet)
|
|
268 |
|
|
269 |
|
|
270 |
// this can be iterated
|
|
271 |
|
|
272 |
lst.map(square).filter(_ > 4)
|
|
273 |
|
363
|
274 |
lst.map(square).find(_ > 4)
|
|
275 |
lst.map(square).find(_ > 4).map(double)
|
|
276 |
|
|
277 |
lst.map(square)
|
362
|
278 |
.find(_ > 4)
|
363
|
279 |
.map(double)
|
|
280 |
|
|
281 |
|
|
282 |
// Option Type and maps
|
|
283 |
//======================
|
|
284 |
|
|
285 |
// a function that turns strings into numbers (similar to .toInt)
|
|
286 |
Integer.parseInt("12u34")
|
|
287 |
|
|
288 |
// maps on Options
|
|
289 |
|
|
290 |
import scala.util._
|
362
|
291 |
|
363
|
292 |
def get_me_an_int(s: String) : Option[Int] =
|
|
293 |
Try(Some(Integer.parseInt(s))).getOrElse(None)
|
|
294 |
|
|
295 |
get_me_an_int("12345").map(_ % 2 == 0)
|
|
296 |
get_me_an_int("12u34").map(_ % 2 == 0)
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
val lst = List("12345", "foo", "5432", "bar", "x21", "456")
|
|
301 |
for (x <- lst) yield get_me_an_int(x)
|
|
302 |
|
|
303 |
// summing up all the numbers
|
|
304 |
|
|
305 |
lst.map(get_me_an_int).flatten.sum
|
|
306 |
|
|
307 |
|
|
308 |
|
204
|
309 |
|
319
|
310 |
// this is actually how for-comprehensions are
|
|
311 |
// defined in Scala
|
204
|
312 |
|
|
313 |
lst.map(n => square(n))
|
|
314 |
for (n <- lst) yield square(n)
|
|
315 |
|
318
|
316 |
// lets define our own higher-order functions
|
|
317 |
// type of functions is for example Int => Int
|
204
|
318 |
|
212
|
319 |
|
363
|
320 |
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] =
|
|
321 |
{
|
204
|
322 |
if (lst == Nil) Nil
|
|
323 |
else f(lst.head) :: my_map_int(lst.tail, f)
|
|
324 |
}
|
|
325 |
|
|
326 |
my_map_int(lst, square)
|
|
327 |
|
|
328 |
// same function using pattern matching: a kind
|
|
329 |
// of switch statement on steroids (see more later on)
|
|
330 |
|
319
|
331 |
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] =
|
362
|
332 |
lst match {
|
|
333 |
case Nil => Nil
|
|
334 |
case x::xs => f(x)::my_map_int(xs, f)
|
|
335 |
}
|
204
|
336 |
|
|
337 |
|
363
|
338 |
|
|
339 |
val biglst = (1 to 10000).toList
|
|
340 |
my_map_int(biglst, double)
|
|
341 |
|
|
342 |
(1 to 10000000).toList.map(double)
|
|
343 |
|
204
|
344 |
// other function types
|
|
345 |
//
|
|
346 |
// f1: (Int, Int) => Int
|
|
347 |
// f2: List[String] => Option[Int]
|
|
348 |
// ...
|
|
349 |
|
|
350 |
|
320
|
351 |
|
204
|
352 |
|
|
353 |
|
212
|
354 |
// Map type (upper-case)
|
|
355 |
//=======================
|
204
|
356 |
|
|
357 |
// Note the difference between map and Map
|
|
358 |
|
364
|
359 |
val m = Map(1 -> "one", 2 -> "two", 10 -> "many")
|
320
|
360 |
|
364
|
361 |
List((1, "one"), (2, "two"), (10, "many")).toMap
|
320
|
362 |
|
364
|
363 |
m.get(1)
|
|
364 |
m.get(4)
|
204
|
365 |
|
364
|
366 |
m.getOrElse(1, "")
|
|
367 |
m.getOrElse(4, "")
|
204
|
368 |
|
364
|
369 |
val new_m = m + (10 -> "ten")
|
204
|
370 |
|
364
|
371 |
new_m.get(10)
|
|
372 |
|
|
373 |
val m2 = for ((k, v) <- m) yield (k, v.toUpperCase)
|
204
|
374 |
|
|
375 |
|
318
|
376 |
|
319
|
377 |
// groupBy function on Maps
|
364
|
378 |
val lst = List("one", "two", "three", "four", "five")
|
|
379 |
lst.groupBy(_.head)
|
204
|
380 |
|
364
|
381 |
lst.groupBy(_.length)
|
204
|
382 |
|
364
|
383 |
lst.groupBy(_.length).get(3)
|
|
384 |
|
|
385 |
val grps = lst.groupBy(_.length)
|
|
386 |
grps.keySet
|
204
|
387 |
|
|
388 |
|
51
|
389 |
|
192
|
390 |
|
|
391 |
// Pattern Matching
|
|
392 |
//==================
|
|
393 |
|
365
|
394 |
// A powerful tool which is supposed to come to Java in
|
|
395 |
// a few years time (https://www.youtube.com/watch?v=oGll155-vuQ).
|
|
396 |
// ...Scala already has it for many years ;o)
|
192
|
397 |
|
|
398 |
// The general schema:
|
|
399 |
//
|
|
400 |
// expression match {
|
|
401 |
// case pattern1 => expression1
|
|
402 |
// case pattern2 => expression2
|
|
403 |
// ...
|
|
404 |
// case patternN => expressionN
|
|
405 |
// }
|
|
406 |
|
|
407 |
|
319
|
408 |
// recall
|
365
|
409 |
def my_map_int(lst: List[Int], f: Int => Int) : List[Int] =
|
|
410 |
lst match {
|
|
411 |
case Nil => Nil
|
|
412 |
case x::xs => f(x)::my_map_int(xs, f)
|
|
413 |
}
|
58
|
414 |
|
365
|
415 |
def my_map_option(o: Option[Int], f: Int => Int) : Option[Int] =
|
|
416 |
o match {
|
|
417 |
case None => None
|
|
418 |
case Some(x) => Some(f(x))
|
|
419 |
}
|
58
|
420 |
|
365
|
421 |
my_map_option(None, x => x * x)
|
|
422 |
my_map_option(Some(8), x => x * x)
|
192
|
423 |
|
212
|
424 |
|
192
|
425 |
// you can also have cases combined
|
266
|
426 |
def season(month: String) : String = month match {
|
192
|
427 |
case "March" | "April" | "May" => "It's spring"
|
|
428 |
case "June" | "July" | "August" => "It's summer"
|
|
429 |
case "September" | "October" | "November" => "It's autumn"
|
204
|
430 |
case "December" => "It's winter"
|
|
431 |
case "January" | "February" => "It's unfortunately winter"
|
365
|
432 |
case _ => "Wrong month"
|
266
|
433 |
}
|
|
434 |
|
365
|
435 |
// pattern-match on integers
|
|
436 |
|
|
437 |
def fib(n: Int) : Int = n match {
|
|
438 |
case 0 | 1 => 1
|
|
439 |
case n => fib(n - 1) + fib(n - 2)
|
|
440 |
}
|
|
441 |
|
|
442 |
fib(10)
|
266
|
443 |
|
204
|
444 |
// Silly: fizz buzz
|
192
|
445 |
def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
|
|
446 |
case (0, 0) => "fizz buzz"
|
|
447 |
case (0, _) => "fizz"
|
|
448 |
case (_, 0) => "buzz"
|
|
449 |
case _ => n.toString
|
|
450 |
}
|
|
451 |
|
365
|
452 |
for (n <- 1 to 20)
|
192
|
453 |
println(fizz_buzz(n))
|
|
454 |
|
|
455 |
|
365
|
456 |
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
|
|
457 |
|
|
458 |
def my_flatten(xs: List[Option[Int]]): List[Int] =
|
|
459 |
xs match {
|
|
460 |
case Nil => Nil
|
|
461 |
case None::rest => my_flatten(rest)
|
|
462 |
case Some(v)::rest => v :: my_flatten(rest)
|
|
463 |
}
|
|
464 |
|
|
465 |
my_flatten(List(None, Some(1), Some(2), None, Some(3)))
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
278
|
472 |
|
|
473 |
|
309
|
474 |
// Recursion
|
|
475 |
//===========
|
|
476 |
|
|
477 |
|
318
|
478 |
/* Say you have characters a, b, c.
|
|
479 |
What are all the combinations of a certain length?
|
309
|
480 |
|
318
|
481 |
All combinations of length 2:
|
|
482 |
|
|
483 |
aa, ab, ac, ba, bb, bc, ca, cb, cc
|
|
484 |
|
|
485 |
Combinations of length 3:
|
|
486 |
|
|
487 |
aaa, baa, caa, and so on......
|
309
|
488 |
*/
|
|
489 |
|
320
|
490 |
def combs(cs: List[Char], n: Int) : List[String] = {
|
|
491 |
if (n == 0) List("")
|
|
492 |
else for (c <- cs; s <- combs(cs, n - 1)) yield s"$c$s"
|
|
493 |
}
|
|
494 |
|
|
495 |
combs(List('a', 'b', 'c'), 3)
|
|
496 |
|
|
497 |
|
|
498 |
|
318
|
499 |
def combs(cs: List[Char], l: Int) : List[String] = {
|
309
|
500 |
if (l == 0) List("")
|
318
|
501 |
else for (c <- cs; s <- combs(cs, l - 1)) yield s"$c$s"
|
309
|
502 |
}
|
|
503 |
|
318
|
504 |
combs("abc".toList, 2)
|
|
505 |
|
|
506 |
|
329
|
507 |
// When writing recursive functions you have to
|
|
508 |
// think about three points
|
|
509 |
//
|
|
510 |
// - How to start with a recursive function
|
|
511 |
// - How to communicate between recursive calls
|
|
512 |
// - Exit conditions
|
|
513 |
|
|
514 |
|
147
|
515 |
|
318
|
516 |
// A Recursive Web Crawler / Email Harvester
|
|
517 |
//===========================================
|
204
|
518 |
//
|
212
|
519 |
// the idea is to look for links using the
|
|
520 |
// regular expression "https?://[^"]*" and for
|
|
521 |
// email addresses using another regex.
|
204
|
522 |
|
|
523 |
import io.Source
|
|
524 |
import scala.util._
|
|
525 |
|
|
526 |
// gets the first 10K of a web-page
|
|
527 |
def get_page(url: String) : String = {
|
|
528 |
Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
|
|
529 |
getOrElse { println(s" Problem with: $url"); ""}
|
147
|
530 |
}
|
|
531 |
|
204
|
532 |
// regex for URLs and emails
|
|
533 |
val http_pattern = """"https?://[^"]*"""".r
|
|
534 |
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
|
|
535 |
|
268
|
536 |
//test case:
|
212
|
537 |
//email_pattern.findAllIn
|
|
538 |
// ("foo bla christian@kcl.ac.uk 1234567").toList
|
|
539 |
|
204
|
540 |
|
|
541 |
// drops the first and last character from a string
|
|
542 |
def unquote(s: String) = s.drop(1).dropRight(1)
|
|
543 |
|
|
544 |
def get_all_URLs(page: String): Set[String] =
|
|
545 |
http_pattern.findAllIn(page).map(unquote).toSet
|
|
546 |
|
|
547 |
// naive version of crawl - searches until a given depth,
|
|
548 |
// visits pages potentially more than once
|
318
|
549 |
def crawl(url: String, n: Int) : Unit = {
|
|
550 |
if (n == 0) ()
|
204
|
551 |
else {
|
|
552 |
println(s" Visiting: $n $url")
|
318
|
553 |
for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
|
204
|
554 |
}
|
147
|
555 |
}
|
|
556 |
|
204
|
557 |
// some starting URLs for the crawler
|
|
558 |
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
|
147
|
559 |
|
204
|
560 |
crawl(startURL, 2)
|
|
561 |
|
|
562 |
|
318
|
563 |
// a primitive email harvester
|
|
564 |
def emails(url: String, n: Int) : Set[String] = {
|
|
565 |
if (n == 0) Set()
|
|
566 |
else {
|
|
567 |
println(s" Visiting: $n $url")
|
|
568 |
val page = get_page(url)
|
|
569 |
val new_emails = email_pattern.findAllIn(page).toSet
|
|
570 |
new_emails ++ (for (u <- get_all_URLs(page)) yield emails(u, n - 1)).flatten
|
|
571 |
}
|
|
572 |
}
|
55
|
573 |
|
318
|
574 |
emails(startURL, 3)
|
55
|
575 |
|
|
576 |
|
318
|
577 |
// if we want to explore the internet "deeper", then we
|
|
578 |
// first have to parallelise the request of webpages:
|
|
579 |
//
|
|
580 |
// scala -cp scala-parallel-collections_2.13-0.2.0.jar
|
|
581 |
// import scala.collection.parallel.CollectionConverters._
|
55
|
582 |
|
53
|
583 |
|
|
584 |
|
|
585 |
|
192
|
586 |
|
319
|
587 |
// Jumping Towers
|
|
588 |
//================
|
278
|
589 |
|
319
|
590 |
|
364
|
591 |
def moves(xs: List[Int], n: Int) : List[List[Int]] =
|
|
592 |
(xs, n) match {
|
|
593 |
case (Nil, _) => Nil
|
366
|
594 |
case (_, 0) => Nil
|
364
|
595 |
case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
|
|
596 |
}
|
319
|
597 |
|
366
|
598 |
// List(5,5,1,0) -> moves(List(5,1,0), 5)
|
319
|
599 |
moves(List(5,1,0), 1)
|
|
600 |
moves(List(5,1,0), 2)
|
|
601 |
moves(List(5,1,0), 5)
|
|
602 |
|
|
603 |
// checks whether a jump tour exists at all
|
|
604 |
|
|
605 |
def search(xs: List[Int]) : Boolean = xs match {
|
|
606 |
case Nil => true
|
366
|
607 |
case x::xs =>
|
|
608 |
if (xs.length < x) true
|
|
609 |
else moves(xs, x).exists(search(_))
|
319
|
610 |
}
|
|
611 |
|
|
612 |
|
|
613 |
search(List(5,3,2,5,1,1))
|
|
614 |
search(List(3,5,1,0,0,0,1))
|
|
615 |
search(List(3,5,1,0,0,0,0,1))
|
|
616 |
search(List(3,5,1,0,0,0,1,1))
|
|
617 |
search(List(3,5,1))
|
|
618 |
search(List(5,1,1))
|
|
619 |
search(Nil)
|
|
620 |
search(List(1))
|
|
621 |
search(List(5,1,1))
|
|
622 |
search(List(3,5,1,0,0,0,0,0,0,0,0,1))
|
|
623 |
|
366
|
624 |
|
|
625 |
import scala.util._
|
|
626 |
List.fill(100)(Random.nextInt(2))
|
|
627 |
search(List.fill(100)(Random.nextInt(10)))
|
|
628 |
|
319
|
629 |
// generate *all* jump tours
|
|
630 |
// if we are only interested in the shortes one, we could
|
|
631 |
// shortcircut the calculation and only return List(x) in
|
|
632 |
// case where xs.length < x, because no tour can be shorter
|
|
633 |
// than 1
|
|
634 |
//
|
|
635 |
|
|
636 |
def jumps(xs: List[Int]) : List[List[Int]] = xs match {
|
|
637 |
case Nil => Nil
|
366
|
638 |
case x::xs => {
|
319
|
639 |
val children = moves(xs, x)
|
366
|
640 |
val results =
|
|
641 |
children.map(cs => jumps(cs).map(x :: _)).flatten
|
319
|
642 |
if (xs.length < x) List(x) :: results else results
|
|
643 |
}
|
|
644 |
}
|
|
645 |
|
|
646 |
jumps(List(3,5,1,2,1,2,1))
|
|
647 |
jumps(List(3,5,1,2,3,4,1))
|
|
648 |
jumps(List(3,5,1,0,0,0,1))
|
|
649 |
jumps(List(3,5,1))
|
|
650 |
jumps(List(5,1,1))
|
|
651 |
jumps(Nil)
|
|
652 |
jumps(List(1))
|
|
653 |
jumps(List(5,1,2))
|
|
654 |
moves(List(1,2), 5)
|
|
655 |
jumps(List(1,5,1,2))
|
|
656 |
jumps(List(3,5,1,0,0,0,0,0,0,0,0,1))
|
|
657 |
|
|
658 |
jumps(List(5,3,2,5,1,1)).minBy(_.length)
|
|
659 |
jumps(List(1,3,5,8,9,2,6,7,6,8,9)).minBy(_.length)
|
|
660 |
jumps(List(1,3,6,1,0,9)).minBy(_.length)
|
|
661 |
jumps(List(2,3,1,1,2,4,2,0,1,1)).minBy(_.length)
|
|
662 |
|
|
663 |
|