美文网首页
scala中的模式匹配

scala中的模式匹配

作者: _fatef | 来源:发表于2018-10-17 17:24 被阅读0次

一、常量、类型匹配

object MatcherDemo {

    def typeMatcher(x: Any) = x match {
        // constant patterns
        case 0 => "0: Int"
        case true => "true: Boolean"
        case "hello" => "hello: String"
        case Nil => "an empty List"
        
        // typed patterns
        case s: String => s"String: $s"
        case i: Int => s"Int: $i"
        case f: Float => s"Float: $f"
        case ai: Array[Int] => s"Array[Int]: $ai"
        case as: Array[String] => s"Array[String]: $as"
        
        case c: Cat => s"Cat { name: ${c.name}, age: ${c.age} }"
        case p: Person if p.age < 3 => s"The person is a baby. Person { name: ${p.name}, age: ${p.age} }"
        
        case l: List[_] => s"List[_]: $l"
        case m: Map[_,_] => s"Map[_,_]: $m"
        
        // default
        case _ => "Unknow"
    
    }                                         //> typeMatcher: (x: Any)String
    
    typeMatcher(0)                            //> res0: String = 0: Int
    typeMatcher(true)                         //> res1: String = true: Boolean
    typeMatcher("hello")                      //> res2: String = hello: String
    typeMatcher(List())                       //> res3: String = an empty List
    
    typeMatcher("scala")                      //> res4: String = String: scala
    typeMatcher(11)                           //> res5: String = Int: 11
    typeMatcher(3.14f)                        //> res6: String = Float: 3.14
    typeMatcher(Array(2,8))                   //> res7: String = Array[Int]: [I@45c8e616
    typeMatcher(Array("c++","scala"))         //> res8: String = Array[String]: [Ljava.lang.String;@4cdbe50f
    
    typeMatcher(Cat("Tom",1))                 //> res9: String = Cat { name: Tom, age: 1 }
    typeMatcher(Person("Misa",2))             //> res10: String = The person is a baby. Person { name: Misa, age: 2 }
    
    typeMatcher(List('s,"jk", 2.2))           //> res11: String = List[_]: List('s, jk, 2.2)
    typeMatcher(Map(1 -> 'q, 3.3 -> "ii", 's -> true))
                                                  //> res12: String = Map[_,_]: Map(1 -> 'q, 3.3 -> ii, 's -> true)
    
    typeMatcher(Person("Misa",12))            //> res13: String = Unknow
}

case class Person (name: String, age: Int)
case class Cat (name: String, age: Int)

二、 数组列表和元组匹配

1. 数组

    def arrayMatcher(a: Array[_]) = a match {
        case Array() => "an empty Array"
        case Array(_) => "an one-element Array"
        case Array(0,_) => "a two-element Array with 0 as the first element"
        case Array(true,_*) => "an Array begining with true, having any number of elements"
        case Array(x,y,_*) => x + " -- " + y
        case _ => "Unkonw"
    }                                         //> arrayMatcher: (a: Array[_])String
    
    arrayMatcher(Array())                     //> res14: String = an empty Array
    arrayMatcher(Array(3))                    //> res15: String = an one-element Array
    arrayMatcher(Array(0,4))                  //> res16: String = a two-element Array with 0 as the first element
    arrayMatcher(Array(true,2,5,6))           //> res17: String = an Array begining with true, having any number of elements
                                                  //| 
    arrayMatcher(Array("true",3,1.1))         //> res18: String = true -- 3

2. 元组

    def tupleMatcher(t: Any) = t match {
        case () => "an empty Tuple"
        case x: (_,_) => x._1 + " --- " + x._2
        case (a, b, c) => s"$a and $b and $c"
        case _ => "Unknow"
    }                                         //> tupleMatcher: (t: Any)String
    
    tupleMatcher(())                          //> res19: String = an empty Tuple
    tupleMatcher((true, 'h))                  //> res20: String = true --- 'h
    tupleMatcher(('f, 99, 2.2))               //> res21: String = 'f and 99 and 2.2
    tupleMatcher((true, 's, 11, "scala"))     //> res22: String = Unknow

3. 列表

    def listMatcher(l: Any) = l match {
        case List() =>  "an empty List"
        case x :: y :: Nil => x + " --- " + y
        case 9 :: tail => tail
        // 下面匹配的两种写法
        case x :: y => x + " +++ " + y
        case :: (x, y) => x + " === " + y
        
        case _ => "Unkonw"
    }                                         //> listMatcher: (l: Any)java.io.Serializable
    
    listMatcher(List())                       //> res23: java.io.Serializable = an empty List
    listMatcher(List('s,2))                   //> res24: java.io.Serializable = 's --- 2
    listMatcher(List(true, 'h, 99))           //> res25: java.io.Serializable = true +++ List('h, 99)
    listMatcher(List(9, "scala", 'p))         //> res26: java.io.Serializable = List(scala, 'p)
    listMatcher('k :: List())                 //> res27: java.io.Serializable = 'k +++ List()

相关文章

  • Scala模式匹配及偏函数

    模式匹配公式: 一、代码Demo 二、Scala中的模式匹配处理异常 三、Scala函数

  • Case Class与模式匹配

    模式匹配入门 在java语言中存在switch语句,例如: 上述scala代码展示了如何使用scala中的模式匹配...

  • scala模式匹配

    以下是常见的scala模式匹配实例。 variableName @ pattern 下面这个示例 模式匹配中cas...

  • Scala学习笔记(八) 模式匹配

    1. 模式匹配简介 模式匹配是 Scala 的重要特性之一,前面两篇笔记Scala学习笔记(六) Scala的偏函...

  • scala-模式匹配和样例类

    模式匹配和样例类 模式匹配 Scala没有Java中的switch case,它有一个更加强大的模式匹配机制,可以...

  • scala(二十一) 模式匹配(match)

    前言 Scala中的模式匹配类似于Java中的switch语法,但是更加强大。模式匹配语法中,采用match关键字...

  • scala中的模式匹配

    一、常量、类型匹配 二、 数组列表和元组匹配 1. 数组 2. 元组 3. 列表

  • Scala中的模式匹配

    简单匹配 模式匹配常用于match语句: 变量使用 模式匹配case中可以使用变量来获取参数值 类型匹配 守卫匹配...

  • Scala中的模式匹配

    本文作者:林伟兵,叩丁狼高级讲师。原创文章,转载请注明出处。 在Java中我们可以通过switch..case语句...

  • Scala中的模式匹配

    在scala中,模式匹配的熟练使用可以给让程序看起来更加的简洁易懂,该特性有很多使用方法,下边介绍一些不太常用的。...

网友评论

      本文标题:scala中的模式匹配

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