progs/lecture2.scala
changeset 173 2de1f79dedf0
parent 167 349d706586ef
child 174 90e0b1cc460b
equal deleted inserted replaced
172:fb229ff17409 173:2de1f79dedf0
   174 
   174 
   175 List(5,6,7,8,9).indexOf(42)
   175 List(5,6,7,8,9).indexOf(42)
   176 
   176 
   177 
   177 
   178 // ... how are we supposed to know that this returns -1
   178 // ... how are we supposed to know that this returns -1
       
   179 
       
   180 
       
   181 //other example for options...NaN
       
   182 val squareRoot: PartialFunction[Double, Double] = { 
       
   183     case d: Double if d > 0 => Math.sqrt(d) 
       
   184 }
       
   185 
       
   186 val list: List[Double] = List(4, 16, 25, -9)
       
   187 
       
   188 val result = list.map(Math.sqrt)
       
   189 // => result: List[Double] = List(2.0, 4.0, 5.0, NaN)
       
   190 
       
   191 val result = list.collect(squareRoot)
       
   192 // => result: List[Double] = List(2.0, 4.0, 5.0)
   179 
   193 
   180 
   194 
   181 // Higher-Order Functions
   195 // Higher-Order Functions
   182 //========================
   196 //========================
   183 
   197