3. Fp
3.1. Functions
The status of the function is equal to the general variable, and can be used as a function parameter and as a return value.
Any input passed into the function is read-only, such as a string that is not changed, and only a new string is returned.
One of the problems in Java is that a lot of private methods are used only once, and there is no close integration with the way it is used; Scala can define functions inside functions, which solves the problem well. 3.1.1. function Definitions
Functions and methods are generally defined with Def, or you can define anonymous functions with Val, or define function aliases.
def m0 (x:int) = X*x
Val m1 = (x:int) = x*x//With (), () is required
Val m2 = {x:int=> x*x}//with {}, {} is required
M0 (10)//100
M1 (10)//100
M2 (10)//100
Functions that do not require a return value, you can use the Def f () {...} to return the unit forever (even if you use return), that is:
def f () {...} Equivalent to Def f (): Unit = {...}
For example:
def f () {return "Hello World"}
F ()//Unit, not "Hello World"
A function that needs to return a value, with Def f () = {...} or def f = {...}
def f () = {"Hello World"}
F ()//"Hello World"
def f = {"Hello World"}//F is an alias for anonymous function {"Hello World"}
F//"Hello World"
Three ways to define the difference:
def f () {return:} |
Call: F, f () can be |
Always returns: Unit |
def f () = ... |
Call: F, f () can be |
Returns the unit or value |
def f = ... |
Call: F |
Returns the unit or value |
Hint: Functional style--try to write functions with return values; As short as possible (the function body does not use {...} )
3.1.2. Mapping Definitions
A special definition: mapping definition (directly equivalent to the mapping relationship in mathematics);
It can also be thought of as a function without parameters, returning an anonymous function, and invoking the returned anonymous function.
Example 1:
def f:int=>double = {//See Def F: (int=>double) = {...}
Case 1 = 0.1
Case 2 = 0.2
Case _ = 0.0
}
F (1)//0.1
F (3)//0.0
Example 2:
def m:option[user]=>user = {
Case Some (x) + = X
Case None = null
}
M (o). Getorelse ("None ...")
Example 3: (Multi-->1)
def m: (int,int) =>int = _+_
M (2,3)//5
Example 4:
def m:int=>int = 30+_, if the only "_" at the end, can be omitted
M (5)// 3.1.3. Special function name +-*/
The method name can be *:
def * (X:int, y:int) = {X*y}
* (10,20)//= 200
1+2, equivalent to 1.+ (2)
Defines a unary operator (pre-set) available Unary_.
Note: unary: Unary, single element, single component. Pronunciation: "' ju:ne ri"
-2, equivalent to: (2). unary_-//-2
+2, equivalent to: (2). unary_+//2
!true, equivalent to: (true). unary_! False
~0, equivalent to (0). unary_~//-1 3.1.4. Default parameters, named parameters
Note: Support starts from Scala2.8.
Please compare the readability of the following two types of wording:
General function calls |
named function call |
SendEmail ( "Jon.pretty@example.com", List ("recipient@example.com"), "Test Email", B Nil, nil, nil) |
SendEmail ( BODY = B, Subject = "Test Email", to = List ("recipient@example.com"), from = "Jon.pretty@example.com", Attachments = List (File1, file2)) |
Define and use:
def join (A:list[string], s:string= "-") = {a.mkstring (s)}
Join (List ("A", "B", "C"))//A-b-c
Join (List ("A", "B", "C"), ":")//A:B:C
To adjust the order of parameter calls:
Join (s= ":", A=list ("A", "B", "C"))//A:B:C
3.1.5. Function calls
def f (s:string = "default") = {s}
F//"Hello World"
F ()//"Hello World"
Ø the invocation of the object's parameterless method, which can be omitted. and ()
"Hello World" touppercase//"Hello World"
Ø the invocation of the 1 parameter method of the object can be omitted. and ()
"Hello World" indexOf w//6
"Hello World" substring 5//"World"
Console print 10//But cannot write print 10, only print (10), omit console.
1 + 2//equivalent (1). + (2)
Ø the invocation of multiple parameter methods of an object can also be omitted. But cannot omit ():
"Hello World" substring (0, 5)//"Hello"
Note:
A function that is not in class or object cannot be called this way:
def m (i:int) = I*i
M 10//Error
L But you can use this call in class or object:
Object Method {
<