progs/lecture1.scala
changeset 36 f5ed0fef41b3
parent 34 40424bbe109e
child 51 0e60e6c24b99
equal deleted inserted replaced
34:40424bbe109e 36:f5ed0fef41b3
    66 List(1,2,3,4).sum
    66 List(1,2,3,4).sum
    67 List(1,2,3,4).take(2).sum
    67 List(1,2,3,4).take(2).sum
    68 List(1,2,3,4).drop(2).sum
    68 List(1,2,3,4).drop(2).sum
    69 List(1,2,3,4,3).indexOf(3)
    69 List(1,2,3,4,3).indexOf(3)
    70 
    70 
    71 "1,2,3,4,5".split(",").toList
    71 "1,2,3,4,5".split(",").mkString("\n")
    72 
    72 "1,2,3,4,5".split(",3,").mkString("\n")
    73 
    73 
    74 // Types
    74 // Types
    75 //=======
    75 //=======
    76 
    76 
    77 /* Scala is a strongly typed language
    77 /* Scala is a strongly typed language
    91 */
    91 */
    92 
    92 
    93 // Smart Strings
    93 // Smart Strings
    94 //===============
    94 //===============
    95 
    95 
    96 println(">\n<")
    96 println(">\n\n<")
    97 println(""">\n<""")
    97 println(""">\n<""")
       
    98 println("""">\n<"""")
    98 
    99 
    99 /* in Java
   100 /* in Java
   100 val lyrics = "Baa, Baa, Black Sheep \n" +
   101 val lyrics = "Baa, Baa, Black Sheep \n" +
   101              "Have you any wool? \n" +
   102              "Have you any wool? \n" +
   102              "Yes, sir, yes sir \n" +
   103              "Yes, sir, yes sir \n" +
   146 def square(x: Int): Int = x * x
   147 def square(x: Int): Int = x * x
   147 
   148 
   148 square(6)
   149 square(6)
   149 
   150 
   150 
   151 
       
   152 
       
   153 // The general scheme for a function: you have to give a type 
       
   154 // to each argument and a return type of the function
       
   155 //
       
   156 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
       
   157 //    body 
       
   158 //  }
       
   159 
       
   160 
       
   161 
   151 // If control structure
   162 // If control structure
   152 //======================
   163 //======================
   153 
   164 
   154 def fact(n: Int): Int = 
   165 def fact(n: Int): Int = 
   155   if (n == 0) 1 else n * fact(n - 1)
   166   if (n == 0) 1 else n * fact(n - 1)
       
   167 
       
   168 
       
   169 fact(5)
       
   170 fact(150)
   156 
   171 
   157 /* boolean operators
   172 /* boolean operators
   158  
   173  
   159    ==     equals
   174    ==     equals
   160    !      not
   175    !      not
   237 
   252 
   238 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
   253 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
   239 
   254 
   240 
   255 
   241 
   256 
   242 // with only a side-effect (no list is produced)
   257 // with only a side-effect (no list is produced),
   243 // has no "yield"
   258 // has no "yield"
   244 
   259 
   245 for (n <- (1 to 10)) println(n)
   260 for (n <- (1 to 10)) println(n)
   246 
   261 
   247 
   262 
   248 // concurrency (ONLY WORKS IN 2.11.8)
   263 // concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12.0)
   249 for (n <- (1 to 10)) println(n)
   264 for (n <- (1 to 10)) println(n)
   250 for (n <- (1 to 10).par) println(n)
   265 for (n <- (1 to 10).par) println(n)
   251 
   266 
   252 
   267 
   253 
   268 // for measuring time
   254 // for testing time
       
   255 def time_needed[T](i: Int, code: => T) = {
   269 def time_needed[T](i: Int, code: => T) = {
   256   val start = System.nanoTime()
   270   val start = System.nanoTime()
   257   for (j <- 1 to i) code
   271   for (j <- 1 to i) code
   258   val end = System.nanoTime()
   272   val end = System.nanoTime()
   259   ((end - start) / i / 1.0e9) + " secs"
   273   ((end - start) / i / 1.0e9) + " secs"
   268 // Webpages
   282 // Webpages
   269 //==========
   283 //==========
   270 
   284 
   271 import io.Source
   285 import io.Source
   272 
   286 
       
   287 // obtaining a webpage
   273 val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" 
   288 val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" 
   274 Source.fromURL(url)("ISO-8859-1").mkString
   289 Source.fromURL(url)("ISO-8859-1").mkString
   275 
   290 
   276 
   291 
   277 
   292 // function for looking up stockmarket data 
   278 def price_lookup(symbol: String): String = {
   293 def price_lookup(symbol: String): String = {
   279   val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
   294   val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
   280   Source.fromURL(url).mkString.drop(1).dropRight(2)
   295   Source.fromURL(url).mkString.drop(1).dropRight(2)
   281 }
   296 }
   282 
   297 
   290 for (s <- companies.par) println(price_lookup(s))
   305 for (s <- companies.par) println(price_lookup(s))
   291 
   306 
   292 
   307 
   293 // A Web Crawler
   308 // A Web Crawler
   294 //===============
   309 //===============
       
   310 //
       
   311 // the idea is to look for dead links
   295 
   312 
   296 import io.Source
   313 import io.Source
   297 import scala.util.matching.Regex
   314 import scala.util.matching.Regex
   298 import scala.util._
   315 import scala.util._
   299 
   316 
   328 
   345 
   329 crawl(startURL, 2)
   346 crawl(startURL, 2)
   330 
   347 
   331 
   348 
   332 
   349 
   333 
       
   334 // Adding your own methods to Strings
       
   335 //====================================
       
   336 
       
   337 // imagine you want to implement an additional
       
   338 // method to strings, like
       
   339 //
       
   340 //     "HAL".increment
       
   341 //
       
   342 // you can avoid ugly fudges, like a MyString
       
   343 // class by using implicit conversions
       
   344 
       
   345 
       
   346 implicit class MyString(s: String) {
       
   347   def increment = for (c <- s) yield (c + 1).toChar 
       
   348 }
       
   349 
       
   350 "HAL".increment
       
   351 
       
   352 
       
   353 // Further Information
   350 // Further Information
   354 //=====================
   351 //=====================
   355 
   352 
   356 // Scala download
   353 // Scala download
   357 //
   354 //