Scala Classroom (2): Foundation (i)

Source: Internet
Author: User

<p> <strong style= "color: #555555;" > Here we reprint <a style= "Font-weight:inherit; Font-style:inherit; Color: #0085cf; " >twitter's Scala classroom </a> , reproduced in the basic content of the Twitter-based Scala classroom Chinese translation, some small changes .</strong></p><p> < strong> expression </strong></p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> 1 + 1res0:int = 2</pre><p> Res0 is the variable name automatically created by the interpreter, used to refer to the computed result of the expression. It is of type int with a value of 2. </p><p> Scala (almost) everything is an expression. </p><p> <strong> values </strong><br/> You can give the result of an expression a name that is assigned to a non-variable (val). </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> val 1 + 1two:int = 2</pre><p> <strong> variable </strong>&lt ;br/> If you need to modify the binding of this name and result, you can choose to use Var</p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> var name =" Steve "name:string = stevescala> name =" Marius "name:string = marius& lt;/pre><p> <strong> function </stronG><br/> You can create a function using def. </p><pre class= "Brush:scala; Title:; Notranslate "Title=" ">scala> def AddOne (m:int): Int = m + 1addOne: (m:int) int</pre><p> in Scala, you need to The function parameter specifies the type signature. </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> val three = AddOne (2) Three:int = 3</pre><p> If the function has no arguments, you can not write parentheses. </p><pre class= "Brush:scala; Title:;  Notranslate "Title=" ">scala> def three () = 1 + 2three: () intscala> three () Res0:int = 3scala> Threeres2:int = 3</pre><p> <strong> Anonymous functions </strong><br/> You can create anonymous functions. </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> (X:int) + x + 1res3:int = Int = <function1></pre><p> this letter The number is an int variable named x plus 1. </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> res3 (1) res4:int = 2</pre><p> You can pass anonymous functions or save them as invariants. </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">scala> val addone = (x:int) + x + 1addone:int = Int = <function1>scala> A Ddone (1) res5:int = 2</pre><p> If your function has many expressions, you can use {} to format the code to make it easy to read. </p><pre class= "Brush:scala; Title:; Notranslate "title=" ">def Timestwo (i:int): Int = {println (" Hello World ") I * 2}</pre><p> for anonymous functions and so on. Lt;/p><pre class= "Brush:scala; Title:;   Notranslate "title=" >scala> {i:int = |   println ("Hello World") | I * 2 | }res6:int = = <function1></pre><p> When an anonymous function is passed as a parameter, this syntax is often used in </p>

  

Scala Classroom (2): Foundation (i)

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.