美文网首页
Scala模式匹配&样例类&偏函数

Scala模式匹配&样例类&偏函数

作者: hipeer | 来源:发表于2018-10-06 18:34 被阅读0次

    模式匹配

    • match不是语句,而是一个表达式,拥有返回值。在match表达式中可以使用任何类型

      val sign = ch match {
        case '+' => 1
        case '*' => 2
        case _ => 0
      }
      
      // 给模式添加守卫,只匹配数字
      val sign = ch match {
        case _ if Character.isDigit(ch) => Character.digit(ch, 10)
      }
      
      // 匹配数组
      val arr = Array(1, 2, 3, 4, 5, 6)
      val result = arr match {
        case Array(1, 2, 3, 4, 5, _) => 1
        case Array(1, 2, _*) => 2
        case _ => 0
      }
      注: _ 表示任意一个元素,_* 表示任意长度的元素
      
      // 类型匹配
      def getType(obj: Any): Unit = {
        obj match {
          case _: Array[String] => println("Array[String]")
          case _: String => println("String")
          case _: Int => println("Int")
          case _: BigInt => println("BigInt")
          case _ => println("unkown")
        }
      }
      
      // 匹配列表
      val result = list match {
        case a :: b :: tail => a + " " + b
        case a :: tail => a
        case _ => "none"
      }
      
      // 匹配元组
      val result = tuple match {
        case (a, _) => a
        case _ => "none"
      }
      注:匹配元组是无法使用 _*
      

      注意:泛型的类型匹配要注意如List[String]、Map[Char,Int]等不会成功匹配,如List[Int]等亦可匹配,因而往往使用通配符List[ _ ]进行匹配,但Array[Int]是可行的

    样例类

    • 样例类是一种特殊的类,他们经过优化以被用于模式匹配。
    abstract class Solution
    case class Add(a: Int, b: Int) extends Solution
    case class Mul(a: Int, b: Int) extends Solution
    // 还可以有单例的样例对象
    case object Nothing extends Solution
    
    def getType(obj: Solution): Any = {
      obj match {
        case Add(a, b) => a + b
        case Mul(a, b) => a * b
        case Nothing => "Nothing"
        case _ => "Null"
      }
    }
    
    // 用法
    val add = Add(1, 1)
    val mul = Mul(10, 10)
    val result = getType(add)  // 2
    val result1 = getType(mul) // 100
    
    // 可以把模式匹配直接放到公共超类中
    abstract class Solution{
      def getType: Any = {
        this match {
          case Add(a, b) => a + b
          case Mul(a, b) => a * b
          case Nothing => "Nothing"
          case _ => "none"
        }
      }
    }
    
    // 用法
    add.getType
    mul.getType
    
    

    注意: 样例类的实例使用(), 样例对象不使用()。

    当声明一个样例类时会自动发生下面几件事:
    • 构造器中的么一个参数都会成为val字段,除非显式的设为var
    • 在伴生对象中提供apply方法,可以不用关键字new就能构造相应对象
    • 提供unapply方法让模式匹配可以工作
    • 生成toString,equals, hashCode,copy方法
    密封类
    • 模式匹配完成后需要确保所有的情况都被考虑,要达到这个目的,需要将样例类的公共超类声明为sealed
    sealed abstract class Solution
    case class Add(a: Int, b: Int) extends Solution
    case class Mul(a: Int, b: Int) extends Solution
    
    • 密封类的所有子类必须都在该密封类所在的文件中定义。
    • 如果一个类是密封的,那么在编译期所有的子类就是可知的,因而编译器可以检查模式语句的完整性。
    模拟枚举
    • 在Scala中,样例类可以模拟枚举类型

      sealed abstract class Color
      // 使用单例样例对象
      case object Red extends Color
      case object Blue extends Color
      case object Green extends Color
      
      def getColor(obj: Color): String = {
        obj match {
          case Red => "Red"
          case Blue => "Blue"
          case Green => "Green"
        }
      }
      
      // 用法
      getColor(Red)
      或
      val color = Red
      getColor(color)
      

    偏函数

    • 被包在花括号内的一组case语句是一个偏函数——一个并非对所有输入值都有定义的函数。它是PartialFunction[A, B]类的一个实例。(A是参数类型, B是返回类型)。
    • PartialFunction[A, B] 类有两个方法:apply方法从匹配到的模式计算函数值,而isDefinedAt方法在输入至少匹配其中一个模式时返回true
    • 如果把函数用到其不支持的值时会报错
    // 定义
    val fun: PartialFunction[Int, String] = { case 1 => "yes"; case 0 => "no" }
    
    // 用法
    fun(1)          // yes
    fun(0)          // no
    fun.isDefineAt(1)   // true
    fun.isDefineAt(3)   // false
    fun(3)          // 报错
    
    • GenTraversable特质的collect方法将一个偏函数应用到所有在该偏函数有定义的元素,并返回包含这些结果的序列
    "1+2*3-4" collect { case '+' => 1; case '*' => 2; case '-' => 1 }   // Vector(1, 2, 1)
    
    • Actor中的例子
    react {
      case (name: String, actor: Actor) => {
        actor ! getip(name)
        act()
      }
      case msg => {
        println("UnHandler message" + msg)
        act()
      }
    }
    

    注意:偏函数表达式必须位于编译器可以推断出返回类型的上下文中。把它赋值给一个带有类型声明的变量,或者将它作为参数传递都是可以的。

    相关文章

      网友评论

          本文标题:Scala模式匹配&样例类&偏函数

          本文链接:https://www.haomeiwen.com/subject/ejihaftx.html