Scala: Short form and placeholder syntax for function text

Source: Internet
Author: User
Tags filter

Short form of function text

Scala provides a number of ways to remove redundant information and write the function text more briefly. Pay attention to these opportunities because they allow you to get rid of the clutter in your code.

One way to make the text of a function shorter is to remove the parameter type. Therefore, the previous example with a filter can be written like this:

scala> someNumbers.filter((x)  => x > 0)
res7: List[Int] = List(5, 10)

The Scala compiler knows that x must be an integer, because it sees that you immediately use this function to filter the list of integers (implied by somenumbers). This is known as target typing: target typing, because the object of the expression-the parameter of Somenumbers.filter () in this case-affects the type of expression-this example determines the type of the x parameter. The precise details of the target type are not important. You can simply start by writing a function literal with no parameter type, and if the compiler is not recognized, add the type. After a few times you have feelings about what the compiler can or cannot solve the puzzle.

The second way to remove unwanted characters is to omit the parentheses that are outside the inferred arguments. In the preceding example, the brackets on both sides of the X are not required:

scala> someNumbers.filter(x => x > 0)
res8: List[Int] =  List(5, 10)

PLACEHOLDER syntax

If you want to make the text of the function more concise, underline as a placeholder for one or more arguments, as long as each parameter appears only once in the function text. For example, _ > 0 is a very short callout for a function that checks whether the value is greater than 0:

scala> someNumbers.filter(_ > 0)
res9:  List[Int] = List(5, 10)

You can think of an underscore as a "blank" that needs to be "filled in" in an expression. This space is filled in with the parameters of the function every time the function is called. For example, because Somenumbers is initialized to the value list ( -11, -10, -5, 0, 5,) on page 113th, the filter method replaces the spaces in _ > 0 first with-11, like -11 > 0, and then replaces with-10, such as -10 > 0, then use-5, like -5 > 0, so that until the last value of the list. Therefore, the function text _ > 0 is the same as a slightly more verbose x => x > 0, as demonstrated below:

scala> someNumbers.filter(x => x > 0)
res10: List[Int] =  List(5, 10)

Sometimes when you take an underscore as a placeholder for a parameter, the compiler may not have enough information to infer the missing parameter type. For example, suppose you just write _ + _:

scala> val f = _ + _
<  console>:4: error: missing parameter type for expanded
function ((x$1,  x$2) => x$1.$plus(x$2))
 val f = _ + _
 ˆ

In this case, you can use a colon to specify the type, as follows:

scala> val f = (_: Int) + (_: Int)
f: (Int, Int) => Int = < function>  
scala> f(5, 10)
res11: Int = 15 

Note that _ + _ will be extended to function text with two parameters. This is also the reason why you can use this short form only if each parameter appears at most once in the function text. Multiple underscores refer to multiple parameters instead of being reused for individual parameters. The first underscore represents the first argument, the second underlines the second, the third ..., and so on.

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.