Scala's biased application function

Source: Internet
Author: User
Tags foreach

Although the underscore in the previous example replaces only a single argument, you can also use an underscore to replace the entire argument list. For example, write println (_), or better yet you can write println _. Here is an example:

someNumbers.foreach(println _)

Scala sees this short form as directly as you enter the following code:

someNumbers.foreach(x => println(x)) 

Therefore, the underscore in this example is not a placeholder for a single argument. It is a placeholder for the entire argument list. Remember to leave a space between the function name and the underscore, because not doing so the compiler would assume that you are stating a different symbol, for example, a method called Println_ that does not seem to exist.

When you use underscores in this way, you are writing a partial application function: Partially applied function. In Scala, when you call a function and pass in any of the required arguments, you are applying the function to the parameter. For example, given the following functions:

scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (Int,Int,Int)Int  

You can apply the sum of the functions to the parameters 1,2 and 3, as follows:

scala> sum(1, 2, 3)
res12: Int = 6 

The partial application function is an expression that you do not need to supply all the parameters required by the function. Replace with only parts, or do not provide the required parameters. For example, to create a biased application expression that does not provide any of the three required arguments, just place an underscore after "sum". You can then deposit the resulting function into a variable. Examples are as follows:

scala> val a = sum _  
a: (Int, Int, Int) => Int = < function>  

With this code, the Scala compiler uses the partial application function expression, sum _, instantiates a function value with three missing integer arguments, and assigns the index of the new function value to variable a. When you apply this new function value to three parameters, it turns back to call sum and passes in the three parameters:

scala> a(1, 2, 3)
res13: Int = 6

What actually happens is this: the variable named a is pointing to a function value object. This function value is an instance of a class that is automatically generated by the Scala compiler according to the biased application function expression sum _. The compiler produces a class that has a apply method with three parameters. The resulting class extends the attribute Function3 and defines the Apply method for three parameters. Three parameters are taken because the sum _ expression is missing a number of three parameters. The Scala compiler translates expression A (1,2,3) into a call to the Apply method of the function value, passing in three parameter 1,2,3. So a (1,2,3) is a short form of the following code:

scala> a.apply(1, 2, 3)
res14: Int = 6 

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.