# 不同個數的元素提取

unapply()方法可以僅提取一個結果，例如：
``object Address {    def unapply(str: String): Option[String] = {        val data = str.split(",")        if (data.length == 3) Some(data(2)) else None    }}val Address(addr) = "B123456,Justin,Kaohsiung"  println(addr)           // Kaohsiung``

``object Student {    def unapply(str: String) = {        val data = str.split(",")        if (data.length == 3) Some(data(0), data(1), data(2)) else None    }}object Kaohsiung {    def unapply(str: String): Boolean = str == "Kaohsiung"}val students = List(                   "B123456,Justin,Kaohsiung",                   "B98765,Monica,Kaohsiung",                   "B246819,Bush,Taipei"               )               students.foreach(_ match {    case Student(_, name, addr @ Kaohsiung()) => println(name)    case _ =>})``

``object Uppercase {    def unapplySeq(s: String): Option[Seq[Char]] = {        Some(for(c <- s.toArray if c.isUpperCase) yield c)    }}val Uppercase(u1 @ _*) = "This is Justin"u1.foreach(print)   // TLval Uppercase(u2 @ _*) = "'JL' stands for Justin Lin"u2.foreach(print)   // TJJLJL ``

``val strs = Array(              "Scala is Java?",               "Java is Scala?",              "Java 7 will include closure!",              "Scala already has closure.")strs.foreach(_ match {    case Uppercase('J')       => println("only 'J' mentioned")    case Uppercase('J', 'S')  => println("'JS' mentioned")    case other                => println("other: " + other)})``

unapplySeq()方法也可以選擇性的傳回固定個數的元素之後，再提供未定個數的元素部份，例如下例找出使用者名稱為"caterpillar"的郵件，取得其網域切割後的字串陣列：
``object Email {    def unapplySeq(s: String): Option[(String, Seq[String])] = {        val parts = s.split("@")        if(parts.length == 2) Some((parts(0), parts(1).split("\\.")))        else None    }}val strs = Array(              "caterpillar@gmail.com",               "caterpillar@openhome.cc",              "Justin@openhome.cc")strs.foreach(_ match {    case Email("caterpillar", domain @ _*) => println(domain)    case _ =>})``