"Turn" Scala FAQ: Partial function and partial application function

Source: Internet
Author: User

Original link http://www.cnblogs.com/nixil/archive/2012/05/16/2503722.html

Scala FAQ: Partial function and partial application function

Scala has a concept of partialfunction and a concept called partial applied Function. The former translated partial function, the latter translated "partial application Function" or "Partial application function", the difference between the word is very big.

First, the partial function is a mathematical concept, the partial function is not a "function", but a concept parallel to the function. The definition can refer to wiki http://zh.wikipedia.org/wiki/%E5%87%BD%E6%95%B0, which means that there may be some values in the definition field x that do not have a corresponding value in the domain Y.

Scala can define partial functions through pattern matching, and the functions defined in either of these two ways can be thought of as partial functions because they all only deal with the partial value of the domain int they define.  So, like P1. The additional benefit of defining a partialfunction is that you can use a Isdefinedat method before the call to verify that the parameter is processed. Or use a OrElse method at invocation, which accepts another partial function that defines what to do if the parameter is not captured by a partial function. That is, a statement that can be displayed. It is best to use partialfunction in the actual code to declare that you really want to define a partial function, not what is missing.

    def p1:partialfunction[int, Int] = {        1 } def p2 = (x:int) +1 }    

And part of the application function, refers to a function has n parameters, and we provide it with less than n parameters, then we get a partial application function.

Like I'm going to define a function.

Then it is possible to derive a partial function from this function:

def p_sum = SUM (1, _:int, _:int)

So you can call P_sum (2,3), which is equivalent to calling sum (6) to get the result. Here the two _ corresponds to the function sum corresponding to the position of the parameter. So you can also define

What's the use of this thing? One is when you need to call a function more than once in your code, and one of the arguments is always the same, so you can use this to knock down some code. What about the other one?

"Turn" Scala FAQ: Partial function and partial application function

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.