Programming in Scala Reading Note 7

Source: Internet
Author: User
Function and closure 1 member Method

Java functions exist. The way to append a function to a class is to append a method to it.

 

2 Local Method

The local method exists in a method.

If one method in a class is private and only one method is used, this method can be defined as a local method.

 

3. function-based first-class citizens

Methods include:

1 method name def add

2. method parameters (A: int, B: INT)

3 value assignment =

4 method body A + B

In fact, (A: int, B: INT) => A + B constitutes a basic function, but its name is "add, so we can call a function like this:

Add (1, 2), return value is 3

You can also call this function as follows:

(A: int, B: INT) => A + B) (1, 2), the return value is also 3

 

Now it should be relatively clear. We can assign a function to a Val variable, so that this variable is a function, such:

Val addition = (A: int, B: INT) => A + B

Then addtion (11, 12) is equal to 23.

 

In addition, the method can be used as a function parameter. For example, the foreach method of list requires a method as a parameter.

Val list = List (1, 2, 3, 4, 5)

List. foreach (Element => println (element + 1))

Another filter method,

Val evenlist = List. Filter (Element => element % 2 = 0)

 

4 more concise

The simplified method is provided as shown in the red text above, and the complete format is actually list. foreach ((Element: INT) => println (element + 1))

We have no reason to write such a complete thing unless the compiler cannot determine the type of this method parameter.

 

There are more concise methods,Placeholder Syntax:

Use this example: Val evenlist = List. Filter (Element => element % 2 = 0)

In this example, there is only one parameter and it appears once in the method body. It can be abbreviated as a placeholder:

Val evenlist = List. Filter (_ % 2 = 0), That is, replace each element.

 

It can also be like this: Val threeparamadd = (_: INT) + (_: INT)

This also occurs once in the method body. The first parameter replaces the first _, the second parameter replaces the second _, and so on...

 

We try to omit the parameter type, knowing that the compiler cannot identify our parameter type for us.

 

Partially Applied Functions (partial (incomplete) Application (provided) function ):

List. foreach (x => println (x ))RewriteList. foreach (println _)

_ Represents not only a single parameter, but also all parameters.

When a method is not provided with enough parametersA space with an underscore, Such as println _

 

Def println (X: Any) = console. println (X)

Is the method in the predef class. We only provide one println _. This _ represents all parameters in this method.

 

Def sum (A: int, B: int, C: INT) = A + B + C

If we give him a variable, we can write it like this.

Val threeparamadd = sum _ partial application functions

Or

Val threeparamadd = (_: INT) + (_: INT) placeholder

Or

Val threeparamadd = (A: int, B: int, C: INT) => A + B + C

 

Back to Val threeparamadd = sum _. When this threeparamadd is called, for example, threeparamadd (1, 2, 3), the compiler will see sum _, then he knew to go to the sum function and found that there were three parameters, which were replaced in sequence to complete the calculation.

There will be other situations, such:

Val oneaddsomeaddthree = sum (1, _: int, 3). In this case, our oneaddsomeaddthree can only have one parameter.

 

If a function is required as a parameter to complete a function, and some application functions can be used for this function, the underline can be omitted at this time.

For example, list. foreach (println _) can be modified to list. foreach (println)

Because the compiler knows that the println we write is a function, rather than something else, it will regard it as a part of the application function.

 

5 Closure

A function that references a free variable. An entity composed of this free variable is called a closure.

Free variables, except function parameters and variables declared in the function.

Bind variables, function parameters, and variables that can be determined during compilation of other functions.

 

Closed term: Closed term, which does not contain free variable function text.

Open term: open term, which contains the function text of free variables.

 

6. Special function call forms

1. Repeated parameters:

Def echo (ARR: string *) {arr. foreach (println _)}

Arr: string * is the scala syntax, similar to string... some in Java.

* Indicates that this parameter is an array. The array element type is string, and the array length is the number of strings given when the method is called.

 

If you want to call an array using the preceding method, for example:

Val some = array ("This's", "little", "wonderful"). If it is called directly, an error occurs. Echo (some)

In this case, the parameter echo (some: _ *) needs to be passed as follows :_*)

The compiler will pass every element in some to the method as a parameter.

 

2. Named parameters:

Def speed (distance: float, time: INT) = distance/Time

Speed (100, 10)

Speed (distance = 100, time = 10)

Speed (time = 10, distance = 100)

 

3. Parameters with default values

Def namebreaker (Name: String = "voctrals Lou", breaker: String = "") = Name. Split (breaker)

If we call namebreaker (), array ("voctrals", "lout") is returned ")

If we provide the value, the default value will no longer work, as shown in figure

Namebreaker (breaker = "_"), returns array ("voctrals Lou ")

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.