//1类似于lambda表达式的函数直接量======================
var get = (name: String) => {
println(123 + name)
}
get("456")
//2高阶函数:func为传入的参数(String为func的参数类型,Unit为func的返回类型)======================
def get1(func: (String) => Unit, name: String) {
func(name)
}
def func1(x: String) = {
println(123 + x)
}
get1(func1, "p")
//3高阶函数, 返回一个函数======================
def get2(num: Int = 123): (String) => Unit = {
(x1: String) => println(num + "+" + x1)
}
//解法1
var ret_func = get2(666)
ret_func("2k")
//解法2,currying函数概念(使用了“闭包”就是变量num)
get2(666)("3k")
println((1 to 5).map(_ + 1)) //对每个元素+1操作
println((1 to 5).filter(_ % 2 == 0)) //过滤
println((1 to 5).reduce(_ + _)) //累加
println((1 to 5).sortWith(_ > _)) //大的数排在左边
//return的用法======================
def get3(x: Int): Int = {
def get4(x: Int): Int = {
return 1 + x
}
get4(x) + 3
}
get3(2)