21
|
1 |
// Lecture 1
|
25
|
2 |
//===========
|
14
|
3 |
|
26
|
4 |
// Value assignments
|
25
|
5 |
// (variable names should be lower case)
|
|
6 |
//======================================
|
21
|
7 |
|
14
|
8 |
val x = 42
|
|
9 |
val y = 3 + 4
|
|
10 |
|
|
11 |
|
25
|
12 |
// Collections
|
|
13 |
//=============
|
14
|
14 |
List(1,2,3,1)
|
|
15 |
Set(1,2,3,1)
|
|
16 |
|
|
17 |
1 to 10
|
|
18 |
(1 to 10).toList
|
|
19 |
|
|
20 |
(1 until 10).toList
|
|
21 |
|
18
|
22 |
// an element in a list
|
33
|
23 |
val lst = List(1, 2, 3, 1)
|
|
24 |
lst(0)
|
|
25 |
lst(2)
|
18
|
26 |
|
23
|
27 |
1::2::3::Nil
|
|
28 |
List(1, 2, 3) ::: List(4, 5, 6)
|
14
|
29 |
|
25
|
30 |
// Printing/Strings
|
|
31 |
//==================
|
14
|
32 |
|
|
33 |
println("test")
|
15
|
34 |
|
33
|
35 |
val tst = "This is a " + "test\n"
|
14
|
36 |
println(tst)
|
|
37 |
|
|
38 |
val lst = List(1,2,3,1)
|
|
39 |
|
|
40 |
println(lst.toString)
|
|
41 |
println(lst.mkString("\n"))
|
|
42 |
|
33
|
43 |
println(lst.mkString(", "))
|
|
44 |
|
14
|
45 |
// some methods take more than one argument
|
21
|
46 |
println(lst.mkString("[", ",", "]"))
|
14
|
47 |
|
32
|
48 |
|
25
|
49 |
// Conversion methods
|
|
50 |
//====================
|
14
|
51 |
|
|
52 |
List(1,2,3,1).toString
|
|
53 |
List(1,2,3,1).toSet
|
|
54 |
"hello".toList
|
|
55 |
1.toDouble
|
|
56 |
|
25
|
57 |
|
32
|
58 |
// useful list methods
|
|
59 |
|
|
60 |
List(1,2,3,4).length
|
25
|
61 |
List(1,2,3,4).reverse
|
32
|
62 |
List(1,2,3,4).max
|
|
63 |
List(1,2,3,4).min
|
|
64 |
List(1,2,3,4).sum
|
|
65 |
List(1,2,3,4).take(2).sum
|
|
66 |
List(1,2,3,4).drop(2).sum
|
|
67 |
List(1,2,3,4,3).indexOf(3)
|
|
68 |
|
|
69 |
"1,2,3,4,5".split(",").toList
|
|
70 |
|
25
|
71 |
|
|
72 |
// Types
|
|
73 |
//=======
|
|
74 |
|
|
75 |
/* Scala is a strongly typed language
|
|
76 |
|
|
77 |
* Base types
|
14
|
78 |
|
25
|
79 |
Int, Long, BigInt, Float, Double
|
|
80 |
String, Char
|
|
81 |
Boolean
|
|
82 |
|
|
83 |
* Compound types
|
12
|
84 |
|
25
|
85 |
List[Int],
|
|
86 |
Set[Double]
|
|
87 |
Pairs: (Int, String)
|
|
88 |
List[(BigInt, String)]
|
|
89 |
*/
|
12
|
90 |
|
25
|
91 |
// Smart Strings
|
|
92 |
//===============
|
|
93 |
|
23
|
94 |
println(">\n<")
|
|
95 |
println(""">\n<""")
|
|
96 |
|
|
97 |
/* in Java
|
|
98 |
val lyrics = "Baa, Baa, Black Sheep \n" +
|
|
99 |
"Have you any wool? \n" +
|
|
100 |
"Yes, sir, yes sir \n" +
|
|
101 |
"Three bags full"
|
|
102 |
*/
|
|
103 |
|
|
104 |
val lyrics = """Baa, Baa, Black Sheep
|
|
105 |
|Have you any wool?
|
|
106 |
|Yes, sir, yes sir
|
|
107 |
|Three bags full""".stripMargin
|
|
108 |
|
|
109 |
println(lyrics)
|
|
110 |
|
14
|
111 |
|
25
|
112 |
// Pairs/Tuples
|
|
113 |
//==============
|
14
|
114 |
|
|
115 |
val p = (1, "one")
|
|
116 |
p._1
|
|
117 |
p._2
|
|
118 |
|
|
119 |
val t = (4,1,2,3)
|
|
120 |
t._4
|
|
121 |
|
25
|
122 |
// Hello World
|
|
123 |
//=============
|
|
124 |
|
32
|
125 |
// an example of a stand-alone scala file;
|
|
126 |
// in the coursework students must submit
|
|
127 |
// plain scala "work-sheets"
|
25
|
128 |
|
32
|
129 |
object Hello extends App {
|
|
130 |
println("hello world")
|
|
131 |
}
|
|
132 |
|
|
133 |
// can be called with
|
|
134 |
//
|
|
135 |
// $> scalac hello-world.scala
|
|
136 |
// $> scala Hello
|
|
137 |
//
|
|
138 |
// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
|
25
|
139 |
|
|
140 |
|
|
141 |
// Function Definitions
|
|
142 |
//======================
|
14
|
143 |
|
|
144 |
def square(x: Int): Int = x * x
|
|
145 |
|
25
|
146 |
square(6)
|
21
|
147 |
|
|
148 |
|
25
|
149 |
// If control structure
|
|
150 |
//======================
|
14
|
151 |
|
|
152 |
def fact(n: Int): Int =
|
|
153 |
if (n == 0) 1 else n * fact(n - 1)
|
|
154 |
|
25
|
155 |
/* boolean operators
|
|
156 |
|
|
157 |
== equals
|
|
158 |
! not
|
|
159 |
&& || and, or
|
|
160 |
*/
|
15
|
161 |
|
|
162 |
|
14
|
163 |
def fact2(n: BigInt): BigInt =
|
|
164 |
if (n == 0) 1 else n * fact2(n - 1)
|
|
165 |
|
25
|
166 |
fact2(150)
|
|
167 |
|
26
|
168 |
|
14
|
169 |
def fib(n: Int): Int =
|
|
170 |
if (n == 0) 1 else
|
26
|
171 |
if (n == 1) 1 else fib(n - 1) + fib(n - 2)
|
14
|
172 |
|
|
173 |
|
26
|
174 |
//gcd - Euclid's algorithm
|
|
175 |
|
|
176 |
def gcd(a: Int, b: Int): Int =
|
|
177 |
if (b == 0) a else gcd(b, a % b)
|
|
178 |
|
|
179 |
gcd(48, 18)
|
|
180 |
|
14
|
181 |
|
25
|
182 |
// String Interpolations
|
|
183 |
//=======================
|
14
|
184 |
|
26
|
185 |
val n = 3
|
|
186 |
println("The square of " + n + " is " + square(n) + ".")
|
|
187 |
|
|
188 |
println(s"The square of ${n} is ${square(n)}.")
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
def gcd_db(a: Int, b: Int): Int = {
|
|
193 |
println(s"Function called with ${a} and ${b}.")
|
|
194 |
if (b == 0) a else gcd_db(b, a % b)
|
|
195 |
}
|
|
196 |
|
|
197 |
gcd_db(48, 18)
|
|
198 |
|
14
|
199 |
|
25
|
200 |
// Assert/Testing
|
|
201 |
//================
|
14
|
202 |
|
32
|
203 |
assert(gcd(48, 18) == 6)
|
|
204 |
|
|
205 |
assert(gcd(48, 18) == 5, "The gcd test failed")
|
|
206 |
|
|
207 |
|
26
|
208 |
// For-Comprehensions (not For-Loops)
|
|
209 |
//====================================
|
14
|
210 |
|
|
211 |
for (n <- (1 to 10).toList) yield square(n)
|
|
212 |
|
25
|
213 |
for (n <- (1 to 10).toList;
|
|
214 |
m <- (1 to 10).toList) yield m * n
|
21
|
215 |
|
|
216 |
|
26
|
217 |
val mult_table =
|
|
218 |
for (n <- (1 to 10).toList;
|
|
219 |
m <- (1 to 10).toList) yield m * n
|
|
220 |
|
|
221 |
mult_table.sliding(10,10).mkString("\n")
|
|
222 |
|
25
|
223 |
|
32
|
224 |
// with if-predicates
|
|
225 |
|
|
226 |
for (n <- (1 to 3).toList;
|
|
227 |
m <- (1 to 3).toList;
|
|
228 |
if (n + m) % 2 == 0) yield (n, m)
|
|
229 |
|
|
230 |
|
|
231 |
|
26
|
232 |
// with patterns
|
|
233 |
|
|
234 |
for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n
|
|
235 |
|
|
236 |
for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2
|
|
237 |
|
25
|
238 |
|
|
239 |
|
32
|
240 |
// with only a side-effect (no list is produced)
|
|
241 |
// has no "yield"
|
|
242 |
|
|
243 |
for (n <- (1 to 10)) println(n)
|
|
244 |
|
|
245 |
|
|
246 |
// concurrency (ONLY WORKS IN 2.11.8)
|
|
247 |
for (n <- (1 to 10)) println(n)
|
|
248 |
for (n <- (1 to 10).par) println(n)
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
// for testing time
|
|
253 |
def time_needed[T](i: Int, code: => T) = {
|
|
254 |
val start = System.nanoTime()
|
|
255 |
for (j <- 1 to i) code
|
|
256 |
val end = System.nanoTime()
|
|
257 |
((end - start) / i / 1.0e9) + " secs"
|
|
258 |
}
|
|
259 |
|
|
260 |
val list = (1 to 1000000).toList
|
|
261 |
time_needed(10, for (n <- list) yield n + 42)
|
|
262 |
time_needed(10, for (n <- list.par) yield n + 42)
|
|
263 |
|
|
264 |
|
|
265 |
|
25
|
266 |
// Webpages
|
|
267 |
//==========
|
32
|
268 |
|
|
269 |
import io.Source
|
|
270 |
|
|
271 |
val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
|
|
272 |
Source.fromURL(url)("ISO-8859-1").mkString
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
def price_lookup(symbol: String): String = {
|
|
277 |
val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
|
|
278 |
Source.fromURL(url).mkString.drop(1).dropRight(2)
|
|
279 |
}
|
|
280 |
|
|
281 |
price_lookup("GOOG")
|
|
282 |
price_lookup("AAPL")
|
|
283 |
|
|
284 |
|
|
285 |
val companies =
|
|
286 |
List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
|
|
287 |
|
|
288 |
for (s <- companies.par) println(price_lookup(s))
|
|
289 |
|
|
290 |
|
|
291 |
// A Web Crawler
|
|
292 |
//===============
|
|
293 |
|
|
294 |
import io.Source
|
|
295 |
import scala.util.matching.Regex
|
|
296 |
import scala.util._
|
|
297 |
|
|
298 |
// gets the first 10K of a web-page
|
|
299 |
def get_page(url: String) : String = {
|
|
300 |
Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
|
|
301 |
getOrElse { println(s" Problem with: $url"); ""}
|
|
302 |
}
|
|
303 |
|
|
304 |
// regex for URLs
|
|
305 |
val http_pattern = """"https?://[^"]*"""".r
|
|
306 |
|
|
307 |
// drops the first and last character from a string
|
|
308 |
def unquote(s: String) = s.drop(1).dropRight(1)
|
|
309 |
|
|
310 |
def get_all_URLs(page: String): Set[String] =
|
|
311 |
http_pattern.findAllIn(page).map(unquote).toSet
|
|
312 |
|
|
313 |
// naive version of crawl - searches until a given depth,
|
|
314 |
// visits pages potentially more than once
|
|
315 |
def crawl(url: String, n: Int): Unit = {
|
|
316 |
if (n == 0) ()
|
|
317 |
else {
|
|
318 |
println(s"Visiting: $n $url")
|
|
319 |
for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
|
323 |
// some starting URLs for the crawler
|
|
324 |
val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc"""
|
|
325 |
//val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney"""
|
|
326 |
|
|
327 |
crawl(startURL, 2)
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
// Adding your own methods to Strings
|
|
333 |
//====================================
|
|
334 |
|
|
335 |
// imagine you want to implement an additional
|
|
336 |
// method to strings, like
|
|
337 |
//
|
|
338 |
// "HAL".increment
|
|
339 |
//
|
|
340 |
// you can avoid ugly fudges, like a MyString
|
|
341 |
// class by using implicit conversions
|
|
342 |
|
|
343 |
|
|
344 |
implicit class MyString(s: String) {
|
|
345 |
def increment = for (c <- s) yield (c + 1).toChar
|
|
346 |
}
|
|
347 |
|
|
348 |
"HAL".increment
|
|
349 |
|
|
350 |
|
|
351 |
// Further Information
|
|
352 |
//=====================
|
|
353 |
|
|
354 |
// Scala download
|
|
355 |
//
|
|
356 |
// http://www.scala-lang.org
|
|
357 |
|
|
358 |
// Eclipse for Scala
|
|
359 |
//
|
|
360 |
// http://scala-ide.org/download/sdk.html
|
|
361 |
|
|
362 |
|
|
363 |
// library docs
|
|
364 |
//
|
|
365 |
// http://www.scala-lang.org/api/current/
|
|
366 |
|
|
367 |
// tutorials
|
|
368 |
//
|
|
369 |
// http://docs.scala-lang.org/tutorials/
|