Example : Pattern matching on Tuples, Using Guards on Case Clause

val subjects = Seq(
                    ("physics", "1stTerm", 85),
                    ("physics", "2ndTerm", 90),
                    ("math", "2ndTerm", 80),
                    ("english", "1stTerm", 82)
                   )
val token = "physics"
for (x <- subjects) {
    val result = x match {
        // A Case clause with Guard(ie if m == "1stTerm")
        case (`token`, m, n) if m=="1stTerm" => s"Physics 1st Term : $x"
        case (_, _, _) => s"Other Subjects : $x"
    }
    println(result)
}

No comments:

Post a Comment