Liaoliang "DT Big Data Dream Factory" fourth talk for and function advanced combat, lazy use

Source: Internet
Author: User

Liaoliang "DT Big Data Dream Factory" fourth talk for and function advanced combat, lazy use

Do you want to know big data, do you want to be a yearly salary million? So what are you waiting for, come on! Follow Liaoliang's teacher to learn spark big data

The use of the for and function advanced combat, lazy

For loop

Object for_function_advanced{

def main (args:array[string]): Unit ={

for (I <-1 to 2;j <-1 to 2) print ((100*i + j) + "")

Printin

for (I <-1 to 2;j <-1 to 2 if I!=j) print ((100*i + j) + "")//Add conditional expression

Printin

Function

def addA (x:int) = x +100

Val add = (x:int) = x+200\\ anonymous function

Printin ("The result from a function is:" + AddA (2))

Printin ("The result from a Val is:" + Add (2))

Scala Features: function is worthwhile; can be passed as a function parameter

Def FAC (n:int): Int = if (n <= 0) 1 Else n * FAC (N-1)

Printin ("The result from a FAC is:" + FAC (10))

Recursive algorithm function to have return value type

function parameter Default value

def combine (content:string, left:string = "[", right:string = "]") =left + content +right

Printin ("The result from a combine are:" +combine ("I Love Spark")

The parameters of the function are variable

def connected (args:int*) = {

var result =0

for (Arg <-args) result +=arg

Result

}

Printin ("The result from a connected is:" +connected (1,2,3,4,5))

Lazy use

Object Lazyops {

def main (args:array[string]): Unti = {

Lazy val File = Source.fromfile ("F:\\tuiguangneirong1.docx")

Printin ("Scala")

for (line <-file.getlines) Printin (line)

}

}

Lazy is instantiated the first time it is used, deferred execution, lazy execution, iterative computation of the same data processing

With a simple code example, learn from the basics about the for loop, and the way in which the For loop is used (with conditional expressions) and function basic (recursive algorithm functions have return value types

And Scala's little features: functions are worthwhile, can be passed as parameters of functions, and lazy use. I hope I follow the teacher to learn 1.1 points of progress;

If you know it's unclear, you can also learn this video or record the contact information

Video Address: http://www.tudou.com/programs/view/ge646hSN8tc/

Study Address: http://mp.weixin.qq.com/s?__biz=MzAwNjAwODI3Mg==&mid=212579488&idx=2&sn= 883193df2b3df163d49c4fdec1ecd585&scene=5#rd

Liaoliang "DT Big Data Dream Factory" fourth talk for and function advanced combat, lazy use

Related Article

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.