Fahad/Eclipse/ScalaProjects/src/CodeSamples/PatternMatching.sc
author Chengsong
Mon, 09 May 2022 17:20:55 +0100
changeset 512 a4b86ced5c32
parent 36 d205c05e13d6
permissions -rw-r--r--
rewrite rules modified slightly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package CodeSamples
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object PatternMatching {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
  def matchTest(x: Any): Any = x match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
    case 1 => "one"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
    case "two" => 2
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
    case y: Int => "scala.Int"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
    case _ => "many"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  }                                               //> matchTest: (x: Any)Any
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  matchTest("Two")                                //> res0: Any = many
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  matchTest("Test")                               //> res1: Any = many
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
  matchTest(1)                                    //> res2: Any = one
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
  matchTest(2)                                    //> res3: Any = scala.Int
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  def matchTest2(x: Any) {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
    x match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
      case 1 => "one"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
      case "two" => 2
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
      case y: Int => "scala.Int"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
      case _ => "many"
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
  }                                               //> matchTest2: (x: Any)Unit
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
  //Matching Using case Classes
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
  case class Person(name: String, age: Int)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  def caseClassTest(): Unit = {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
    val alice = new Person("Alice", 25)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
    val bob = new Person("Bob", 32)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
    val charlie = new Person("Charlie", 32)
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    for (person <- List(alice, bob, charlie)) {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
      person match {
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
        case Person("Alice", 25) => println("Hi Alice!")
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
        case Person("Bob", 32) => println("Hi Bob!")
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
        case Person(name, age) =>
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
          println("Age: " + age + " year, name: " + name + "?")
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
      }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
    }
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
  }                                               //> caseClassTest: ()Unit
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
  caseClassTest()                                 //> Hi Alice!
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
                                                  //| Hi Bob!
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
                                                  //| Age: 32 year, name: Charlie?
d205c05e13d6 Handout 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
}