progs/lecture2.scala
changeset 148 ead6089209ba
parent 147 72f7dd1a3754
child 150 9a2f2a1de42b
equal deleted inserted replaced
147:72f7dd1a3754 148:ead6089209ba
     1 // Scala Lecture 2
     1 // Scala Lecture 2
     2 //=================
     2 //=================
     3 
     3 
     4 
     4 
     5 // Overloaded math operations
     5 // the pain with overloaded math operations
     6 
     6 
     7 (100 / 4)
     7 (100 / 4)
     8 
     8 
     9 (100 / 3)
     9 (100 / 3)
    10 
    10 
   263 // f1: (Int, Int) => Int
   263 // f1: (Int, Int) => Int
   264 // f2: List[String] => Option[Int]
   264 // f2: List[String] => Option[Int]
   265 // ... 
   265 // ... 
   266 
   266 
   267 
   267 
       
   268 // an aside: partial application
       
   269 
       
   270 def add(a: Int)(b: Int) : Int = a + b
       
   271 
       
   272 sum(add(2), 0, 2)
       
   273 sum(add(10), 0, 2)
       
   274 
       
   275 def add2(a: Int, b: Int) : Int = a + b
       
   276 sum(x => add2(2, x), 0, 2)
       
   277 sum(x => add2(10, x), 0, 2)
       
   278 
   268 // Function Composition
   279 // Function Composition
   269 //======================
   280 //======================
   270 
   281 
   271 // How could Higher-Order Functions and Options be helpful?
   282 // How could Higher-Order Functions and Options be helpful?
   272 
   283 
   309 better_first_word("").map(duplicate)
   320 better_first_word("").map(duplicate)
   310 better_first_word("").map(duplicate).map(valid_msg)
   321 better_first_word("").map(duplicate).map(valid_msg)
   311 
   322 
   312 
   323 
   313 
   324 
   314 
       
   315 // Type abbreviations
       
   316 //====================
       
   317 
       
   318 // some syntactic convenience
       
   319 
       
   320 type Pos = (int, Int)
       
   321 type Board = List[List[Int]]
       
   322 
   325 
   323 
   326 
   324 
   327 
   325 // Implicits (Cool Feature)
   328 // Implicits (Cool Feature)
   326 //=========================
   329 //=========================
   527 val B = (1 to 1000000 by 4).toSet
   530 val B = (1 to 1000000 by 4).toSet
   528 
   531 
   529 time_needed(10, count_intersection(A, B))
   532 time_needed(10, count_intersection(A, B))
   530 time_needed(10, count_intersection2(A, B))
   533 time_needed(10, count_intersection2(A, B))
   531 
   534 
       
   535 
       
   536 // Type abbreviations
       
   537 //====================
       
   538 
       
   539 // some syntactic convenience
       
   540 
       
   541 type Pos = (int, Int)
       
   542 type Board = List[List[Int]]
   532 
   543 
   533 
   544 
   534 
   545 
   535 
   546 
   536 // Sudoku
   547 // Sudoku