Scala高手实战****第18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏
本篇文章主要讲述Scala函数式编程之偏函数,异常,及Lazy
第一部分:偏函数
偏函数:当函数有多个参数,而在使用该函数时不想提供所有参数(比如函数有3个参数),只提供0~2个参数,此时得到的函数便是偏函数。
*/
object C18 {
def main(args: Array[String]): Unit = {
val sample=1 to 10
val isEven:PartialFunction[Integer,String]={
case x if x%2==0=>x+" is even"
case _=>"other"
}
val isOdd:PartialFunction[Integer,String]={
case x if x%2==1=>x+" is odd"
}
isEven(4)
lazy val x=3
println(x)
}
}
被lazy修饰的只有在被调用的时候才会实例化
object HelloExceptionAndLazyValue {
def main(args: Array[String]): Unit = {
try{
1/0
}catch {
case ioException:IOException => println("IOException:" + ioException.toString())
case illegalArgs:IllegalArgumentException => println("IllegalArgumentException:" + illegalArgs.toString())
case arithmeticInstruction:ArithmeticInstruction =>("ArithmeticException"+arithmeticInstruction.toString())
} finally{
}
// val score = 100
lazy val score = 100
println("......"+score)
println("......")
}
}

浙公网安备 33010602011771号