Fahad/Eclipse/ScalaProjects/bin/CodeSamples/PatternMatching.sc
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 02 Nov 2014 12:24:41 +0000
changeset 38 b48939cca0cf
parent 37 d3eda1846087
permissions -rw-r--r--
slightly polished the scala file re.scala
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package CodeSamples
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object PatternMatching {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
  def matchTest(x: Any): Any = x match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
    case 1 => "one"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
    case "two" => 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
    case y: Int => "scala.Int"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
    case _ => "many"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  }                                               //> matchTest: (x: Any)Any
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  matchTest("Two")                                //> res0: Any = many
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  matchTest("Test")                               //> res1: Any = many
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
  matchTest(1)                                    //> res2: Any = one
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
  matchTest(2)                                    //> res3: Any = scala.Int
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  def matchTest2(x: Any) {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
    x match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
      case 1 => "one"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
      case "two" => 2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
      case y: Int => "scala.Int"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
      case _ => "many"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
    }
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
  }                                               //> matchTest2: (x: Any)Unit
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
  //Matching Using case Classes
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
  case class Person(name: String, age: Int)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  def caseClassTest(): Unit = {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
    val alice = new Person("Alice", 25)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
    val bob = new Person("Bob", 32)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
    val charlie = new Person("Charlie", 32)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
    for (person <- List(alice, bob, charlie)) {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
      person match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
        case Person("Alice", 25) => println("Hi Alice!")
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
        case Person("Bob", 32) => println("Hi Bob!")
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
        case Person(name, age) =>
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
          println("Age: " + age + " year, name: " + name + "?")
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
      }
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
    }
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
  }                                               //> caseClassTest: ()Unit
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
  caseClassTest()                                 //> Hi Alice!
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
                                                  //| Hi Bob!
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
                                                  //| Age: 32 year, name: Charlie?
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
}