3rd Lesson Scala Functional Programming thorough mastery and spark source reading notes

Source: Internet
Author: User

Contents of this lesson:
A thorough explanation of functional programming in 1:scala
Scala functional programming in the 2:spark source code
3: Cases and jobs

Functional Programming Begins:
def fun1 (name:string) {
println (name)
}

Assign a function name to a variable, then this variable is a function.
Val Fun1_v = fun1_

Visit Fun1_v ("Scala")

Result: Scala

anonymous function: Parameter name = = point to function body
Val fun2= (content:string) = println (content)
Fun2

Advanced functions:
One: The function parameter is also a function, as follows
Def bigdata (func: (String) =>unit,content:string) {//input parameter is String, return type is Unit
Func (content)
}

Bigdata (Fun1_v, "Spark")

Results Spark

Second: The return value of the function is also a function
def func_returned (content:string) = (message:string) + println (content + "" +message)
Val returned =func_returned ("Spark")
Returned ("Scala")
Result: Spark Scala

Three: Advanced functions in arrays
Array.map (2*_)

Four: After the function closure function is out of scope, it can still access
def scala (content:string) = (message:string) + println (content + ":" + message)
Val Funcresult=scala ("Spark")
Funcresult ("Flink")
Result Spark:flink

currying * * * *
def sum (x:int,y:int) = X+y
Into
def sum_currying (x:int) = (y:int) = X+y
Sum_currying (1) (3)
Results 4

Job: Count the total number of occurrences of all words under a folder (functional programming, knowledge points of the collection)

Object Helloscala {
def main (args:array[string]) {
Scanparseallfile ()
}

Def scanparseallfile () {
Val arrbuffer = arraybuffer[string] ()
var i = 0;
val files = (new File ("D:/wangs/lepus/src/com/huawei/lepus/activity/manager/ats")). Listfiles ()
For (file <-files) {
if (File.isfile ()) {
Val f = source.fromfile (file)
for (line <-f.getlines ()) {
Line.split ("[,:.! \\s?*\\/-=+] () >< ")
For (Key <-line.split ('. ')) {
i + = 1;
Arrbuffer + = key;
}
}
}
For (FileName <-arrbuffer) {
println (FileName)
}
}
}
}

3rd Lesson Scala Functional Programming thorough mastery and spark source reading notes

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.