Chapter12 Scala operator

Source: Internet
Author: User

1. Identifiers
    • The variable name, function name, class name, and so on are collectively referred to as identifiers , and Scala can use any character as an identifier, for example ()!#%&×+-/:<>[email protected]\^|~ .

    • Any sequence of characters can be used in anti-quotes .

    val √ = scala.math.sqrt _    √(2)    val `val42    println(`val`)
2. Central operator
    • The operator is connected to two parameters, respectively, on either side of the operator.

    • a 标识符 bEquivalent to a.标识符(b) .

    1to10    1.to(10)    1-> 10    1.->(10
3. Unary operators
    • An operator with only one argument becomes a unary operator , and the position of the operator may be before or after the argument, all in the following two cases.

    • a 标识符Equivalent to a.标识符() , 标识符 a equivalent to a.unary_标识符 .

    1 toString    1.toString()    a42    -a    a.unary_-
4. Assignment operators
    • The assignment operator is in the form of an 操作符= expression a 操作符= b , which is equivalent to a = a 操作符 b .
    a1    aa1
    • <=、>=、!=、==、===、=/=And so is not an assignment operator,
5. Priority level
  • The precedence of the post operator is lower than the middle operator, which is a 中置操作符 b 后置操作符 equivalent to (a 中置操作符 b) 后置操作符 .

  • The symbol priority is shown in the following table:

    operator Priority Level
    Operators except for the following characters 1 (highest)
    * / % 2
    + - 3
    : 4
    < > 5
    != 6
    & 7
    ^ 8
    | 9
    Assignment operator 10 (lowest)
6. Adhesion
    • Scala except the colon operator and the assignment operator are right-associative , and all other operators are left- associative .
    // 构造列表List    12Nil   // :: 右结合    1 :: (2Nil)    (12Nil// 错误
    • The second parameter of the right combination is the method, for example, 2::Nil equivalent to Nil.::(2) .
7. Apply and Update methods
    • The function or method is located to the left of the equal sign of an assignment statement , and is called by the Update method , otherwise called by the apply method .
    val scores = new scala.collection.mutable.HashMap[String, Int]    scores("Bob"100     等价于调用:scores.update("Bob"100)     val bobsScore = scores("Bob")    等价于调用:scores.apply("Bob")
8. Extractor unapply
    • Extractor: an object with a unapply method that can be used as an inverse of the method used by the associated object.

    • Unapply accepts an object from which to extract values.

     class fraction(n:int, D:int) {      Private ValNum:int =if(d = =0)1 ElseNPrivate ValDen:int =if(d = =0)0 ElseDdef* (other:fraction) =NewFraction (num * other.num, den * other.den)}//Unapply returns the two values that are constructed for the object     object fraction {      defApply (N:int, d:int) =NewFraction (n, D)defUnapply (input:fraction) =if(Input.den = =0) NoneElseSome ((Input.num, Input.den))} Object Main extends App {        varFraction (A, b) = fraction (3,4) * FRACTION (2,5) println (a) println (b)}
    • Each sample class automatically has the Apply method and the Unapply method, which is explained in the following section of the sample class, as long as you understand it.

    • The extractor can only test the input and not really extract its value, only returns a Boolean.

9. Unapplyseq method
    • Use the Unapplyseq method to extract a sequence of values of any length , which returns a Option[seq[a]], and A is the type to be extracted .
    // Name 提取器可以产生所有组成部分的序列    // 关于 Option,Some,None后面章节会详细讲解,这里只需了解upapplySeq的作用就可以。    object Name {      def unapplySeq(input: String): Option[Seq[String]] =        if""Noneelse Some(input.trim.split("\\s+"))    }

adjourned

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Chapter12 Scala operator

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.