progs/lecture2.scala
changeset 278 0c2481cd8b1c
parent 268 e43f7e92ba26
child 309 b192bc772613
--- a/progs/lecture2.scala	Fri Aug 16 08:45:21 2019 +0100
+++ b/progs/lecture2.scala	Tue Oct 29 09:54:52 2019 +0000
@@ -513,6 +513,18 @@
 println(people.sortWith(superior).mkString("\n"))
 
 
+// String interpolations as patterns
+
+val date = "2000-01-01"
+val s"$year-$month-$day" = date
+
+def parse_date(date: String) = date match {
+  case s"$year-$month-$day" => Some((year.toInt, month.toInt, day.toInt))
+  case s"$day/$month/$year" => Some((year.toInt, month.toInt, day.toInt))
+  case _ => None
+} 
+
+
 // Tail Recursion
 //================
 
@@ -723,3 +735,18 @@
 
 
 
+// if you like verbosity, you can full-specify the literal. 
+// Don't go telling that to people, though
+(1 to 100).filter((x: Int) => x % 2 == 0).sum 
+
+// As x is known to be an Int anyway, you can omit that part
+(1 to 100).filter(x => x % 2 == 0).sum
+
+// As each parameter (only x in this case) is passed only once
+// you can use the wizardy placeholder syntax
+(1 to 100).filter(_ % 2 == 0).sum
+
+// But if you want to re-use your literal, you can also put it in a value
+// In this case, explicit types are required because there's nothing to infer from
+val isEven: (x: Int) => x % 2 == 0
+(1 to 100).filter(isEven).sum