Scala Learning Notes 38-method naming conventions and for expressions

Source: Internet
Author: User
Tags naming convention

Method Naming conventions

A method naming convention was previously mentioned in the section "Operator overloading": The first character of the method determines the priority of the method. Now say another naming convention: If the method ends with a colon (:), the call target is the instance following the operator.

For example, the following:

class Cow {  ^ (moon:moon) = println ("Cow jumped over the moon")}class  Moon {  ^:(Co W:cow) = println ("This Cow jumped over the moon too"newnew^  ^: Moon

Take a look at the results:

In addition to the: End operator, a set of operators is also targeted at subsequent instances, including unary operators + 、-、! and ~. Take a look at the following example:

class Sample {  def unary_+ = println ("called Unary +")  def unary_-= println ("Called unary-") 
    def Unary_! = println ("called Unary!") )  def unary_~ = println ("called Unary ~"new  sample+Sample- Sample! Sample~sample

The unary operator + is mapped to a call to unary_+ (), and a unary operator-mapped to unary_-(), and so on. (Scala has rules for method names, and operators are not allowed to be placed after characters such as letters, numbers, unless the operator is prefixed with an underscore.) In other words, the method cannot be called Jumpover: (), but can be called Jumpover_: ())

Look at the results of the execution:

for-expression

There is a built-in foreach method in the container, as long as you provide the required function value for the Foreach method. If you want to control loops, or if you want to manipulate multiple containers, use an external iterator. Look at an example:

 for (I <-1 to 3) {  print ("ho")}

The above expression we have seen several times, it will print "Ho ho ho" such a string. In fact, this expression is shorthand for the general syntax of the following expression:

 for ([Pattern <-generator; definition*]+; filter*)      [yield] expression

The parameters received by the for expression include one or more generators (generator), 0 or more definitions (definition), and 0 or more filters (filter). These things are separated from each other by semicolons. The yield keyword is optional, and if it exists, it means that the expression returns a set of values instead of a unit.

To see the use of yield, here's an example:

 for (I <-1 to 3)    2 for (I <-1 to 3) 2println(RESULT1) println     (RESULT2)

The results of the implementation are as follows:

Then look at the filter, for example, only the dual number executes the function value:

 for if i% 2 = =0  )* 2println (RESULT1)

Take a look at the results:

Take a look at the variable definition:

 for (I <-1 to 3; j=i+1)   * 2println (RESULT1)

Execution Result:

Finally, take a look at the use of multiple generators:

 for (I <-1 to 3; J <-2 to 4)   + "," + j)

Execution Result:

#########

Scala Learning Notes 38-method naming conventions and for expressions

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.