Article directory
Part of Application Functions
Partial application function: It means that a function has N parameters, and we provide less than N parameters for it, then we get a partial application function.
def sum(a:Int,b:Int,c:Int) = a + b + c;
Then you can derive a partial function from this function as follows:
def p_sum = sum(1, _:Int, _:Int)
So you can call p_sum(2,3) in this way, which is equivalent to calling sum(1,2,3) and the result is 6. The two here correspond to the parameters of the corresponding position of the function sum. So you can also define them as
def p_sum = sum (_:Int, 1, _:Int)
2. Partial function
Partial Function Definition: A series of case statements enclosed in a pair of braces
For example, try/catch below throws an exception
try { var n= 10 / 0 }catch { //Partial function: case ex: Arithmetic Exception=> case ex: ArithmeticException=>{ // Arithmetic abnormality occurs println("Arithmetic abnormality occurs") } //Partial function: case ex: Exception case ex: Exception=>{ // Exception handling println("Abnormal 1 occurred") println("Abnormal 2 occurred") } }
If you want to define a function that accepts and processes only a subset of its parameter definition domain, an exception is thrown for parameters outside the parameter definition domain. Such a function is a partial function (as the name suggests, this function only handles some of the incoming parameters).
Let's look at two examples of partial functions:
def main(args: Array[String]): Unit = { //Call the higher-order function foo, pass in the ordinary function as a parameter, and return the result to r val r: Int = foo((a: Int, b: Int) => a * b) println(s"r: $r") //Partial function: A series of case statements enclosed in a pair of braces val r: Int = foo({ case (a, b) => a * b }) println(s"r: $r") } //A higher-order operator, which receives a function, needs a tuple to be parameterized, and the function returns an Int value. //The higher-order operator needs to perform the received function according to the incoming execution, and then return to Int. def foo(f: (Int, Int) => Int): Int = { f(3, 4) }
An example of the second partial function:
def main(args: Array[String]): Unit = { println(foo1((a, b) => { a._2 + b })) //Partial function: A series of case statements enclosed in a pair of braces println(foo1({ case ((a, b), c) => a * b + c })) def foo1(f: ((Int, Int), Int) => Int): Int = { f((1, 2), 10) } }
Application of Partial Functions: Numbers in Sets*2
//Because map can only adjust the data type and cannot change the length of the collection, the result will be null. val list1 = List(1,2,4,"abc", false) val list2: List[AnyVal] = list1.map({ case a: Int => a * a case _ => }) println(s"list2: ${list2.mkString(",")}") // list2: 1,4,16,(),() //Collecti => filter + map can do the combination of filtering and map val list3: List[Int] = list1.collect({ case a: Int => a * a }) println(s"list3: ${list3.mkString(", ")}") // list3: 1, 4, 16
Application of Partial Function: Reorganization of Map Data and Return New Map
val map: Map[Int, (Int, Int)] = Map(1 -> (2, 20), 10 -> (20, 30), 20 -> (30, 40)) val map1 = map.map({ //This is the representation of some applied functions. case (k, (_, v)) => (k, v) }) println(s"map1: (${map1.mkString("), (")})") // map1: (1 -> 20), (10 -> 30), (20 -> 40)