zoukankan      html  css  js  c++  java
  • 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("......")
      }
    }
    

      

      

  • 相关阅读:
    vector容器
    CSS3文字与字体 text-overflow 与 word-wrap
    div 居中
    C# 邮件发送
    SD详解-销售过程
    js 常用
    finereport报表--动态格间运算 二
    finereport报表--动态格间运算 一
    CSS 渐变色
    CSS3 box-shadow 属性 紧跟在 -webkit-, -ms- 或 -moz-
  • 原文地址:https://www.cnblogs.com/sunrunzhi/p/10000095.html
Copyright © 2011-2022 走看看