progs/lecture2.scala
changeset 173 2de1f79dedf0
parent 167 349d706586ef
child 174 90e0b1cc460b
--- a/progs/lecture2.scala	Thu Mar 08 12:33:09 2018 +0000
+++ b/progs/lecture2.scala	Mon Mar 12 12:11:50 2018 +0000
@@ -178,6 +178,20 @@
 // ... how are we supposed to know that this returns -1
 
 
+//other example for options...NaN
+val squareRoot: PartialFunction[Double, Double] = { 
+    case d: Double if d > 0 => Math.sqrt(d) 
+}
+
+val list: List[Double] = List(4, 16, 25, -9)
+
+val result = list.map(Math.sqrt)
+// => result: List[Double] = List(2.0, 4.0, 5.0, NaN)
+
+val result = list.collect(squareRoot)
+// => result: List[Double] = List(2.0, 4.0, 5.0)
+
+
 // Higher-Order Functions
 //========================