Scala再探
- 2. 函数式风格
拒绝变量。 For (int I = 0; I < index; i++);
控制结构。拒绝 continue,break;
List 与递归
- 3. 函数是头等对象
def method(a: Int, b: Int): Int = {
return a + b;
}
var meathod = (a: Int, b: Int) => {
a + b;
}
var meathod2 = (x: Int) => ((b: Int) => meathod(x, b))
var meathod3 = meathod2(100);
println(meathod3(10));
策略模式
- 5. 闭包
依照这个函数字面量在 def bindVar() = {
var i : Int = 5;
运行时创建的函数值(
对象)被称为闭包( def plusI() = {
i += 1;
closure)。名称源自于
}
通过“捕获”自由变量的
绑定,从而对函数字面 def minusI() = {
i -= 1;
量执行的“关闭”行动。 }
}
- 6. LIST高阶方法
Map,floatMap,foreach
Filter,partition,find,takeWhile,dropWhile,span
Forall, exist
List(1,2,3,4,5) filter (_ %2==0)
Result : List(2,4)