scala notes (5) - pattern and case class

- Pattern and Case Class

	
ch match{
	case _ if Character.isDigit(ch) => ..
	case '+' => ...
	case _ => ...
}
prefix match {
	case "0" | "0x" | "0X" => ...
}

  • case variable should be lowercase. use `` to enclose a expression
str match{
	case `pathSeparator` => ..
	case pathSeparator =>
}

  • with type
obj match {
	case x: Int => x
	case s: String => Integer.parseInt(s)
	case _: BigInt => Int.MaxValue
	case BigInt => 0 //is to match BigInt object
}

  • match array, list and tuple 
arr match {
	case Array(0) => "0"
	case Array(x, y) => s"$x $y"
	case Array(0, rest @ _*) => "0 ..."
	case _ => "something else"
}
lst match {
	case 0 :: Nil => "0"
	case x :: y :: Nil => s"$x $y"
	case 0 :: tail => "0 ..."
	case _ => "something else"
}

pair match {
	case (0, _) => "0 ..."
	case (y, 0) => s"$y 0"
	case _ => "neither is 0"
}

  • pattern object can be used in case statement to extract groups
val pattern = "([0-9]+) ([a-z]+)".r
"99 bottles" match {
	case pattern(num, letter) => ... //pattern.unapplySeq("...")
}
val (x,y) = (1,2)
val (q, r) = BigInt(10) /& 3
val Array(first, second, rest @ _*) = arr
val Array(E, x) =arr // E is constant

val $result = x match { case 2 => () } // Unit, () is sole value of Unit

  • pattern in for expression
for ((k, "") <- System.getProperties())
	println(k)
for ((k, v) <- System.getProperties() if v == "")
	println(k)

  • case class
abstract class Amount
case class Dollar(value: Double) extends Amount
case class Currency(value: Double, unit: String) extends Amount
case object Nothing extends Amount //case object is also supported, no () after Nothing


constructor  parameter becomes val

apply, unapply, toString, equals, hashCode and copy methods auto added

  • match nested structures

abstract class Item
case class Article(description: String, price: Double) extends Item
case class Bundle(description: String, discount: Double, items: Item*) extends Item //variable parameters need to be last one

case Bundle(_, _, art @ Article(_, _), rest @ _*) => ...

def price(it: Item): Double = it match {
	case Article(_, p) => p
	case Bundle(_, disc, its @ _*) => its.map(price _).sum - disc
}

case classes are suitable for fixed structure.

  • sealed case classes
object SealedCase extends App{
  val t: Test = SubTest("xyz") //t need to be top-level type, Test. otherwise, compiler doesn't check completeness of pattern match
  t match{
    case SubTest(x) => println(x)
  }
}

sealed abstract class Test
case class SubTest(value: String) extends Test
case class SubTest2(value: Int) extends Test


  • simulating enumeration (case object)
sealed abstract class TrafficLightColor
case object Red extends TrafficLightColor
case object Yellow extends TrafficLightColor
case object Green extends TrafficLightColor
color match {
	case Red => "stop"
	case Yellow => "hurry up"
	case Green => "go"
}

  • partial functions

PartialFunction[A, B] -> apply and isDefinedAt

val f: PartialFunction[Char, Int] = { case '+' => 1 ; case '-' => -1 }// f('-') -1, f('0') MatchError, f.isDefinedAt('0') false
"-3+4".collect { case '+' => 1 ; case '-' => -1 } // Vector(-1, 1)

Seq[A] and Map[k,v] are PartialFunction

exhaustive set of case clauses defines a Function1, not Partial Function


val f: PartialFunction[Char, Int] = { case '+' => 1 ; case '-' => -1 } //partial function
val g = f.lift // A function with type Char => Option[Int] //lift turns partial function to regular function with return type Option

function.unlift -> partial function


catch clause is partial function.

def tryCatch[T](b: => T, catcher: PartialFunction[Throwable, T]) =
	try { b } catch catcher






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章