Scala 入门笔记

    name match {
      case "aaa" => println("this is aaa")
      case "bbb" => println("this is bbb")
      case _ => println("lala")

    }


// 匹配类型
case str: String => ....
case int: Int => ....
case matchType: MatchType => .... // 匹配自定义类型

  // 自定义类型
  class MatchType {
  }

匹配数组,元组,集合

val arr = Array(1, 2, 3, 6)

arr match {
  case Array(3, a, b, c) => println(s"case: $a, $b, $c")
  case Array(_, x, y, z) => println(s"case: $x, $y, $z")
  case _ => println("no matched")
}

val tup = ("a", 1, 4)

tup match {
  case ("c", b, 4) => println(s"case:  $b")
  case ("a", x, y) => println(s"case: $x, $y")
  case _ => println("no matched")
}

val list1 = List(0, 1, 2, 3)

list1 match {
    // Nil 是个空List, :: 从右向左运算 (0 :: Nil 生成一个List(0))
  case 0 :: Nil => println("case1: 0")
  case 2 :: a :: b :: c :: d :: Nil => println(s"case2: $a, $b, $c, $d")
  case 0 :: a => println(s"case3: $a")
  case List(0, a, b, c) => println(s"case4: $a, $b, $c")
  case _ => println("hahaha")
}

// 匹配样例类
object CaseClassDemo {
  def main(args: Array[String]): Unit = {
    val arr = Array(CheckTimeOutTask, SubmitTask("1000", "task-0001"), HeartBeat(3000))

    arr(Random.nextInt(arr.length)) match {
      case CheckTimeOutTask => println("CheckTimeOutTime")
      case SubmitTask(port, name) => println("SubmitTask")
      case HeartBeat(time) => println("HeartBeet")
    }
  }
}

case class HeartBeat(time: Long)
case class SubmitTask(id: String, taskName: String)
case object CheckTimeOutTask

  

模式守卫

为了让匹配更加具体,可以使用模式守卫,也就是在模式后面加上if <boolean expression>

def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = {
  notification match {
    case Email(sender, _, _) if importantPeopleInfo.contains(sender) =>
      "You got an email from special someone!"
    case SMS(number, _) if importantPeopleInfo.contains(number) =>
      "You got an SMS from special someone!"
    case other =>
      showNotification(other) // nothing special, delegate to our original showNotification function
  }
}

val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com")

val someSms = SMS("867-5309", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!")
val importantSms = SMS("867-5309", "I'm here! Where are you?")

println(showImportantNotification(someSms, importantPeopleInfo))
println(showImportantNotification(someVoiceRecording, importantPeopleInfo))
println(showImportantNotification(importantEmail, importantPeopleInfo))
println(showImportantNotification(importantSms, importantPeopleInfo))

  

def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = { notification match { caseEmail(sender, _, _) if importantPeopleInfo.contains(sender) => "You got an email from special someone!"caseSMS(number, _) if importantPeopleInfo.contains(number) => "You got an SMS from special someone!"case other => showNotification(other) // nothing special, delegate to our original showNotification function } } val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com") val someSms = SMS("867-5309", "Are you there?") val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!") val importantSms = SMS("867-5309", "I'm here! Where are you?") println(showImportantNotification(someSms, importantPeopleInfo)) println(showImportantNotification(someVoiceRecording, importantPeopleInfo)) println(showImportantNotification(importantEmail, importantPeopleInfo)) println(showImportantNotification(importantSms, importantPeopleInfo))

原文地址:https://www.cnblogs.com/sunnystone85/p/11363659.html