As we all know, Scala is a functional programming language of the Geek type, and the features supported include:
- The function has a "class citizen" status;
- Support for anonymous functions (volume of function numbers)
- Supports higher order functions
- Support closures
- Partial application functions
- Currying
The first point to note is that there are two forms of methods and function objects in Scala, which are functions defined by the DEF keyword, whereas function objects are converted by means of a method, or lambda is assigned a value.
1. Start with "class citizen"
Many people who have a slight understanding of functional programming may have heard of the phrase "A-class citizen," but few have been able to articulate exactly what a "one-class citizen" is. Here I make an analogy you'll see right away: In reality, what kind of person can be considered a first-class citizen? First, he must be an independent individual -a person who relies on his parents or friends to survive must not be regarded as a citizen, let alone a first, and secondly, the person has to have enough freedom -both on the envisioned and the next five oceans to catch the turtle, before they can be first-class citizens. Corresponding to our functional programming, we can summarize a few points:
(1) function definition and invocation do not depend on other structures, such as C, Python, JS, Scala, and the reverse is typically Java, because any function of Java (methods) must be defined in the class, interface, enumeration (in fact, also the class), and any method call through the object, The static method or interface of the class (JDK 1.8), the method cannot be called directly, it must be attached to other structures to exist. So in this case the function is definitely not "a citizen".
(2) functions can be used as parameters of functions, return values, and can be variable assignment of functions, and the definition of the function is extremely free, any code block can also define the function.
Now let's look at Scala, which perfectly fits all of the above requirements (but note that in addition to the script-style Scala, other Scala programs can only be included in class or object), and Scala's functions support the definition inside the function, and functions defined with a lambda expression can be assigned to any variable, constant, and all functions can be used as return values, parameters.
2. The learning of lambda expressions
Many of the Scala beginners are on the Scala lambda, because Scala lambda is so versatile that you may not be able to read it many times. Let's start with the basics:
The most basic:
Val fun = (a:int) + = {A < && a > 0}
When the parameter defined by R is a function:
def fun1 (f:string = Unit) = f ("Wangyalou")
We can easily use lambda to pass in the required functions:
FUN1 ((s:string) =>{println (s)})
Note that the average person does not write this, because lambda as a parameter can ~ ~ Jane ~ Write ~~~~~ Ready I'm going to start! First, omit the type arguments that can be inferred:
FUN1 ((s) =>{println (s)})
When there is only one parameter, the () before,=> () can be saved:
FUN1 (S=>{println (s)})
can also be simplified, Scala can be used instead of only one occurrence of the parameters:
Fun1 (println (_)) or Fun1 (println _)
Finally, we can not even underline:
FUN1 (println)
Note that in the final case we took advantage of the "ETA transform"that the compiler supports lambda, that is, when the expression has only one parameter, and the entire execution part is a function call, you can write the name of the functions directly
An ETA extension (eta-expression) is another thing, referring to the process of converting a normal method into a function object:
Val b= Too (_,_,_) val b= too _ =>int = foo
Where too is a method with a parameter of 3 int
However, too (_,_,1) must not be an ETA extension!
The use of underscores is profound, and here are some examples:
Example 1:lambda as a parameter, _ can replace the one-time parameter, and omit "="
Val nums = Array (1,2,3,4) Nums.filter (_>2)
= Array (3, 4)
Example 2:lambda as a parameter, _ can replace the one-time parameter, and omit "="
scala> def foo (f: (Int,int) =>int) (A:int,b:int) = f (A, B)
Foo: (f: (int, int) = = int) (a:int, b:int) int
scala> foo (_+_) (3,4)
Res33:int = 7
Example 3:lambda as a function definition, _ can replace the one-time parameter, and omit "=", but then add the type, because there is no way to infer the type of "_"
Val B = (_:int) + (_:int)
B (+)
Operation Result:
Res32:int = 3
3. Partial application function (partial function)
An example is sufficient to make it clear that:
Scala> def foo (a: int, b: int, C: Booleanifelse(A:int, B:int, C:boolean)Int
Scala> Val foom = foo (_: int, _: int, false)(int, int) + int = <function2>
Similar to the partial function in Python, where a parameter is determined, the other parameters are replaced with "_" and indicate its type, be sure to indicate the type Ah!!! Otherwise it will be a case of ETA extension failure!!