val (one, two) = (1, 2)
one //res0: Int = 1
two //res1: Int = 2
def sellerAndItemId(orderId: Int): (Int, Int) =
orderId match {
case 0 => (1, 2)
}
val (sellerId, itemId) = sellerAndItemId(0)
sellerId // sellerId: Int = 1
itemId // itemId: Int = 2
val sellerItem = sellerAndItemId(0)
sellerItem._1 //res4: Int = 1
sellerItem._2 //res5: Int = 2
val sampleList = List((1, 2, 3), (4, 5, 6), (7, 8, 9))
sampleList.map(x => s"${x._1}_${x._2}_${x._3}")
//res0: List[String] = List(1_2_3, 4_5_6, 7_8_9)
sampleList.map {
case (orderId, shopId, itemId) =>
s"${orderId}_${shopId}_$itemId"
}
//res1: List[String] = List(1_2_3, 4_5_6, 7_8_9)
上下两个map做了同样的事情,但下一个map为tuple中的三个值都给了名字,增加了代码的可读性.
//case如果是常量,就在值相等时匹配.
//如果是变量,就匹配任何值.
def describe(x: Any) = x match {
case 5 => "five"
case true => "truth"
case "hello" => "hi!"
case Nil => "the empty list"
case somethingElse => "something else " + somethingElse
}
case class,tuple以及列表都可以在匹配的同时捕获内部的内容.
case class Sample(a:String,b:String,c:String,d:String,e:String)
def showContent(x: Any) =
x match {
case Sample(a,b,c,d,e) =>
s"Sample $a.$b.$c.$d.$e"
case (a,b,c,d,e) =>
s"tuple $a,$b,$c,$d,$e"
case head::second::rest =>
s"list head:$head second:$second rest:$rest"
}