Scala functions have the principle of simplicity and follow the style of saving what can be saved. This also makes us programmers who come into contact with Scala programming for the first time feel very confused when reading the program. The export is, I don't know what these bars mean. Therefore, the simplification principles of these functions are recorded for better memory.
The simplification principles of functions include the simplification principles of ordinary functions and anonymous functions. I will first introduce the simplification principles of ordinary functions, and then introduce the simplification principles of anonymous functions
Simplification principle of ordinary functions:
The standard function is written as follows
(1) Return can be omitted. Scala will use the last line of the function body as the return value
(2) If the function body has only one line of code, you can omit curly braces
(3) If the return value type can be inferred, it can be omitted (: omitted together with the return value type)
(4) If there is a return, the return value type cannot be omitted and must be specified
(5) If the function explicitly declares unit, even if the return keyword is used in the function body, it will not work
(6) Scala can omit the equal sign if it expects a type with no return value
(7) If the function has no parameters but declares a parameter list, parentheses can be added or not when calling
(8) If the function does not have a parameter list, the parentheses can be omitted and must be omitted when calling
(9) If you don't care about the name and only care about logical processing, the function name (def) can be omitted
//To simplify the principle, can save the province //(1)return Can be omitted, scala The last line of the function body is used as the return value def f1(s:String):String = { s+" cls" } //(2)If the function body has only one line of code, you can omit curly braces def f2(s:String):String = s+" cls" //(3)If the return value type can be inferred, it can be omitted(:Omit together) def f3(s:String) = s+"cls" var s = f3("dh ai ") println(s) //(4)If so return,Then the return value type cannot be omitted and must be specified def f4(s:String) :String = { return s+"cls" } //(5)If there is a function declaration unit,Then it is used in the function body return Keywords don't work either def f5(s:String):Unit = { return s+" cls" } val as = f5("dh ") println(as) //(6)If Wang's wife has no return value type, it can be omitted= def f6(s:String):String={ return s+" cls" } //(7)If the function has no parameters but declares a parameter list, parentheses can be added or not when calling def f7():String={ "cls" } val as1 = f7() val as2 = f7 println(as1 +" "+ as2) //(8)If the function does not have a parameter list, the parentheses can be omitted and must be omitted when calling def f8 = "cls" println(f8) //(9)If you don't care about the name, you only care about the function name(def)Can be omitted def f9 = (x:String)=>{println("wusong")} def f10(f:String=>Unit) = { f("") } f10(f9) println(f10((x:String)=>{println("wusong")}))
Principle between anonymous functions:
First, let's look at the writing of anonymous functions, as shown below,
(X: int) = > {function body}
(1) The type of parameter can be omitted and will be automatically deduced according to the formal parameter
(2) If only one parameter is found after the type is omitted, the parentheses can be omitted; Other situations: parentheses can never be omitted if there are no parameters and the parameters exceed 1.
(3) If an anonymous function has only one line, braces can also be omitted
(4) If the parameter appears only once, the parameter is omitted and the following parameters can be used_ replace
//Define a function:Parameters contain data and logical functions def operation(arr:Array[Int],op:Int=>Int)={ for (elem <- arr) yield op(elem) } //Define logical functions def op(ele:Int):Int = { ele+1 } //(3)Standard function call val arr = operation(Array(1,2,3,4),op) println(arr.mkString(",")) //(4)Using anonymous functions val arr1 = operation(Array(1,2,3,4),(ele:Int)=>{ ele+1 }) println(arr1.mkString(",")) //(4.1)The parameter type can be omitted and will be automatically derived according to the formal parameters var arr2 = operation(Array(1,2,3,4),(ele)=>{ ele+1 }) println(arr2.mkString(",")) //(4.2)If only one parameter is found after the type is omitted, the parentheses can be omitted; //Other situations:Parentheses can never be omitted if there are no parameters and parameters greater than 1 val arr3 = operation(Array(1,2,3,4),ele =>{ ele+1 }) println(arr3.mkString(",")) //(4.3)If an anonymous function has only one line, braces can also be omitted val arr4 = operation(Array(1,2,3,4),ele => ele+1) println(arr4.mkString(",")) //(4.4)If the parameter appears only once, the parameter can be omitted, and the following parameters can be used_replace val arr5 = operation(Array(1,2,3,4),_+1) println(arr5.mkString(" "))
Give an example of an anonymous function for you to better understand the simplified writing of anonymous functions
def calculator(a: Int, b: Int, op: (Int, Int) => Int): Int = { op(a, b) } // (1)Standard Edition println(calculator(2, 3, (x: Int, y: Int) => {x + y})) // (2)If there is only one line, braces can also be omitted println(calculator(2, 3, (x: Int, y: Int) => x + y)) // (3)The type of parameter can be omitted and will be automatically deduced according to the formal parameter; println(calculator(2, 3, (x , y) => x + y)) // (4)If the parameter appears only once, the parameter is omitted and the following parameters can be used_replace println(calculator(2, 3, _ + _))