Scala教程(十一)Curry详解与模式匹配
1 curry函数
1.1 curry化的函数
curry化的函数被应用了多个参数列表,而不是仅仅一个。
// 传统函数
def curringSumOld(x:Int,y:Int) = x * y;
// curring化的函数被应用了多个参数列表
def curringSum(x:Int)(y:Int) = x + y;
println(curringSum(10)(15));
输出结果: 25
1.2 curry占位符
用偏应用函数表达式方式,把占位符标注用在curriedSum里。
// 用偏应用函数表达式方式,把占位符标注用在curriedSum里
val onePlus = curringSum(1)_
println(onePlus(12));
输出结果:
131.3 curry对比
// 数组对比
val a = Array("hello","scala");
val b = Array("HELLO","SCALA");
// 忽略大小写对比
println(a.corresponds(b)(_.equalsIgnoreCase(_)));
输出结果:
true2 模式匹配case class
2.1 match表达式
// 正则匹配
val pattern = "([0-9]+) ([a-z]+)".r;
"741258933 hadoop" match {
case pattern(num,item) => println(num+":"+item)
}
输出结果:741258933:hadoop
2.2 数组匹配
def match_array(arr:Any) = arr match{
case Array(0) => println("Array:0" )
case Array(x,y) => println("Array:x="+x+",y="+y )
case Array(0,_*) => println("Array:..." )
case _ => println("something else" )
}
match_array(Array(0))
match_array(Array(0,1))
match_array(Array(0,1,2,3,4,5))
match_array(Array("one","two","three"))
输出结果:
Array:0
Array:x=0,y=1
Array:...
somethingelse
2.3 case class匹配
Scala的case class使得对对象进行模式匹配变得非常方便,简单的来说,Scala的case class就是在普通的类定义前加case这个关键字,然后你可以对这些类来模式匹配。
abstract class Person
case class Student(age:Int) extends Person
case class Worker(age:Int,salary:Double) extends Person
case object Shared extends Person
/**
* 1、声明case class 每个成员都会默认生成val,如age:Int
* 2、每个case class 都会有伴生对象,每个伴生对象都会有自己case class的具体对象
* 3、模式匹配的时候,可以从case class提取内容,提取方法从apply中。
*/
object case_class_object {
def main(args: Array[String]): Unit = {
def caseOps(person:Person) = person match{
case Student(age) => println("I am " + age + " years old");
case Worker(_,salary) => println("Wow,I got "+salary)
case Shared => println("no property...");
}
caseOps(Student(19))
caseOps(Worker(19,4000))
caseOps(Shared)
val worker = Worker(29,3000);
val worker2 = worker.copy(salary = 5000);
val worker3 = worker.copy(age = 31);
}
}
输出结果:
Iam 19 years old
Wow,Igot 4000.0
noproperty...
2.4 嵌套匹配
abstract class Item
case class Book(description: String, price: Double) extends Item
case class Bundle(description: String, price: Double, items: Item*) extends Item
object pattern_match_case_class_nested {
def main(args: Array[String]): Unit = {
def case_class_nested(person: Item) = person match {
// 匹配Bundle类型,嵌套Book类型,Item类型可以0到N个参数
case Bundle(_, _, art @ Book(_, _), rest @ _*) => println(art.description + " : " + art.price)
case Bundle(_, _, Book(descr, _), _*) => println("The first description is : " + descr)
case _ => println("Oops!!!")
}
// 输出结果:Scala for the Spark Developer : 69.95
case_class_nested(Bundle("1111 Special's", 30.0,
Book("Scala for the Spark Developer", 69.95),
Book("Hadoop in Action", 69.95),
Book("Hive", 79.95),
Book("HBase", 32.86)))
}
}
输出结果:Scala forthe Spark Developer : 69.95
2.5 Option匹配
Options语法:sealed关键字表示:封闭的、密封的、
sealed约束:
其修饰的trait,class只能在当前文件里面被继承。
scala编译器在检查模式匹配的时候, scala就能够在编译时进行检查是否所 有的选项已经列在case中,减少编程的错误。
sealed abstract classOption[+A] extends Product withSerializable {
Options有两个字类,分别让车Some、None
Some中如果有具体的值,则用Some表示。反之,则用None表示。
代码示例:
def main(args: Array[String]): Unit = {
// 声明数组
val scores = Map("Alice" -> 99 , "Spark" -> 100)
// 模式匹配
scores.get("Alice") match {
// Option子类Some,有具体的值,则之输出。
case Some(score) => println(score)
case None => println("No score")
}
}
输出结果:99
--以上为Curry详解与模式匹配内容,谢谢大家对我的关注。
——厚积薄发(yuanxw)