scala高阶函数

模式匹配
模式匹配
样例类
封闭类
偏函数

it-auto; text-indent: 0px; text-transf模式匹配
模式匹配是数据结构上的概念
val hi=Array("hi","hello","world")
hi match{
case Array(“hi”, _*) => println(“Ok”)
case Array("hi","world",_) => println("Wrong")
case _ => println(“None") }
数据结构包括各种集合,类,函数等
通配符 “ _”表示任意,“ _*”则表示任意长度,

模式匹配
类型匹配
def getType(a:Any){
a match{
case _ :Array[Char] => println("Array[Char]")
case _ :Int => println("Int")
case _ :Char => println("Char")
case _ => println(“Error")
} }
注意:泛型的类型匹配要注意如List[String]、 Map[Char,Int]等不会成功匹配,如
List[Int]等亦可匹配,因而往往使用通配符List[ _ ]进行匹配,但Array[Int]是可行的

m: none; white-space: normal; widows: 样例类
abstract class Expr
case class Number( n :Int) extends Expr
case class Sum(e1 : Expr , e2 : Expr) extends Expr
case object Mul(e1 : Expr , e2 : Expr) extends Expr .
def getType(a:Expr):String =
a match{
case Number(n) => Number
case Sum(m,n) => “Sum“
case _ => “Else"
}

样例类
abstract class Expr{
def getType : String = this match {
case Number(n) => “Number”
case Sum(e1 , e2) => “Sum” } }
val hi=new Number(4)
val hello=new Sum(3,7)
hi.getType =“Number” hello.getType =“ Sum”
样例类默认生成toString、 equals、 hashCode跟copy方法

封闭类
模式匹配完成后需要确保所有情况皆被考虑
Scala编译器会检测match表达式所遗漏的模式组合
使样本类的超类被封闭(sealed),封闭类除类定义文件外不能添加子类
sealed abstract class Expr
case class Number( n :Int) extends Expr
case class Sum(e1 : Expr , e2 : Expr) extends Expr
case class Mul(e1 : Expr , e2 : Expr) extends Expr

封闭类
如何定义存在可能样本遗漏的模式匹配
def getType(a:Expr):String =
a match{
case Number(n) => “Number“
case Sum(m,n) => “ Sum“}
warning : match is not exhaustive
case _ =>
添加注解
def getType(a:Expr):String = (a: @unchecked ) match {...}


normal; line-height: normal; orphans: 偏函数
限定输入参数的值的函数
把函数应用到其不支持的值时,产生运行异常
PartialFunction[A,B],Scala包中给出的部分函数类型
val getNum:PartialFunction[Int,String] ={
case 1 => “one”
case 2 => “two”}
getNum.isDefinedAt(1) = true
getNum.isDefinedAt(3) = false
PartialFunction可以使用orElse组成新的函数
val part= getNum orElse getSize orElse getLength

g: 0px; -webkit-text-size-adjust: auto偏函数
把参数模式匹配
react{
case (name:String , actor:Actor) =>{
actor ! getip(name)
act( ) }
case msg =>{
println(“Unhandled message:”+msg)
act( ) } }

: auto; -webkit-text-stroke-width: 0px类型参数
泛型
类型变量界限
型变(协变与逆变)

-space: normal; widows: 2; word-spacin泛型
abstract class IntStack{
def push(x:Int):IntStack = new IntNonEmptyStack(x,this)
def isEmpty:Boolean
def top:Int
def pop:IntStack}
class IntEmptyStack extends IntStack{
def isEmpty=true
def top=error(“EmptyStack.top”)
def pop=error(“ EmptyStack.pop”) }
class IntNonEmptyStack(elem:Int,rest:IntStack) extends IntStack{
def isEmpty=false
def top=elem
def pop=rest }

-spacing: normal; line-height: normal;泛型
类、特质、函数、方法可带有类型参数
def getType(a:Any) //a是Any类型
def getType[T](a:T) //a是泛型,getType是泛型函数
class Pair[T,S](val first:T,val second:S) //泛型类
trait Pair[T] //泛型特质
当类型被指定的时候构成具体的类、函数等
val getInt = getType[Int] _
支持类型推断
val p1=new Pair[25,25.0] //生成Pair[Int,Double]类
val p2=new Pair[25.0,25]

-space: normal; widows: 2; word-spacin泛型
abstract class Stack[T]{
def push(x:T):Stack[T] = new NonEmptyStack[T](x,this)
def isEmpty:Boolean
def top:T
def pop:Stack[T]}
class EmptyStack[T] extends Stack[T]{
def isEmpty=true
def top=error(“EmptyStack.top”)
def pop=error(“ EmptyStack.pop”) }
class NonEmptyStack[T](elem:T,rest:Stack[T]) extends Stack[T]{
def isEmpty=false
def top=elem
def pop=rest }

泛型
val m=new IntEmptyStack
val m=new EmptyStack[Int]
val n=new EmptyStack[Array]
val q=m.push(1).push(2)
val p=n.push(Array(5,7,9)).push(Array('5','7','9'))

style=" font-style: normal; font-vari类型参数界限
对于泛型结构,类型参数界限能限制应用在该泛型上的类型
Ordered[T]:Scala标准库中提供的一个特质,用以表示可比较的类型
abstract class Stack[ T <: Ordered[T] ]
class EmptyStack[ T <: Ordered[T] ] extends Stack[T]
class NonEmptyStack[ T <: Ordered[T] ](elem:T,rest:Stack[T]) extends Stack[T]
可以通过自行构建的类混入Ordered特质使之符合参数界限(能比较)
类型参数上界实则通过限制类型必须拥有某些特质
T <: Compatable[T]
T <: Integral[T]

: normal; line-height: normal; orphans类型参数界限
abstract class Stack[ T <: Ordered[T] ]
class EmptyStack[ T <: Ordered[T] ] extends Stack[T]
class NonEmptyStack[ T <: Ordered[T] ](elem:T,rest:Stack[T]) extends Stack[T]
val m=EmptyStack[Int]
Int不是Ordered[Int]的子类,但RichInt是
视界限定
T <% Ordered[T] // T 可以被隐式转换为Ordered[T]
类似的,便有类型参数下界
R >: S //S是R的子类,R是S的超类

ont-style: normal; font-variant: norma类型参数界限
上下文界定
T : M //M[T]是在源码中定义的另一个泛型类
多重界定
T >: lower <:upper //同时有上下界,但不能同时有多个上界或下界
T <: Ordered[T] with Cloneable //能要求同时混入多个特质
T <% Ordered[T] <% String //能同时多个视图限定
T : Ordering : Manifest //能同时多个上下文界定
类型约束
T =:= U T <:< U T <%< U
使用类型约束需添加(implicit ev: T <:< U)

ormal; font-variant: normal; font-weig型变(协变与逆变)
class School
class Student extends School
Stack[School]Stack[Student]没有任何关系,是两个独立的类
class Stack[+T] +号意味着Stack与T是协变的,Stack与T朝着同样的方向型变
Stack[Student] extends Stack[School]
class Stack[-T] -号意味着Stack与T是逆变的,Stack与T朝着相反的方向型变
Stack[School] extends Stack[Student]
注意:泛型的类型声明中可以同时使用协变和逆变

t-indent: 0px; text-transform: none; w型变(协变与逆变)
trait Function1[-T,+R] extends AnyRef
class Animal(val sound =“rustle”)
class Bird extends Animal(override val sound=“call”)
class Chicken extends Bird(override val sound=“cluck”)
val getTweet : (Bird =>String) =( (a:Animal) => a.sound )
getTweet : Bird =>String = <fouction1>
//输入参数是逆变的,只能是Bird的超类
val hatch : ( ( )=>Bird ) =( ( )=>new Chicken )
hatch : ( ) => Bird =<function0>
//输出值是协变的,可以是Bird的子类




你可能感兴趣的:(scala高阶函数)