大数据spark蘑菇云行动前传第5课:零基础彻底实战Scala函数式编程及Spark源码解析
1 零基础彻底实战Scala函数式编程入门
2 Spark源码中的scala函数式编程鉴赏
package com.dt.spark.scala.bascis
object HelloFunctionPograming {
def main(args: Array[String]): Unit = {
val age=helloword("spark", 80)
println(age)
// println("=========== "+fab(100))
helloword("spark")
helloword(age=31,name = "hadoop")
println("========== "+sum(1,2,3,4,5,6))
println("========== "+sum(1 to 6 : _*))
println("===sumrecursive==="+ sumrecursive(1 to 6 : _*))
}
def helloword (name:String,age:Int =30) = {
println("hello,my name is " + name +age)
//ages
100
}
def fab(n:Long):Long ={
if (n <=1) 1
else fab(n-2) + fab(n-1)
}
//可变参数,变长参数
def sum (numbers:Int * )={
var result =0
for (number <- numbers) result +=number
result
}
def sumrecursive(numbers:Int*):Int={
if (0 == numbers.length) 0
else numbers.head + sumrecursive(numbers.tail:_*)
}
}
运行结果
hello,my name is spark80
100
hello,my name is spark30
hello,my name is hadoop31
========== 21
========== 21
===sumrecursive===21