好程序员大数据学习路线分享高阶函数

  好程序员大数据学习路线分享高阶函数,我们通常将可以做为参数传递到方法中的表达式叫做函数

高阶函数包含:作为值的函数、匿名函数、闭包、柯里化等等。

定义函数时格式:val 变量名 = (输入参数类型和个数) => 函数实现和返回值类型和个数

“=”表示将函数赋给一个变量

“=>”左面表示输入参数名称、类型和个数,右边表示函数的实现和返回值类型和参数个数

作为值的函数

定义函数

scala> val func = (x:Int) => x * x
func: Int => Int = <function1>

scala> val func:Int => Int = x => x * x
func: Int => Int = <function1>

scala> func(3)
res0: Int = 9

函数调用

scala> val arr = Array(1,2,3,4)
arr: Array[Int] = Array(1, 2, 3, 4)

scala> val res = arr.map(x => func(x))
res: Array[Int] = Array(1, 4, 9, 16)

scala> val res = arr.map(func(_))
res: Array[Int] = Array(1, 4, 9, 16)

scala> val res = arr.map(func)
res: Array[Int] = Array(1, 4, 9, 16)

将方法转化为函数

scala> def m1(x:Int):Int = x * x
m1: (x: Int)Int

scala> def m1(x:Int) = x * x
m1: (x: Int)Int

scala> def m2(x:Int) {x * x}
m2: (x: Int)Unit

scala> val f1 = m1 _
f1: Int => Int = <function1>

scala> val res = arr.map(x => m1(x))
res: Array[Int] = Array(1, 4, 9, 16)

scala> val res = arr.map(m1(_))
res: Array[Int] = Array(1, 4, 9, 16)

scala> val res = arr.map(m1)
res: Array[Int] = Array(1, 4, 9, 16)

匿名函数

在Scala中,你不需要给每一个函数命名,没有将函数赋给变量的函数叫做匿名函数

scala> arr.map((x:Int) => x * x)
res3: Array[Int] = Array(1, 4, 9, 16)

scala> arr.map(x => x * x)
res4: Array[Int] = Array(1, 4, 9, 16)

scala> arr.map(m1)
res1: Array[Int] = Array(1, 4, 9, 16)

scala> arr.map(_ * 2)
res2: Array[Int] = Array(2, 4, 6, 8)

闭包

闭包就是能够读取其他函数内部变量的函数

可以理解成,定义在一个函数内部的函数

本质上,闭包是将函数内部和函数外部链接起来的桥梁

object Bibao {

  def sum(f:Int => Int):(Int,Int) => Int = {
    def sumf(a:Int,b:Int):Int = {
      if (a > b) 0 else f(a)+sumf(a + 1,b)
    }
    sumf  //隐式转换成函数
  }
  def main(args: Array[String]): Unit = {
    def sumInts = sum(x => x)
    println(sumInts(1,2))
  }
}

柯里化

柯里化指的是将原来接收两个参数列表的方法或函数变成新的一个参数列表的方法或函数的过程

声明和转化

scala> def curry(x:Int)(y:Int) = x * y  //声明
curry: (x: Int)(y: Int)Int

scala> curry(3)(4)
res8: Int = 12

scala> val curry1 = curry(3)  //转换成方法 : 加""
curry1: Int => Int = <function1>

scala> curry1(5)
res9: Int = 15

scala> def curry2(x:Int) = (y:Int) => x * y  //声明
curry2: (x: Int)Int => Int

scala> val func = curry2(2)  //直接转化
func: Int => Int = <function1>

scala> func(4)
res16: Int = 8

scala> def curry3() = (x:Int) => x * x
curry3: ()Int => Int

scala> val func = curry3()    //转化空参
func: Int => Int = <function1>

scala> func(3)
res17: Int = 9

柯里化需要与隐式转换相结合

implicit  隐式的 -> 隐式值在当前会话中同类型只能定义一次,不同类型可定义多个

scala> def m1(x:Int)(implicit y:Int=5) = x * y
m1: (x: Int)(implicit y: Int)Int

scala> m1(3)
res10: Int = 15

scala> m1(3)(6)    //隐式值可以改变
res11: Int = 18

scala> implicit val x = 100  //定义成全局的隐式值,可以覆盖
x: Int = 100

scala> m1(3)(6)
res12: Int = 18

scala> m1(3)
res13: Int = 300

scala> implicit val y = "abc"
y: String = abc

案例: 把数组中元祖的value相加

scala> val arr = Array(("xixi",1),("haha",2),("heihei",3))
arr: Array[(String, Int)] = Array((xixi,1), (haha,2), (heihei,3))

scala>  arr.foldLeft(0)( + ._2)  //(初始值)(上次计算结果+循环出的结果)
res15: Int = 6

Curry的Demo

object Context{   //一般情况会新建类,再在此地调用
  implicit val a = "yaoyao"
  implicit val b = 100
}
object Curry {

  //与变量没有关系,系统自己匹配相同类型的值
  implicit val a = "yaoyao"
  implicit val b = 100

  def m1(str:String)(implicit name:String="xiaodan"){
    println(str + name)
  }

  def main(args: Array[String]): Unit = {
    import Context.a
    m1("Hello ")
  }
}

隐式转换

作用: 隐式的对类的方法进行增强,丰富现有类库的功能

隐式转换:

继承 -> 通过方法的重写来对父类的方法增强

代理模式 -> 远程代理,多用于网站,代理一个实例,可以对实例方法增强,在调用方法之前

        代理,方法之后环绕

    装饰模式 -> 装饰模式也叫包装模式,用java读取文件时要用到IO流,也是对实例方法增强                          

    new BufferInputStream(new FileInputStream).read()

就是用到了装饰模式和门面模式 ->

装饰模式是显示的包装,隐式转换就是隐式的做了包装

门面模式起到了隐式包装的作用

隐式转换函数 : 是指以implicit关键字声明的带有单个参数的函数

案例: 用隐式转换,实现从给定的URI直接能调用read方法读取文件内容

object MyPredef {

  //implicit def fileToRichFile(file:String) = new RichFile(file)

  implicit val fileToRichFile = (file:String) =>new RichFile(file)

}

object RichFile {

  def main(args: Array[String]): Unit = {

    val file = "e://wordcount.txt"

//    //显示的实现了对file的方法增强

//    val richFile = new RichFile(file)

//    val content: String = richFile.read()

//    println(content)

    //隐式转换

    import MyPredef.fileToRichFile

    val content = file.read()

    println(content)

  }

}

class RichFile(val file:String){

  //创建read方法

  def read():String = {

    Source.fromFile(file).mkString

  }

}

猜你喜欢

转载自blog.51cto.com/14479068/2431406