Scala 模式匹配
Scala 模式匹配有多种形式:
基本模式匹配:基本模式匹配是最基本的模式匹配形式,它可以在 match 块中匹配任意值。
常量模式匹配:常量模式匹配允许您匹配常量,例如字符串和数字。
变量模式匹配:变量模式匹配允许您匹配任意值,并将其赋值给变量。
类型模式匹配:类型模式匹配允许您匹配特定类型的值。
结构体模式匹配:结构体模式匹配允许您匹配具有特定结构的值,例如列表和元组。
序列模式匹配:序列模式匹配允许您匹配序列,例如列表和数组。
多重模式匹配:多重模式匹配允许您同时匹配多个条件。
Spark 偏函数
偏函数表示用{}包含用case进行类型匹配的操作,这种操作一般用于匹配唯一的属性值,在Spark中的算子内经常会遇到。例如:
@Test
偏函数
def case1() = {
val list = List(false, true)
//偏函数
list.foreach {
case false => println(1)
case true => println(0)
case _ => println(100)
}
}
示例
- 值匹配
@Test
//值匹配
def case2() = {
val list = List(false, true)
for (l <- list) {
l match {
case false => println(1)
case true => println(0)
case _ => println(100)
}
}
}
- 类型匹配
@Test
//类型匹配
def case3() = {
val list: List[Any] = List(23, "hello", 8.5, false)
for (l <- list) {
l match {
case i: Int => println(i) // 23
case d: Double => println(d) //8.5
case s: String => println(s)
case other => println(other) // false
}
}
}
- 按照顺序匹配
@Test
//按照顺序匹配
def case4() = {
val l1: List[Int] = List(1, 2, 3, 4)
val l2: List[Int] = List(4, 8, 12)
val empty: List[Nothing] = List()
for (l <- List(l1, l2, empty)) {
l match {
case List(_, _, 3, _) => println("集合4个元素,第三个元素是3. " + l.toString())
case List(_*) => println("集合有0个或者更多元素 " + l.toString())
}
}
}
/*
集合4个元素,第三个元素是3. List(1, 2, 3, 4)
集合有0个或者更多元素 List(4, 8, 12)
集合有0个或者更多元素 List()
*/
- case里面用 guard 的数组匹配:
@Test
//case里面用 guard 的数组匹配:
def case5() = {
val tupA = ("Good", "Morning")
val tupB = ("How", "What")
for (tup <- List(tupA, tupB)) {
tup match {
case (a, b) if a == "Good" => println("输出的是第一个元素是Good的Tuple:" + tup)
case (a, b) => println("输出的是其他Tuple " + tup)
}
}
}
/*
输出的是第一个元素是Good的Tuple:(Good,Morning)
输出的是其他Tuple (How,What)
*/
- 对象深度匹配
@Test
//对象深度匹配
case class Person(name: String, age: Int)
def case6() = {
val xm: Person = new Person("xiaoming", 18)
val bob: Person = new Person("bob", 22)
val peter: Person = new Person("peter", 30)
for (p <- List(xm, bob, peter)) {
p match {
case Person("xiaoming", 18) => println("Hi xm")
case Person("bob", 22) => println("Hi bob")
case Person(name, age) => println("who are you " + name + " ,age:" + age)
}
}
}
/*
Hi xm
Hi bob
who are you peter ,age:30
*/
- 正则表达式匹配
@Test
//正则表达式匹配
def case7() = {
val BookExtractorRE: Regex = """Book: title=([^,]+),\s+authors=(.+)""".r
val MagazineExtractorRE: Regex = """Magazine: title=([^,]+),\s+issue=(.+)""".r
val catalog: List[String] = List(
"Book: title=Programming Scala, authors=Dean Wampler, Alex Payne", "Magazine: title=The New Yorker, ", "issue=January 2009",
"Book: title=War and Peace, authors=Leo Tolstoy", "Magazine: title=The Atlantic, issue=February 2009",
"BadData: text=Who put this here??")
for (item <- catalog) {
item match {
case BookExtractorRE(title, authors) => println("Book \"" + title + "\", written by " + authors)
case MagazineExtractorRE(title, issue) => println("Magazine \"" + title + "\", issue " + issue)
case entry => println("Unrecognized entry: " + entry)
}
}
}
/*
Book "Programming Scala", written by Dean Wampler, Alex Payne
Unrecognized entry: Magazine: title=The New Yorker,
Unrecognized entry: issue=January 2009
Book "War and Peace", written by Leo Tolstoy
Magazine "The Atlantic", issue February 2009
Unrecognized entry: BadData: text=Who put this here??
*/
}