progs/lecture1.scala
changeset 32 45557ad18ea6
parent 26 a7afc2540a88
child 33 b6cb302633eb
equal deleted inserted replaced
31:d0caa12ab8d8 32:45557ad18ea6
    41 println(lst.mkString("\n"))
    41 println(lst.mkString("\n"))
    42 
    42 
    43 // some methods take more than one argument
    43 // some methods take more than one argument
    44 println(lst.mkString("[", ",", "]"))
    44 println(lst.mkString("[", ",", "]"))
    45 
    45 
       
    46 
    46 // Conversion methods
    47 // Conversion methods
    47 //====================
    48 //====================
    48 
    49 
    49 List(1,2,3,1).toString
    50 List(1,2,3,1).toString
    50 List(1,2,3,1).toSet
    51 List(1,2,3,1).toSet
    51 "hello".toList
    52 "hello".toList
    52 1.toDouble
    53 1.toDouble
    53 
    54 
    54 
    55 
       
    56 // useful list methods
       
    57 
       
    58 List(1,2,3,4).length
    55 List(1,2,3,4).reverse
    59 List(1,2,3,4).reverse
       
    60 List(1,2,3,4).max
       
    61 List(1,2,3,4).min
       
    62 List(1,2,3,4).sum
       
    63 List(1,2,3,4).take(2).sum
       
    64 List(1,2,3,4).drop(2).sum
       
    65 List(1,2,3,4,3).indexOf(3)
       
    66 
       
    67 "1,2,3,4,5".split(",").toList
       
    68 
    56 
    69 
    57 // Types
    70 // Types
    58 //=======
    71 //=======
    59 
    72 
    60 /* Scala is a strongly typed language
    73 /* Scala is a strongly typed language
   105 t._4
   118 t._4
   106 
   119 
   107 // Hello World
   120 // Hello World
   108 //=============
   121 //=============
   109 
   122 
   110 // show an example of a stand-alone scala file
   123 // an example of a stand-alone scala file;
   111 // remind that in the course work you are asked a 
   124 // in the coursework students must submit 
   112 // plain scala "work-sheet"
   125 // plain scala "work-sheets"
   113 
   126 
       
   127 object Hello extends App { 
       
   128   println("hello world")
       
   129 }
       
   130 
       
   131 // can be called with
       
   132 //
       
   133 // $> scalac hello-world.scala
       
   134 // $> scala Hello
       
   135 //
       
   136 // $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
   114 
   137 
   115 
   138 
   116 // Function Definitions
   139 // Function Definitions
   117 //======================
   140 //======================
   118 
   141 
   173 
   196 
   174 
   197 
   175 // Assert/Testing
   198 // Assert/Testing
   176 //================
   199 //================
   177 
   200 
       
   201 assert(gcd(48, 18) == 6)
       
   202 
       
   203 assert(gcd(48, 18) == 5, "The gcd test failed")
       
   204 
       
   205 
   178 // For-Comprehensions (not For-Loops)
   206 // For-Comprehensions (not For-Loops)
   179 //====================================
   207 //====================================
   180 
   208 
   181 for (n <- (1 to 10).toList) yield square(n)
   209 for (n <- (1 to 10).toList) yield square(n)
   182 
   210 
   189        m <- (1 to 10).toList) yield m * n
   217        m <- (1 to 10).toList) yield m * n
   190 
   218 
   191 mult_table.sliding(10,10).mkString("\n")
   219 mult_table.sliding(10,10).mkString("\n")
   192 
   220 
   193 
   221 
       
   222 // with if-predicates
       
   223 
       
   224 for (n <- (1 to 3).toList; 
       
   225      m <- (1 to 3).toList;
       
   226      if (n + m) % 2 == 0) yield (n, m)
       
   227 
       
   228 
       
   229 
   194 // with patterns
   230 // with patterns
   195 
   231 
   196 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
   232 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
   197 
   233 
   198 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
   234 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
       
   235 
       
   236 
       
   237 
       
   238 // with only a side-effect (no list is produced)
       
   239 // has no "yield"
       
   240 
       
   241 for (n <- (1 to 10)) println(n)
       
   242 
       
   243 
       
   244 // concurrency (ONLY WORKS IN 2.11.8)
       
   245 for (n <- (1 to 10)) println(n)
       
   246 for (n <- (1 to 10).par) println(n)
       
   247 
       
   248 
       
   249 
       
   250 // for testing time
       
   251 def time_needed[T](i: Int, code: => T) = {
       
   252   val start = System.nanoTime()
       
   253   for (j <- 1 to i) code
       
   254   val end = System.nanoTime()
       
   255   ((end - start) / i / 1.0e9) + " secs"
       
   256 }
       
   257 
       
   258 val list = (1 to 1000000).toList
       
   259 time_needed(10, for (n <- list) yield n + 42)
       
   260 time_needed(10, for (n <- list.par) yield n + 42)
   199 
   261 
   200 
   262 
   201 
   263 
   202 // Webpages
   264 // Webpages
   203 //==========
   265 //==========
       
   266 
       
   267 import io.Source
       
   268 
       
   269 val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" 
       
   270 Source.fromURL(url)("ISO-8859-1").mkString
       
   271 
       
   272 
       
   273 
       
   274 def price_lookup(symbol: String): String = {
       
   275   val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
       
   276   Source.fromURL(url).mkString.drop(1).dropRight(2)
       
   277 }
       
   278 
       
   279 price_lookup("GOOG")
       
   280 price_lookup("AAPL")
       
   281 
       
   282 
       
   283 val companies = 
       
   284   List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
       
   285 
       
   286 for (s <- companies.par) println(price_lookup(s))
       
   287 
       
   288 
       
   289 // A Web Crawler
       
   290 //===============
       
   291 
       
   292 import io.Source
       
   293 import scala.util.matching.Regex
       
   294 import scala.util._
       
   295 
       
   296 // gets the first 10K of a web-page
       
   297 def get_page(url: String) : String = {
       
   298   Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
       
   299     getOrElse { println(s"  Problem with: $url"); ""}
       
   300 }
       
   301 
       
   302 // regex for URLs
       
   303 val http_pattern = """"https?://[^"]*"""".r
       
   304 
       
   305 // drops the first and last character from a string
       
   306 def unquote(s: String) = s.drop(1).dropRight(1)
       
   307 
       
   308 def get_all_URLs(page: String): Set[String] = 
       
   309   http_pattern.findAllIn(page).map(unquote).toSet
       
   310 
       
   311 // naive version of crawl - searches until a given depth,
       
   312 // visits pages potentially more than once
       
   313 def crawl(url: String, n: Int): Unit = {
       
   314   if (n == 0) ()
       
   315   else {
       
   316     println(s"Visiting: $n $url")
       
   317     for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)
       
   318   }
       
   319 }
       
   320 
       
   321 // some starting URLs for the crawler
       
   322 val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc"""
       
   323 //val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney"""
       
   324 
       
   325 crawl(startURL, 2)
       
   326 
       
   327 
       
   328 
       
   329 
       
   330 // Adding your own methods to Strings
       
   331 //====================================
       
   332 
       
   333 // imagine you want to implement an additional
       
   334 // method to strings, like
       
   335 //
       
   336 //     "HAL".increment
       
   337 //
       
   338 // you can avoid ugly fudges, like a MyString
       
   339 // class by using implicit conversions
       
   340 
       
   341 
       
   342 implicit class MyString(s: String) {
       
   343   def increment = for (c <- s) yield (c + 1).toChar 
       
   344 }
       
   345 
       
   346 "HAL".increment
       
   347 
       
   348 
       
   349 // Further Information
       
   350 //=====================
       
   351 
       
   352 // Scala download
       
   353 //
       
   354 //  http://www.scala-lang.org
       
   355 
       
   356 // Eclipse for Scala
       
   357 //
       
   358 //  http://scala-ide.org/download/sdk.html
       
   359 
       
   360 
       
   361 // library docs
       
   362 //
       
   363 //  http://www.scala-lang.org/api/current/
       
   364 
       
   365 // tutorials
       
   366 //
       
   367 //  http://docs.scala-lang.org/tutorials/