Scala(二)

Scala


  • match表达式

    • 类似Java switch语句

      • 能处理所有类型

      • 不需要

      • 能够生成值

        val firstArg=if(args.length>0) args(0) else ""
        firstArg match{
            case "salt" => println("pepper")
            case "chips" => println("salsa")
            case "eggs" => println("bacon")
            case _ => println("huh?")	//以上未列出的值,使用“_”表示
        
      • 样例类在模式匹配

        • 模式匹配

          • 检查某个值是否匹配某一个模式在机制,一个成功在匹配同时会匹配值结构为其组成部分

            //仅匹配类型
            def matchTest3(x: Any): String = x match {
              case x:Int => "Int"
              case x:String => "String"
              case _ => "Any"
            }
            matchTest3(3.0)  // Any
            matchTest3(1)     // Int
            //样例类的模式匹配
            def matchTest4(x: Student)= x match {
              case Student(name,19) => println(name)
              case Student("Tom",age) => println(age)
              case Student(name,age) => println(name,age)
              case _ => println("no matches")
            }
            matchTest4(Student("Jason",19))
            matchTest4(Student("Tom",20))
            matchTest4(Student("Jimmy",20))
            
      • 非样例类的模式匹配

        • 单例对象中指定unapply()方法时,成为提取器对象

        • unapply()方法接受一个实例对象,返回最初创建它所用的参数

          class Student(_name:String,_age:Int) {
            var name=_name
            var age=_age
          }
          object Student{
            def apply(name: String, age: Int): Student = new Student(name, age)
            def unapply(arg: Student): Option[(String, Int)] ={
              if(arg==null) None else Some(arg.name,arg.age)  
            }
          }
          def matchTest(x:Student)=x match {
              case Student(name,age) if age<=20 => println("young man")
              case Student(name,age) if age>20 => println("old man")
          }
          matchTest(Student("Jason",19))   //young man
          
    • 偏函数

      • 偏函数是只对函数定义域的一个子集进行定义的函数

      • PartialFunction[-A,+B]是一个特质

        • A为函数定义域,B为偏函数返回值类型
        • apply()
        • idDefinedAt()
        //自定义偏函数
        val inc = new PartialFunction[Any, Int] {
            def apply(any: Any) = any.asInstanceOf[Int]+1
            def isDefinedAt(any: Any) =  if (any.isInstanceOf[Int]) true else false
        }
        List(1,2,3,"four").collect(inc)
        
      • case语句

        val pf:PartialFunction[Any, Int]={case x:Int=>x+1} //返回一个偏函数
        List(1,2,3,"four").collect(pf)  //输出List(2,3,4)
        
    • 注解

      • Scala标准库注解包—scala.annotation

      • 注解语法:@注解名称(注解参数…)

      • 可使用注解的地方

        • 类,方法,方法参数,字段,局部变量

          object DeprecationDemo extends App{
            @deprecated("deprecation message", "release # which deprecates method")
            def hello = "hola"
            @throws(classOf[Exception])
            def test(){}  
          }
          
    • 运算符

      • 在Scala中,运算符即是方法.任何具有单个参数的方法都可以用作中缀运算符

        1.+(10) >> 1+10

      • 定义和使用运算符

        case class Vec(val x: Double, val y: Double) {
          def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y)
          def add(that: Vec) = new Vec(this.x + that.x, this.y + that.y)
        }
        val vector1 = Vec(1.0, 1.0)
        val vector2 = Vec(2.0, 2.0)
        val vector3 = vector1 + vector2  // 或者 vector1 add vector2
        vector3.x  // 3.0
        vector3.y  // 3.0
        
    • 正则表达式

      • Scala支持多种正则表达式解析方式

        • String.matches()方法

        • 正则表达式模式匹配

        • scala.tuil.matching.Regex API

          //String.matches
          "!123".matches("[a-zA-Z0-9]{4}")  //false
          "34Az".matches("[a-zA-Z0-9]{4}")  //true
          
          //模式匹配,Regex实现了提取器
          val ZipcodePattern = "([a-zA-Z][0-9][a-zA-Z] [0-9][a-zA-Z][0-9])".r
          "L3R 6M2" match {
              case ZipcodePattern(zc) => println("Valid zip-code: " + zc )  //zc为第1个分组结果,可以匹配多个分组
              case zc => println("Invalid zip-code: " + zc )
          
        • scala.tuil.matching.Regex

          • findFirstMatchIn()返回第一个匹配

          • findAllMatchiIn()返回所有匹配结果

          • findAllIn()返回所有匹配结果

            import scala.util.matching.Regex
            val numberPattern: Regex = "[0-9]".r
            numberPattern.findFirstMatchIn("awesomepassword") match {
              case Some(_) => println("Password OK")  //匹配成功
              case None => println("Password must contain a number")   //未匹配
            }
            
        • 捕获分组

          • 识别"name:Jasonage:19,…"中的键值对

            import scala.util.matching.Regex
            
            val studentPattern:Regex="([0-9a-zA-Z-#() ]+):([0-9a-zA-Z-#() ]+)".r
            val input="name:Jason,age:19,weight:100"
            
            for(patternMatch<-studentPattern.findAllMatchIn(input)){
                println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}")
            }
            
        • 字符串替换

          //search
          val nums = "[0-9]+".r.findAllIn("123 Main Street Suite 2012")
          nums.next   // -> 123
          nums.next  // -> 2012
          
          //replace
          "[0-9]+".r.replaceFirstIn("234 Main Street Suite 2034", "567") //234->567   
          "[0-9]+".r.replaceAllIn("234 Main Street Suite 2034", "567") //234、2034->567  
          
        • 在字符串中查找模式

          val date = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
          "2014-05-23" match {
              case date(year, month, day) => println(year,month,day)
          }
          "2014-05-23" match {
              case date(year, _*) => println("The year of the date is " + year) 
          } 
          "2014-05-23" match {
              case date(_*) => println("It is a date")
          }
          
      • 闭包

        • 闭包是依照包含自由变量的函数字面量在运行时创建的函数值

        • 闭包是对函数本身即其使用的自由变量的统一定义

          val y=1
          val add=(x:Int)=>x+y	//add是闭包,y是自由变量
          
        • 闭包的变量绑定

          • 闭包可捕获自由变量的变化
          • 闭包对捕获便令做出的改变在闭包之外也可见
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值