Coursera Scala 4-6:模型匹配
Coursera Scala 4-6:模型匹配
匹配值
val times = 1
times match {
case 1 => "one"
case 2 => "two"
case _ => "some other number"
}
List(('a',1), ('b',2), ('a',1)) match {
case Nil => println("null")
case (C, n) :: ps => (C, n+1) :: ps 得到List(('a',2), ('b',2), ('a',1))
case p :: ps => p 得到('a',1)
case p :: ps => ps 得到List(('b',2), ('a',1))
}
}
匹配類型
def bigger(o: Any): Any = {
o match {
case i: Int if i < 0 => i - 1
case i: Int => i + 1
case d: Double if d < 0.0 => d - 0.1
case d: Double => d + 0.1
case text: String => text + "s"
}
}
匹配類成員
def calcType(calc: Calculator) = calc match {
case _ if calc.brand == "hp" && calc.model == "20B" => "financial"
case _ if calc.brand == "hp" && calc.model == "48G" => "scientific"
case _ if calc.brand == "hp" && calc.model == "30B" => "business"
case _ => "unknown"
}
樣本類Case Class
使用樣本類可以方便得存儲和匹配類的內容。你不用new關鍵字就可以創建它們
scala> case class Calculator(brand: String, model: String)
defined class Calculator
scala> val hp20b = Calculator("hp", "20b")
hp20b: Calculator = Calculator(hp,20b)
使用樣本類進行模式匹配:
e match{
case pattern => expr
}
在沒有匹配項時拋出 MatchError
Pattern可以是:
- 小寫字母開頭表示變量(保留字除外) 用於綁定值到這個變量
- 大寫開頭表示常量 兩者是否相等來進行匹配(==)
構造器pattern C(p1,....,pn) 匹配所有C類型(或子類型)通過p1...p構造的
(注意類的定義要用case class)
同樣的變量隻能在Pattern出現一次
順序匹配下去
trait Expr
case class Number(n:Int) extends Expr
case class Sum(e1:Expr,e2:Expr) extends Expr
object exprs {
def show(e:Expr): String = e match {
case Number(x) => x.toString
case Sum(l.r) => show(l)+"+"+show(r)
}
}
show(Sum(Number(1),Number(44))) > res0:String=1+44
例子:
trait Expr {
def isNumber: Boolean
def isSum: Boolean
def numValue: Int
def leftOp: Expr
def rightOp: Expr
def show():String = this match{
case Number(n) => n+""
case Sum(e1,e2) => e1+"+"+e2
}
}
另一個例子:
Scala代碼
def show(e: Expr): String = e match {
case Sum(Prod(x1, x2), Prod(x3, x4)) =>
if (x1 == x3) show(x1) + "*" + "(" + show(x2) + "+" + show(x4) + ")"
else if (x1 == x4) show(x1) + "*" + "(" + show(x2) + "+" + show(x3) + ")"
else if (x2 == x3) show(x2) + "*" + "(" + show(x1) + "+" + show(x4) + ")"
else if (x2 == x4) show(x2) + "*" + "(" + show(x1) + "+" + show(x3) + ")"
else show(_Prod(x1, x2)) + "+" + show(_Prod(x3, x4))
case Sum(n1, n2) => show(n1) + "+" + show(n2)
case Prod(Sum(n1, n2), n3) => "(" + show(_Sum(n1, n2)) + ")" + "*" + show(n3)
case Prod(n3, Sum(n1, n2)) => show(n3) + "*(" + show(_Sum(n1, n2)) + ")"
case Prod(n1, n2) => show(n1) + "*" + show(n2)
case _ => e.toString()
}
Reference
https://twitter.github.io/scala_school/zh_cn/basics2.html
https://hongjiang.info/scala-pattern-matching-1/
最後更新:2017-04-03 05:39:17