Partial function, also called partial application function, is called when the function is only part of the parameters. It's easy to get an example first.
Object Partialappliedfunction {def main (args:array[string]): Unit = {val part_sum = sum (1,_:int,3) println (Part_ SUM (2))} def sum (a:int,b:int,c:int) =a+b+c}
We have defined a function sum, which has a total of 3 parameters. We have also defined a function part_sum, which relies on the SUM function, but only some of the parameters are replaced with placeholders. So when we call Part_sum, we simply pass in the non-passed arguments of sum.
and Part_sum (2) = = SUM (+/-)
This is called a partial application function.
We know that in Scala a function is a class citizen, and a function can be assigned to another variable or function as a variable.
So let's look at the following statement
scala> def sum (a:int,b:int,c:int) =a+b+csum: (A:int, B:int, C:int) intscala> val sum3 = Sum<console>:11:erro R:missing arguments for method Sum;follow This method with ' _ ' If you want to treat it as a partially applied function Val sum3 = sum ^
Assigning a function name directly to another variable will cause an error.
In fact, what we want to do is:
scala> val sum3 = SUM (_:int,_:int,_:int) sum3: (int, int, int) = = Int = <function3>
Scala offers a simple notation.
scala> val sum3 = sum _sum3: (int, int, int) = = Int = <function3>
is the special case of partial application of functions, all parameters are applied.
In the video, the teacher cited an example:
Object Partialappliedfunction {def main (args:array[string]): Unit = {val data = List (1,2,3,4,5) Data.foreach (pri Ntln _)}}
which
Data.foreach (println _)
is also part of the application function, at the beginning of how to think not understand. This is not the notation for parameter placeholders. Actually, this code should understand that.
Data.foreach () should pass in a function as a parameter. and println _ and Sum _ are similar to the wording.
The parameter placeholder should be written like this:
Data.foreach (println (_))
This article is from the "Ding Dong" blog, please be sure to keep this source http://lqding.blog.51cto.com/9123978/1741829
21st: Partial functions (partially applied function) practical explanation