Good programmers to share large data line learning higher-order functions

  Good programmers to share large data line learning higher-order functions, we usually can be used as an expression method to pass parameters to the called function

Order function comprises: a function value, anonymous function, closures, currying the like.

Format-defined function: val = variable name (input parameter type and number) => function implemented and the number and type of return value

"=" Indicates a function assigned to a variable

"=>" Denotes the left input parameter name, type and number, and to achieve the right side represents the number of parameters and return type of the function

As a function of the value of

Defined Functions

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

Function call

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)

The method of conversion as a function of

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)

Anonymous function

In Scala, you do not need to give each function name, no function is a function assigned to the variable called an anonymous function

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)

Closure

Closure is the function of the internal variable can be read by other function

As it will be appreciated, in a defined function within the function

In essence, the closure is a function of the internal and external functions to link the bridge

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))
  }
}

Currying

Currying refers to the original two receiving method or function argument list procedure becomes a new method or function of a parameter list

Declarations and transformation

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

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

Scala> Curry Val curry1 = (. 3)  // converted Method: add " "
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 ) // direct conversion
func: Int => Int = < function1>

scala> func(4)
res16: Int = 8

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

scala> val func = curry3 () // null transformation parameters
func: Int => Int = < function1>

scala> func(3)
res17: Int = 9

Currying desirably combined implicit conversion

implicit implicit -> implicit value in the current session of the same type can be defined only once, a plurality of different types can

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 ) // implicit value may be changed
res11: Int = 18

scala> implicit val x = 100 // defined implicit global value, may be covered
x: Int = 100

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

scala> m1(3)
res13: Int = 300

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

Case: The Ghost progenitor array of value adding

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) // (initial value) (the last calculation cycle results +)
res15: Int. 6 =

Curry manner Demo

object Context {// will generally be a new category, here again calling
  Implicit Val A = "yaoyao"
  Implicit B = Val 100
}
Object {Curry

  // variable does not matter, the system itself matching values of the same type
  Implicit Val A = "yaoyao"
  Implicit B = Val 100

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

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

Implicit conversion

Action: Implicit approach to enhanced class, enrich existing library function

Implicit conversion:

Inheritance -> by rewriting method to enhance the parent class method

Proxy Mode -> remote agent, used for the site, a proxy instance, can be enhanced for instance methods before calling the method

        Agent, after surrounding Method

    Decoration Mode -> decorative patterns, also known as packing mode, to use IO streams when using java to read the file, but also to enhance the instance method                          

    new BufferInputStream(new FileInputStream).read()

It is used in decorative patterns and facade Mode ->

Decorative pattern is displayed in the package, implicit conversion is done implicitly packaging

Facade pattern acts as an implicit package

Implicit transfer function: keyword statement refers to the implicit argument function with a single

Case: implicit conversion, read the contents of the documents given URI can call directly from the read method

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"

// // show the method to achieve the enhancement of the file

//    val richFile = new RichFile(file)

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

//    println(content)

    // implicit conversion

    import MyPredef.fileToRichFile

    val content = file.read()

    println(content)

  }

}

class RichFile(val file:String){

  // Create the read method

  def read():String = {

    Source.fromFile(file).mkString

  }

}

Guess you like

Origin blog.51cto.com/14479068/2431406