One of the main features of Scala is the support for functional programming. A function is a class citizen in function programming: functions can be passed as arguments to other functions, can be used as return values for other functions, and can even be nested within other functions. These higher-order functions are called function values.
Give a simple example: Sum from 1 to a number. Using Java is easy to implement:
int sum (int max) { int result = 0; for (int i = 0; I <= max; i++) { + =i ; } return result; }
It doesn't make much difference to use Scala.
Now expand the requirement: sum the odd or even numbers within a range. What to do with Java, the most straightforward way to write a scheme for odd and even sums, respectively. But this purely repetitive physical work seems a bit annoying. It would be great if you could pass the logic of the parity analysis as a parameter in this case. See how this is done using a functional scheme:
def totaloverrange (max:int, func:int = int): int = { = 0; for (I <-1 to max) { + = func (i) } result}
This defines a totaloverrange function to be computed, which has two parameters Max and func. Where Max is a normal parameter that represents the maximum value of the loop. Func is a function value, which is essentially a function that receives an int and returns an int. In the Totaloverrange method body, the logical processing of the cyclic value is given by the function value Func, Totaloverrange is only responsible for the accumulation.
Let's try a simple summation from 1 to Max:
Print (Totaloverrange (5, I=>i))
The preceding code snippet passes two parameters when calling the Totaloverrange method, the first parameter identifies the loop to 5, and the second parameter, "I=>i", is an anonymous function (that is, a function that implements No name), which is used to represent the processing logic for the loop value. Use "= =" in the function to separate the argument list and the function implementation from the left. The function implementation here simply returns the parameter I. Note that you do not need to specify the type of the function value parameter here, and Scala infers the type of the parameter from the Totaloverrange method. Take a look at the results:
Let's do a few more of the even sums within 5:
if Else 0))
At first I wrote the function values:
if return Else return 0
Add return, but it is wrong to do so. Do not understand why, I hope that later in the study can be doubts.
Take a look at the results:
The logic of odd arithmetic doesn't need me to write it.
One benefit of using function values is shown here: You can reduce the duplication of code. By placing the common code in a function, the partial encapsulation of the difference is passed in as a function value parameter, which reduces the duplication of the code. This looks somewhat like a template method pattern, but there is no need to define so many interfaces and classes.
Think again, what if the function as a function value is complex? Do you use a function scheme or a template schema scheme?
I am not good to say, the specific situation to analyze it.
#######
Scala study notes 23-function values