1.5 pattern matching

  • 519 views
Uploaded on

 

More in: Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
519
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
10
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Pattern matching
  • 2. myObject match { case 1 => println("First was hit") case 2 => println("Second was Hit") case _ => println("Unknown")}
  • 3. myObject match { case i: Int => println("Found an int") case s: String => println("Found a String") case _ => println("Unknown") }
  • 4. myObject match { case i: Int => println("Found an int") case s: String => println("Found an String") case other => println("Unknown " + other) }
  • 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. val res = myObject match { case i: Int if i == 1 => "Found an int" case s: String => "Found a String" case other => "Unknown " + other}
  • 7. val res = myObject match { case (first, second) => second case (first, second, third) => third}
  • 8. val mathedElement = list match { case List(firstElement, lastElement) => firstElement case List(firstElement, _ *) => firstElement case _ => "failed"}
  • 9. def length(list: List[_]): Int = list match { case Nil => 0 case head :: tail => 1 + length(tail) }
  • 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. Case classesClass types that can be used in pattern matchingGenerated into your class: equals hashCode toString getters (and optionally setters) ++
  • 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. 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. val regex = """(d+)(w+)""".rval myString = ...val res: String = myString match { case regex(digits, word) => digits case _ => "None"}
  • 15. val regex = """(d+)(w+)""".rval myString = ...val res: Option[String] = myString match { case regex(digit, word) => Some(digit) case _ => None}
  • 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. 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. 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