64 List(1,2,3,4).max |
68 List(1,2,3,4).max |
65 List(1,2,3,4).min |
69 List(1,2,3,4).min |
66 List(1,2,3,4).sum |
70 List(1,2,3,4).sum |
67 List(1,2,3,4).take(2).sum |
71 List(1,2,3,4).take(2).sum |
68 List(1,2,3,4).drop(2).sum |
72 List(1,2,3,4).drop(2).sum |
69 List(1,2,3,4,3).indexOf(3) |
73 List(1,2,3,4,3)indexOf(3) |
70 |
74 |
71 "1,2,3,4,5".split(",").mkString("\n") |
75 "1,2,3,4,5".split(",").mkString("\n") |
72 "1,2,3,4,5".split(",3,").mkString("\n") |
76 "1,2,3,4,5".split(",3,").mkString("\n") |
73 |
77 |
74 // Types |
78 // Types |
188 if (n == 1) 1 else fib(n - 1) + fib(n - 2) |
194 if (n == 1) 1 else fib(n - 1) + fib(n - 2) |
189 |
195 |
190 |
196 |
191 //gcd - Euclid's algorithm |
197 //gcd - Euclid's algorithm |
192 |
198 |
193 def gcd(a: Int, b: Int): Int = |
199 def gcd(a: Int, b: Int) : Int = |
194 if (b == 0) a else gcd(b, a % b) |
200 if (b == 0) a else gcd(b, a % b) |
195 |
201 |
196 gcd(48, 18) |
202 gcd(48, 18) |
|
203 |
|
204 |
|
205 def power(x: Int, n: Int) : Int = |
|
206 if (n == 0) 1 else x * power(x, n - 1) |
|
207 |
|
208 power(5, 5) |
197 |
209 |
198 |
210 |
199 // String Interpolations |
211 // String Interpolations |
200 //======================= |
212 //======================= |
201 |
213 |
283 //========== |
295 //========== |
284 |
296 |
285 import io.Source |
297 import io.Source |
286 |
298 |
287 // obtaining a webpage |
299 // obtaining a webpage |
288 val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" |
300 val url = """https://nms.kcl.ac.uk/christian.urban/""" |
289 Source.fromURL(url)("ISO-8859-1").mkString |
301 Source.fromURL(url)("ISO-8859-1").mkString |
290 |
302 |
291 |
303 |
292 // function for looking up stockmarket data |
304 // function for looking up stockmarket data |
293 def price_lookup(symbol: String): String = { |
305 def price_lookup(symbol: String): String = { |
294 val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1" |
306 val url = "https://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1" |
295 Source.fromURL(url).mkString.drop(1).dropRight(2) |
307 Source.fromURL(url).mkString.drop(1).dropRight(2) |
296 } |
308 } |
297 |
309 |
298 price_lookup("GOOG") |
310 price_lookup("GOOG") |
299 price_lookup("AAPL") |
311 price_lookup("AAPL") |
300 |
312 |
301 |
313 |
302 val companies = |
314 val companies = |
303 List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
315 List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") |
304 |
316 |
305 for (s <- companies.par) println(price_lookup(s)) |
317 for (s <- companies) println(price_lookup(s)) |
306 |
318 |
307 |
319 |
308 // A Web Crawler |
320 // A Web Crawler |
309 //=============== |
321 //=============== |
310 // |
322 // |
311 // the idea is to look for dead links |
323 // the idea is to look for dead links using the |
|
324 // regular expression "https?://[^"]*" |
312 |
325 |
313 import io.Source |
326 import io.Source |
314 import scala.util.matching.Regex |
327 import scala.util.matching.Regex |
315 import scala.util._ |
328 import scala.util._ |
316 |
329 |
338 for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1) |
351 for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1) |
339 } |
352 } |
340 } |
353 } |
341 |
354 |
342 // some starting URLs for the crawler |
355 // some starting URLs for the crawler |
343 val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc""" |
356 val startURL = """https://nms.kcl.ac.uk/christian.urban/""" |
344 //val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney""" |
357 //val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney""" |
345 |
358 |
346 crawl(startURL, 2) |
359 crawl(startURL, 2) |
347 |
360 |
348 |
361 |
349 |
362 |
350 // Further Information |
363 // Further Information |
351 //===================== |
364 //===================== |
352 |
365 |
353 // Scala download |
366 // The Scala home page and general information is at |
354 // |
367 // |
355 // http://www.scala-lang.org |
368 // http://www.scala-lang.org |
356 |
369 // http://docs.scala-lang.org |
357 // Eclipse for Scala |
370 // |
358 // |
371 // |
359 // http://scala-ide.org/download/sdk.html |
372 // It should be fairly easy to install the Scala binary and |
360 |
373 // run Scala on the commandline. There are also at least |
361 |
374 // four IDEs you can use with Scala: |
362 // library docs |
375 // |
|
376 // (0) Some general information for setting up IDEs |
|
377 // with Scala support can be found at |
|
378 // |
|
379 // http://docs.scala-lang.org/getting-started.html |
|
380 // |
|
381 // (1) Eclipse for Scala (one big bundle) |
|
382 // |
|
383 // http://scala-ide.org/download/sdk.html |
|
384 // |
|
385 // (2) IntelliJ (needs additional Plugins) |
|
386 // |
|
387 // https://www.jetbrains.com/idea/ |
|
388 // http://docs.scala-lang.org/getting-started-intellij-track/getting-started-with-scala-in-intellij.html |
|
389 // |
|
390 // (3) Sublime (not free, but unlimited trial period; |
|
391 // needs SublimeREPL plugin) |
|
392 // |
|
393 // https://www.sublimetext.com |
|
394 // |
|
395 // (4) Emacs (old-fashioned, but reliable) |
|
396 // |
|
397 // https://www.gnu.org/software/emacs/ |
|
398 // |
|
399 // I use the old scala-tool support for Emacs distributed at |
|
400 // |
|
401 // https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs |
|
402 // |
|
403 // but there is also support for the newer Ensime Scala Mode |
|
404 // |
|
405 // http://ensime.org/editors/emacs/scala-mode/ |
|
406 // |
|
407 // There is also Scala support in the Atom editor, but my |
|
408 // experience is mixed. People also use Scala with Vim and Jedit. |
|
409 // |
|
410 // All of the IDEs above support a REPL for Scala. Some of them have |
|
411 // the very nifty feature of a Scala Worksheet -- you just save your |
|
412 // file and it will be automatically evaluated and the result pasted |
|
413 // into your file. However, this way of writing Scala code never worked |
|
414 // for me. I just use the REPL. |
|
415 // |
|
416 // |
|
417 // Scala Library Docs |
363 // |
418 // |
364 // http://www.scala-lang.org/api/current/ |
419 // http://www.scala-lang.org/api/current/ |
365 |
420 // |
366 // tutorials |
421 // Scala Tutorials |
367 // |
422 // |
368 // http://docs.scala-lang.org/tutorials/ |
423 // http://docs.scala-lang.org/tutorials/ |
|
424 // |
|
425 // There are also a massive number of Scala tutorials on youtube |
|
426 // and there are tons of books and free material. |
|
427 // |
|
428 |
|
429 |