Scala扩展

match表达式

类似Java switch语句

  • 能处理类型所有类型
  • 不需要break
  • 能够生成值
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?")	//以上未列出的值,使用“_”表示
}

样例类的模式匹配

模式匹配

  • 检查某个值(value)是否匹配某一个模式的机制,一个成功的匹配同时会将匹配值解构为其组成部分
//样例类的模式匹配
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))


//仅匹配类型
def matchTest3(x: Any): String = x match {
  case x:Int => "Int"
  case x:String => "String"
  case _ => "Any"
}
matchTest3(3.0)  // Any
matchTest3(1)     // Int

非样例类的模式匹配

单例对象中指定unapply()方法时,称为提取器对象(Extractor Objects)
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()
  • isDefinedAt()
//自定义偏函数
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)

注解(Annotation)

  • Scala标准库注解包——scala.annotation
  • 注解语法
    @注解名称(注解参数…)
常用注解
@throws@deprecated@unchecked@SerialVersionUID……

  • 可使用注解的地方
    类、方法、方法参数、字段、局部变量
object DeprecationDemo extends App{
  @deprecated("deprecation message", "release # which deprecates method")
  def hello = "hola"
  @throws(classOf[Exception])
  def test(){}  
}

运算符

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

  • 定义和使用运算符
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.util.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 //使用“.r”方法可使任意字符串变成一个Regex实例
"L3R 6M2" match {
    case ZipcodePattern(zc) => println("Valid zip-code: " + zc )  //zc为第1个分组结果,可以匹配多个分组
    case zc => println("Invalid zip-code: " + zc )
} 

  • scala.util.matching.Regex
    findFirstMatchIn() 返回第一个匹配(Option[Match])
    findAllMatchIn() 返回所有匹配结果(Regex.Match)
    findAllIn() 返回所有匹配结果(String)
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:Jason,age: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")
}

隐式类

  • 隐式转换
    • 隐式参数、隐式函数
    • 隐式类:用implicit关键字修饰的类,其主构造器可用于隐式转换
      • 只能在类、Trait、对象(单例对象、包对象)内部定义
      • 构造器只能携带一个非隐式参数
      • 隐式类不能是case class
      • 在同一作用域内,不能有任何方法、成员或对象与隐式类同名

隐式类应用
为String对象增加新的方法

object Stringutils {
  implicit class StringImprovement(val s:String){//隐式类
  def increment=s.map(x=>(x +1).toChar)
  }
}
object Main extends App{
  import Stringutils._
  println("mobin".increment)	//输出:npcjo
}

Scala异常处理

  • 抛出异常
throw new 异常类型
  • 捕获异常
try{
  //todo
}catch{
  case ex:异常类型=>{ //todo }
  ……
}finally{
  //todo
}

try {
      val f=new File("input.txt")
      if(!f.canWrite)
        throw new Exception("file can't write")
      val file=Source.fromFile(f);
      for(line<-file.getLines()) println(line)
} catch {
      case ex: FileNotFoundException => {
        println("Missing file exception")
      }
      case ex: IOException => {
        println("IO Exception")
      }
      case ex: Exception => {
        println(ex.getMessage)
      }
} finally {
      println("Exiting finally...")
}

  • 使用Either处理异常
    • Either[A, B] 表示要么包含一个类型为A的实例,要么包括一个类型为B的实例
    • Either只有两个子类型:Left、Right,如果Either[A, B]对象包含的是A的实例,则它是Left实例,否则是Right实例
    • Either用于异常处理时,一般约定:Left 代表出错的情况,Right 代表成功的情况
def divide(x:Int): Either[String,Int] ={
  if(x==0)
    Left("除数不能为0")
  else
    Right(100/x)
}
def test(x:Int)=divide(x) match {
    case Left(errMsg)=>println(errMsg)
    case Right(result)=>println(result)
}
test(0)
test(1)

  • allCatch
scala.util.control.Exception.allCatch.opt("42".toInt)      // Some(42)
scala.util.control.Exception.allCatch.opt("42a".toInt)    // None

scala.util.control.Exception.allCatch.toTry("42".toInt)  // 42
scala.util.control.Exception.allCatch.toTry("42a".toInt)  // Failure (e)

scala.util.control.Exception.allCatch.withTry("42".toInt)  // Success(42)
scala.util.control.Exception.allCatch.withTry("42a".toInt)  // Failure (e)

scala.util.control.Exception.allCatch.either("42".toInt)  // Right(42)
scala.util.control.Exception.allCatch.either("42a".toInt)  // Left(e)

  • failAsValue 发生异常时使用缺省值
scala.util.control.Exception.failAsValue(classOf[Exception])(-9999)("42a".toInt)

高级类型

结构类型

//定义结构类型
{
    def sayHello(name:String):Unit
}
//结构类型作为函数参数
def f(a:{def sayHello():Unit}){a.sayHello}
//函数调用
f(new {def sayHello():Unit={println("hello")}})

  • 复合类型可以由多个对象类型构成,主要用于缩短已有对象成员的签名
    格式为:A with B with C … { refinement }
trait X1 
trait X2
//定义复合类型参数x
def test(x: X1 with X2) = {println("ok")}
//函数调用,实参为匿名对象
test(new X1 with X2)
object A extends X1 with X2
//实参为单例对象
test(A)

Scala集成Java API

  • 需求
    在Scala中使用Java API计算今天、昨天……等日期
  • 分析
    java.text.SimpleDateFormat
    java.util.Calendar
    java.util.Date
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
val dateFmt = "yyyy-MM-dd"
def today(): String = {
    val date = new Date
    val sdf = new SimpleDateFormat(dateFmt)
    sdf.format(date)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值