Chapter 6 higher-order functions
6.1 as an argument
Passed to the function as a function of another variable, then the type as a function of the parameter is: function1, namely:
(Parameter Type) => Return value
// 6.1 as parameters DEF PLUS (X: Int) =. 3 + X Val RESULT1 = the Array (. 1, 2,. 3,. 4 ) .map (PLUS (_)) the println (result1.mkString ( ","))
Screaming Tip : with a type of the function parameter is function1, with two parameters are function2, and so on.
notes:
// 6.1 as parameters DEF down the Match1 (): Unit = { DEF PLUS (X: Int) =. 3 + X Val RESULT1 = the Array (. 1, 2,. 3,. 4 ) .map (PLUS (_)) the println (RESULT1 .mkString ( ",")) // 4,5,6,7 } down the Match1 ()
6.2 anonymous function
That function without a name, anonymous function can be set by the function expression.
Triple = Val (X: Double) =>. 3 * X
the println (Triple (. 3)) // Use: Array (3.14, 1.42, 2.0 ) .map (Triple) // contained in () in Array (3.14, 1.42 , 2.0) .map ((X: Double) =>. 3 * X) // contained in {} Array (3.14, 1.42, 2.0) map {(x: Double) => 3 * x}
notes:
// 6.2 anonymous function DEF match2, (): Unit = { Val Triple = (X: Double) =>. 3 * X the println (Triple ( . 3 )) //9.0 // use: the println (the Array (3.14, 1.42, 2.0 ) .map (Triple) .mkString ( "," )) //9.42, 4.26, 6.0 // contained in () in println (Array (3.14, 1.42, 2.0) .map ((x: Double) => 3 * X) .mkString ( "," )) // enclosed in {} in the println (the Array (3.14, 1.42, 2.0) {Map (X: Double) => X}. 3 mkString * ( "," )) } match2, ( )
6.3 Higher-order functions
def highOrderFunction1(f: Double => Double) = f(10) def minus7(x: Double) = x -7 val result2 = highOrderFunction1(minus7) println(result2)
def minusxy(x: Int) = (y: Int) => x - y val result3 = minusxy(3)(5) println(result3)
6.4 Parameter (Type) inference
// passed to the function expression valueAtOneQuarter ((X: Double) =>. 3 * X) // Parameter Estimation omitted type information valueAtOneQuarter ((X) =>. 3 * X) // single parameter may be omitted parentheses valueAtOneQuarter (x => 3 * the X-) // If the variable is intended => appears on the right only once, you can use _ to replace valueAtOneQuarter (3 * _)
6.5 Closures
def minusxy(x : Int) = (y : Int) => x - y println(minusxy(10)(20))
This is a closure
Another example:
def minusxy(x: Int) = (y: Int) => x - y val f1 = minusxy(10) val f2 = minusxy(10) println(f1(3) + f2(3))
Here f1, f2 these two functions is called closure.
6.6 currying
// traditional definition of two parameters DEF MUL (X: Int, Y: Int) = X * Y the println (MUL ( 6, 7 are )) // Curry definition, used to closures def mulOneAtATime (x: Int) = (Y: Int) => X * Y the println (mulOneAtATime ( . 6) (. 7 )) // Scala may be abbreviated DEF mulOneAtATime (X: Int) (Y: Int) = X * Y the println (mulOneAtATime ( 10) (. 8 ))
2) the application of currying
Compare two strings are equal ignoring the case again under note, here are two tasks:
1, transfer all uppercase (lowercase)
2, more equality
For these two operations, we thought to deal with a function, in fact, inadvertently has become a thought process of the two functions.
Examples are as follows:
val a = Array("Hello", "World") val b = Array("hello", "world") // def corresponds[B](that: GenSeq[B])(p: (A,B) => Boolean): Boolean println(a.corresponds(b)(_.equalsIgnoreCase(_)))
def corresponds[B](that: GenSeq[B])(p: (A,B) => Boolean): Boolean = { val i = this.iterator val j = that.iterator while (i.hasNext && j.hasNext) if (!p(i.next(), j.next())) return false !i.hasNext && !j.hasNext }
6.7 abstraction
runInThread DEF (Block: () => Unit) { new new the Thread { the override DEF RUN () {Block ()} } .start () }
// passed to the function runInThread {() => println ( "Hi"); Thread .sleep (10000); println ( "Bye" )} () => somewhat redundant, can be used to change the name to indicate calls, omit () retention => // this function is called abstract control (looks like a key programming language function word) DEF runInThread (Block: => Unit) { new new the Thread { the override DEF RUN () {Block} } .start () }
// elegant incoming runInThread {println ( "Hi") ; Thread.sleep ( 1000); println ( "Bye")}
// define similar while the function until def until (condition:=> Boolean)(block: => Unit) { if (!condition) { block until(condition)(block) } }
var x = 10
until (x == 0) {
x -= 1
println(x)
}
notes:
// 6.7 abstraction DEF runOnThread (F1 :() => Unit): Unit = { new new the Thread { the override DEF RUN (): Unit = { F1 () } } .start () } runOnThread ({ () => the println ( "work!" ) the Thread.sleep ( 5000 ) the println ( "finish the job! ' ) }) // () => 1; 2; 3 + 1