scala 偏函数

偏函数
常用于 数据转换

object FunctionDemo {


  def funPatrtition3:PartialFunction[String,Int]={
    case "男"=>1
    case "male"=>1
    case "女"=>0
    case "female"=>0
    case _=>(-1)
  }
  val arr = Array("男","女","male","female","中")
  val ints: Array[Int] = arr.collect(funPatrtition3)		//[1,0,1,0,-1]
  println(ints.toList)


  def funPartition2:PartialFunction[Int,String]={
    case 1=>"优秀"
    case 2=>"良好"
    case 3=>"合格"
    case 4=>"不合格"
    case _=>"错误"
  }
  println(funPartition2(3))			//合格
  val arr = Array(1,4,2,5,1,6,4,2,3,7,2)
  val strings: Array[String] = arr.collect(funPartition2)		//[优秀,不合格,良好,错误,优秀,错误,不合格,良好,合格,错误,良好]
  println(strings.toList)


  def funPartition:PartialFunction[String,Int]={
    case "hello"=>1
    case "world"=>2
    case _=>0
  }
  val i: Int = funPartition("hello")
  println(i)		//1
  val arr = Array("hello","scala","java","world")
  val ints: Array[Int] = arr.collect(funPartition)     //[1,0,0,2]
  for(in<-ints){
    println(in)
  }


}
上一篇:数据交互工具_Hue


下一篇:php5.6.3 源码安装