Higher order functions
In mathematics and computer science, higher-order functions are functions that meet at least one of the following conditions:
- 接受一个或多个函数作为输入- 输出一个函数
In mathematics they are also called operators (operators) or functional. The derivative in calculus is a common example, because it maps a function to another function.
Examples of higher order functions
Suppose there is a function that sums all the integers in a given two-digit interval:
def sumInts(a: Int, b: Int): Int = if0else1, b)
If the sum of squares of consecutive integers is now required:
def square(x: Int): Int = x * xdef sumSquares(a: Int, b: Int): Int = if0else1, b)
If you want to calculate the sum of the power of 2:
defif01else2 * powerOfTwo(x-1)def sumPowersOfTwo(a: Int, b: Int): Int = if0else powerOfTwo(a) + sumPowersOfTwo(a+1, b)
The above function is an additive form of f (n) from A to B, and we can extract the common part of these functions to rewrite the function sum, where the defined F is passed in as a parameter to the higher-order function sum:
def sum (f:int = = int, a:int, b:int): Int = if (a > B) 0 else F (a) + sum (F, A+1 , b) def ID (x:int): Int = x< Span class= "Hljs-keyword" >def Square (x:int): Int = x * Xdef poweroftwo (x:int) : Int = if (x = = 0 ) 1 else 2 * poweroftwo (X-1 ) def sumints (A:int, b:int): Int = SUM (ID, a, b) def sumsquared (A:int, b:int): Int = SUM (square, A, b) def Sumpowersoftwo (A:int, b:int): Int = SUM (Poweroftwo, A, b)
Useful higher-order functions
The map method applies a function to all elements of a collection and returns the result; foreach applies the function to each element.
//打印三角形scala> (19).map("^" * _).foreach(println _)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The filter method outputs all elements that match a particular condition:
scala> (1920)res14: scala.collection.immutable.IndexedSeq[Int] = Vector(2468)
You can familiarize yourself with higher-order functions by practicing some common methods of accepting function parameters in the Scala collection library.
Currying
Curry (currying) refers to the process of turning a function that accepts two parameters into a new function that takes a parameter. The new function returns a function that takes the original second argument as a parameter.
Functions that return functions
In the above example of a higher order function, def sumInts(a: Int, b: Int): Int = sum(x => x, a, b)
we def sumSquared(a: Int, b: Int): Int = sum(x => x*x, a, b)
define a new function, the above two functions each pass a and b two parameters into the SUM function, can we simplify these parameters to make the function definition simpler?
We can simplify the parameter by returning the function's function:
def sum(f: Int => Int): (Int, Int) => Int = { def sumF(a: Int, b: Int): Int = if0 else f(a) + sumF(a+1, b) sumF}//于是得到如下定义,这样就简化了参数def sumInts = sum(x => x)def sumSquared = sum(x => x * x)def sumPowersOfTwo = sum(powerOfTwo)
Multiple parameter lists
According to the above example, can we simplify, omit sumInts
, sumSquared
sumPowersOfTwo
The form of these intermediate functions?
Replace the function with a sum(square)(1, 10)
function sumSquared
.
In general, this type of function is left-associative: sum(square)(1, 10) == (sum(square))(1, 10)
.
We can define the sum function like this:
def sum(f: Int => Int)(a: Int, b: Int): Int = if0else1, b)
This makes the function writing more concise.
In general, a multi-parameter function is defined as def f(args1)...(argsn) = E
,
When n > 1 o'clock, equal to def f(args1)...(args n-1) = {def g(argsn) = E; g}
or def f(args1)...(args n-1) = (argsn => E)
.
If you repeat this process n times, get it def f = (args1 => (args2 => ... (argsn => E) ) )
.
This function definition is called curry (currying).
reprint Please indicate the author Jason Ding and its provenance
Gitcafe Blog Home page (http://jasonding1354.gitcafe.io/)
GitHub Blog Home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Baidu Search jasonding1354 access to my blog homepage
"Scala" high-order functions and currying