scala中的模式匹配

一、常量、类型匹配

object MatcherDemo {

    def typeMatcher(x: Any) = x match {
        // constant patterns
        case 0 => "0: Int"
        case true => "true: Boolean"
        case "hello" => "hello: String"
        case Nil => "an empty List"
        
        // typed patterns
        case s: String => s"String: $s"
        case i: Int => s"Int: $i"
        case f: Float => s"Float: $f"
        case ai: Array[Int] => s"Array[Int]: $ai"
        case as: Array[String] => s"Array[String]: $as"
        
        case c: Cat => s"Cat { name: ${c.name}, age: ${c.age} }"
        case p: Person if p.age < 3 => s"The person is a baby. Person { name: ${p.name}, age: ${p.age} }"
        
        case l: List[_] => s"List[_]: $l"
        case m: Map[_,_] => s"Map[_,_]: $m"
        
        // default
        case _ => "Unknow"
    
    }                                         //> typeMatcher: (x: Any)String
    
    typeMatcher(0)                            //> res0: String = 0: Int
    typeMatcher(true)                         //> res1: String = true: Boolean
    typeMatcher("hello")                      //> res2: String = hello: String
    typeMatcher(List())                       //> res3: String = an empty List
    
    typeMatcher("scala")                      //> res4: String = String: scala
    typeMatcher(11)                           //> res5: String = Int: 11
    typeMatcher(3.14f)                        //> res6: String = Float: 3.14
    typeMatcher(Array(2,8))                   //> res7: String = Array[Int]: [I@45c8e616
    typeMatcher(Array("c++","scala"))         //> res8: String = Array[String]: [Ljava.lang.String;@4cdbe50f
    
    typeMatcher(Cat("Tom",1))                 //> res9: String = Cat { name: Tom, age: 1 }
    typeMatcher(Person("Misa",2))             //> res10: String = The person is a baby. Person { name: Misa, age: 2 }
    
    typeMatcher(List('s,"jk", 2.2))           //> res11: String = List[_]: List('s, jk, 2.2)
    typeMatcher(Map(1 -> 'q, 3.3 -> "ii", 's -> true))
                                                  //> res12: String = Map[_,_]: Map(1 -> 'q, 3.3 -> ii, 's -> true)
    
    typeMatcher(Person("Misa",12))            //> res13: String = Unknow
}

case class Person (name: String, age: Int)
case class Cat (name: String, age: Int)

二、 数组列表和元组匹配

1. 数组

    def arrayMatcher(a: Array[_]) = a match {
        case Array() => "an empty Array"
        case Array(_) => "an one-element Array"
        case Array(0,_) => "a two-element Array with 0 as the first element"
        case Array(true,_*) => "an Array begining with true, having any number of elements"
        case Array(x,y,_*) => x + " -- " + y
        case _ => "Unkonw"
    }                                         //> arrayMatcher: (a: Array[_])String
    
    arrayMatcher(Array())                     //> res14: String = an empty Array
    arrayMatcher(Array(3))                    //> res15: String = an one-element Array
    arrayMatcher(Array(0,4))                  //> res16: String = a two-element Array with 0 as the first element
    arrayMatcher(Array(true,2,5,6))           //> res17: String = an Array begining with true, having any number of elements
                                                  //| 
    arrayMatcher(Array("true",3,1.1))         //> res18: String = true -- 3

2. 元组

    def tupleMatcher(t: Any) = t match {
        case () => "an empty Tuple"
        case x: (_,_) => x._1 + " --- " + x._2
        case (a, b, c) => s"$a and $b and $c"
        case _ => "Unknow"
    }                                         //> tupleMatcher: (t: Any)String
    
    tupleMatcher(())                          //> res19: String = an empty Tuple
    tupleMatcher((true, 'h))                  //> res20: String = true --- 'h
    tupleMatcher(('f, 99, 2.2))               //> res21: String = 'f and 99 and 2.2
    tupleMatcher((true, 's, 11, "scala"))     //> res22: String = Unknow

3. 列表

    def listMatcher(l: Any) = l match {
        case List() =>  "an empty List"
        case x :: y :: Nil => x + " --- " + y
        case 9 :: tail => tail
        // 下面匹配的两种写法
        case x :: y => x + " +++ " + y
        case :: (x, y) => x + " === " + y
        
        case _ => "Unkonw"
    }                                         //> listMatcher: (l: Any)java.io.Serializable
    
    listMatcher(List())                       //> res23: java.io.Serializable = an empty List
    listMatcher(List('s,2))                   //> res24: java.io.Serializable = 's --- 2
    listMatcher(List(true, 'h, 99))           //> res25: java.io.Serializable = true +++ List('h, 99)
    listMatcher(List(9, "scala", 'p))         //> res26: java.io.Serializable = List(scala, 'p)
    listMatcher('k :: List())                 //> res27: java.io.Serializable = 'k +++ List()

你可能感兴趣的:(scala中的模式匹配)