[Translation] differences between Scala Chinese methods and functions

Source: Internet
Author: User

What are the differences between functions and methods in Scala?

A method can appear as a part of an expression (call a function and pass a parameter), but a method (with a parameter) cannot be used as the final expression,

However, the function can be used as the final expression:

Scala> // define a method Scala> def M (X: INT) = 2 * XM: (x: INT) intscala> // define a function Scala> Val F = (X: INT) => 2 * XF: int => Int = <function1> Scala> // The method cannot appear as the final expression Scala> m <Console>: 9: Error: Missing arguments for method m; follow this method with '_' if you want to treat it as a partially applied function m ^ Scala> // the function can appear as the final expression Scala> fres9: int => Int = <function1>

The method without parameters can be used as the final expression. In fact, this is a method call. Scala stipulates that the brackets can be omitted for calls without parameters.

(We will discuss the method call below)

scala> def m1()=1+2m1: ()Intscala> m1res10: Int = 3

 

The parameter list is optional for methods, but mandatory for Functions

The method can have no parameter list, or the parameter list can be empty. However, the function must have a list of parameters (or it can be empty). See the example below.

Scala> // The method can have no parameter list Scala> def m2 = 100; M2: intscala> // The method can have an empty parameter list Scala> def m3 () = 100m3: () intscala> // The function must have a parameter list; otherwise, scala> var F1 ==> 100 <Console>: 1: Error: illegal start of simple expression var F1 ==> 100 ^ Scala> // the function can also have an empty parameter list Scala> var F2 = () => 100f2 :() => Int = <function0>

So why does the method have no parameter list? Let's look down.

 

The method name indicates a method call. The function name only indicates the function itself.

Because a method cannot exist as a final expression, if you write a method name without parameters (No parameter list or parameter)

This expression means: Call this method to obtain the final expression. Because the function can be used as the final expression. If you write down the function name

The call does not happen. the method itself will be returned as the final expression. to force a function to be called, you must write () after the function name ()

Scala> // This method does not have a parameter list Scala> m2res11: Int = 100 Scala> // This method has an empty parameter list Scala> m3res12: int = 100 Scala> // get the function itself, and the function call Scala> f2res13: () => Int = <function0> Scala> // call the function Scala> F2 () res14: Int = 100

 

Why can we provide a method where the function appears?

Many advanced functions in Scala, such as map () and filter (), require a function as a parameter. But why can we provide a method?

? As shown below:

Scala> Val mylist = List (, 72) mylist: list [int] = List (3, 56, 1, 4, 72) Scala> // map () the parameter is a function Scala> mylist. map (x) => 2 * X) res15: list [int] = List (6,112, 2, 8,144) Scala> // try to map () function provides a method as the parameter Scala> def M4 (X: INT) = 3 * xm4: (x: INT) intscala> // execute Scala> mylist normally. map (M4) res17: list [int] = List (9,168, 3, 12,216)

 

This is because if we provide a method where a function is expected to appear, the method will be automatically converted to a function. This action is called ETA expansion.

In this way, using functions will become much simpler. You can verify the behavior according to the following code:

Scala> // where the function is expected to appear, we can use Scala> Val F3 :( INT) => Int = m4f3: int => Int = <function1> Scala> // where the function is not expected to appear, the method is not automatically converted to the function Scala> Val V3 = M4 <Console>: 8: Error: missing arguments for method M4; follow this method with '_' if you want to treat it as a partially applied function Val V3 = M4 ^

With this automatic conversion, we can write very concise code, as shown below

Scala> // 10. <interpreted as obj. method, that is, the integer <method. Therefore, this expression is a method and will be interpreted as a function Scala> mylist. filter (10. <) res18: list [int] = List (56, 72)

This is because operators in Scala are interpreted as methods.

  • Prefix OPERATOR: op obj is interpreted as obj. Op.
  • Infix OPERATOR: obj1 op obj2 is interpreted as obj1.op (obj2)
  • Suffix OPERATOR: OBJ op is interpreted as obj. Op.

You can write 10 instead of 10. <

scala> myList.filter(10<)warning: there were 1 feature warning(s); re-run with -feature for detailsres19: List[Int] = List(56, 72)

 

How to forcibly convert a method into a function

You can add an underline to the method name to force the function to be converted into some application functions.

scala> val f4 = m4 _f4: Int => Int = <function1>scala> f4(2)res20: Int = 6

 

Passing the name parameter is a method

Passing a name parameter is actually a method without a parameter list. That's why you can use a name instead of adding ()

Scala> // use 'X' twice, which means that Scala> def M1 (X: => INT) = List (x, x) M1 is called twice: (X: => INT) list [int] Scala> Import util. randomimport util. randomscala> Val r = new random () R: Scala. util. random = [email protected] Scala> // because the method is called twice, the two values are not equal. Scala> m1 (R. nextint) res21: list [int] = List (-1273601135,200 4676878)

If you cache the name passing parameter (function) in the method body, you cache the value (because the X function is called once)

Scala> // cache the Function Represented by the pass-through parameter Scala> def M1 (X: => INT) = {Val y = x; List (Y, Y)} M1: (X: => INT) list [int] Scala> m1 (R. nextint) res22: list [int] = List (-1040711922,-1040711922)

 

Is it possible to reference the method represented by the name passing parameter in the function body (the cached method is represented by the name passing parameter ).

scala> def m1(x: => Int)={val y=x _;List(y(),y())}m1: (x: => Int)List[Int]scala> m1(r.nextInt)res23: List[Int] = List(-1982925840, -933815401)

[Translation] differences between Scala Chinese methods and functions

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.