Upcoming SlideShare
×

# 1.5 pattern matching

717 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
717
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
14
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 1.5 pattern matching

1. 1. Pattern matching
2. 2. myObject match { case 1 => println("First was hit") case 2 => println("Second was Hit") case _ => println("Unknown")}
3. 3. myObject match { case i: Int => println("Found an int") case s: String => println("Found a String") case _ => println("Unknown") }
4. 4. myObject match { case i: Int => println("Found an int") case s: String => println("Found an String") case other => println("Unknown " + other) }
5. 5. myObject match { case i: Int if i == 1 => println("Found an int") case s: String => println("Found a String") case other => println("Unknown " + other) }
6. 6. val res = myObject match { case i: Int if i == 1 => "Found an int" case s: String => "Found a String" case other => "Unknown " + other}
7. 7. val res = myObject match { case (first, second) => second case (first, second, third) => third}
8. 8. val mathedElement = list match { case List(firstElement, lastElement) => firstElement case List(firstElement, _ *) => firstElement case _ => "failed"}
9. 9. def length(list: List[_]): Int = list match { case Nil => 0 case head :: tail => 1 + length(tail) }
10. 10. public static Integer getSecondOr0(List<Integer> list) { if (list != null && list.size() >= 2) { return list.get(1); } else { return 0; }}def second_or_0(list:List[Int]) = list match { case List(_, x, _*) => x case _ => 0}
11. 11. Case classesClass types that can be used in pattern matchingGenerated into your class: equals hashCode toString getters (and optionally setters) ++
12. 12. abstract class Person(name: String)case class Man(name: String) extends Person(name)case class Woman(name: String, children: List[Person]) extends Person(name)
13. 13. p match { case Man(name) => println("Man with name " + name) case Woman(name, children) => println("Woman with name " + name + " and with " + children.size + " children")}
14. 14. val regex = """(d+)(w+)""".rval myString = ...val res: String = myString match { case regex(digits, word) => digits case _ => "None"}
15. 15. val regex = """(d+)(w+)""".rval myString = ...val res: Option[String] = myString match { case regex(digit, word) => Some(digit) case _ => None}
16. 16. The Option type, never again NullPointerExceptionOption has two possible values Some(value) None val someOption: Option[String] = Some("value") val noOption: Option[String] = None
17. 17. def getValue(s: Any): Option[String]getValue(object) match { case Some(value) => println(value) case None => println("Nothing")}val result = getValue(object).getOrElse("Nothing")
18. 18. Tasks (30 min)Open the pattern-matching projectTests in package scalaexamples.patternmatchingAdd @Test to one and one methodFollow instructions in the codeMake the tests pass