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