For and function advanced combat, lazy usage notes Summary

Source: Internet
Author: User

Contents of this issue:
1:for Cycle of advanced combat, (more practical code)
2:function Advanced Combat, (function more definition and implementation mode)
Use of 3:lazy, (lazy loading)


For loops are commonly used:
for (I <-1 to 2, J <-1 to 2 if i!=j) {
println ((100*i+j) + "")
}


function functions:
Note: (especially important)
The function in 1:scala is always returned with a value, but you can not explicitly specify the return value of the function at declaration time, the last parameter in the code block as the return value of the Scala function.
2:scala because the function has a return value, the function can be passed as a function parameter.

function definition Mode 0: function with no return value
def sum (x:int) {
Val s=x+100
println (s)//println method its return value unit as the return value of the SUM function
}

function Definition Method 1: Declaring a function that does not explicitly specify a return type
def sum (x:int) ={
Val s=x+100
}
Parse the above code: Define a function named sum with the DEF keyword, which has a parameter x, whose type is int, pushed to by the type of Scala that can be automatically typed, which does not explicitly specify its return value type, ={} This block of code will be pushed to the automatic type.


function Definition Method 2: Declaring a function that specifies the return type
def sumall (x:int,y:int): int={
Val S=x+y
S
}
Parse the above code: This function specifies the type returned: int={}, specifying the type returned as Int


function definition Mode 3: anonymous function, function without name (especially important)
Val add = (x:int) = x+100
Parse the above code: left Val Add it is an immutable variable, right (x:int) = x+100 It is an anonymous function, the right function evaluates the result assigned to the left add this variable.
println ("result is:" +add (50))//Call print result is 150


Recursive method of function definition: function must have return value
def FAC (n:int): Int = if (n <= 0) 1 Else n * FAC (N-1)
Parse the above code: FAC This function must have a return value: Int, if there is no return value, the compilation will fail and error


The parameters of the function are mutable: that is, there are many arguments to the function, and the following code (args:int* and args:string*) indicates that multiple arguments can be passed
Demo1:
def content (args:int*) = {
var result = 0;
for (sum <-args) {
Result + = Sum
}
Result
}
Val sum = content (1, 2, 3, 4, 5, 6)
println ("result is:" + sum)

Demo2:
def lianjie (args:string*) = {
var result = "Hello:"
for (i <-args) {
Result + = i + ","
}
Result
}
Val str = Lianjie ("Spark", "Hadoop");
println ("result is:" + str)
The result is: Hello:spark,hadoop,

Lazy use: When a variable or constant is added to the lazy keyword modifier, it means that it is instantiated only when it is first used
Lazy val File = Source.fromfile ("D:\\hadssss.text")//This file does not actually exist, and if the lazy keyword is added, there will be no error.
println ("Scala")
for (line <-file.getlines ()) {
println (line)
//}

For and function advanced combat, lazy usage notes Summary

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.