流程控制
- if
var a = 3; if(a > 0){ println(s"$a > 0") }else{ println(s"$a < 0") } 3 > 0
- whilr
var b = 3; while (b < 10){ println(b) b += 1; } 3 4 5 6 7 8 9
循环
- 构造一个循环
var one :Range.Inclusive = 1 to 10 println(one) 结果: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) var one :Range.Inclusive = 1 to (10, 2) println(one) 结果: Range(1, 3, 5, 7, 9) var seqs = 1 until (10, 3) println(seqs) 结果: Range(1, 4, 7)
- 打印集合
var seqs = 1 until 10 for(i <- seqs){ print(i) } 结果: 123456789 // 循环体没有break,有守卫 var seqs = 1 until 10 for(i <- seqs if(i % 2 == 0)){ print(i) } 结果:2468
- 打印九九乘法表
for(i <- 1 to 9){ for(j <- 1 to 9){ if(j<=i) print(s"$i * $j = ${i * j}\t") if(j == 1) println() } } 简写: var num = 0; for(i <- 1 to 9; j <- 1 to 9){ num += 1 if(j<=i) print(s"$i * $j = ${i * j}\t") if(j == 1) println() } println(num) num 打印81次 性能优化: var num = 0; for(i <- 1 to 9; j <- 1 to 9 if(j<=i)){ num += 1 print(s"$i * $j = ${i * j}\t") if(j == 1) println() } println(num) num 打印45次
- 循环结果收集
val ints = for(i <- 1 to 10) yield i println(ints) 结果:Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val ints = for(i <- 1 to 10) yield { val num = 2; i * num; } println(ints) 结果: Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
函数
- 函数无返回值
var y = fun01() def fun01(){ println("fun001") } 结果:fun001 函数要被调用才能打印 () 无结果返回
- 函数有返回值,需要1.有定义
def fun002(): Int ={ 3 } var x = fun002(); println(x)
- 有参
def fun03(a:Int): Unit ={ println(a) }
- 递归
def fun04(num:Int): Int ={ if(num == 1){ num }else{ num * fun04(num - 1) } } val i = fun04(4) println(i) 结果: 24 return 4 * fun04(4 - 1) 解析: fun04(4 - 1) = 3 * 2 * 1 2 * fun04(2-1) fun04(2-1) = 1
- 参数默认值
def fun05 (a:Int = 10, b:String = "abc"): Unit ={ println(s"$a\t$b") } fun05() 结果:10 abc def fun05 (a:Int = 10, b:String = "abc"): Unit ={ println(s"$a\t$b") } fun05(20) 结果: 20 abc def fun05 (a:Int = 10, b:String = "abc"): Unit ={ println(s"$a\t$b") } fun05(b = "wang") 结果: 10 wang
- 匿名函数
var fun06 = (a:Int, b:Int) => { a + b } val str = fun06(3, 4) val str:Int = fun06(3, 4) println(str) 结果:7 var fun06:(Int, Int)=>Int = (a:Int, b:Int) =>{ a + b } val str:Int = fun06(3, 4) println(str) 结果:7 (Int, Int) => Int (参数类型列表) => 返回值类型 (a:Int, b:Int) => {a + b} (参数实现列表) => 函数体
- 嵌套函数
方法里面套娃 - 偏函数引用
def fun07(date:Date, level:String, msg:String){ println(s"date=$date\t tp=$level \t msg=$msg") } fun07(new Date(), "info", "MESSAGE") 结果:date=Sat Jan 30 19:02:31 CST 2021 tp=info msg=MESSAGE 将日志级别恒定为error级别 var fun = fun07(_:Date, "error", _:String) fun(new Date(), "exception...") 结果: date=Sat Jan 30 19:03:56 CST 2021 tp=error msg=exception...
- 可变参数
def fun08(a:Int*): Unit ={ for(e <- a){ println(e) } } fun08(5, 6 , 7) 结果: 5 6 7
- forEach
def fun08(a:Int*): Unit ={ a.foreach( (x:Int)=> { println(x) }) } fun08(5, 6 , 7) 当有一个参数时候 def fun08(a:Int*): Unit ={ a.foreach( println(_)) } fun08(5, 6 , 7) 当有一个参数时候 def fun08(a:Int*): Unit ={ a.foreach( println) } fun08(5, 6 , 7)
高阶函数
- 函数作为参数
def computer(a:Int, b:Int, fun:(Int, Int) => Int): Unit ={ var res: Int = fun(a , b) println(res) } computer(4, 5, (x:Int, y: Int)=>{ x * y}) computer(4, 5, _+_)
- 函数作为返回值
def fun09(i: String): (Int, Int) => Int = { def plus(x: Int, y: Int): Int = { x + y } if (i.equals("+")) { plus } else { (x: Int, y: Int) => { x * y } } } computer(4, 5, fun09("+")) def computer(a: Int, b: Int, fun: (Int, Int) => Int): Unit = { var res: Int = fun(a, b) println(res) } 结果: 9
- 柯里化
def fun10(a:Int)(b:Int)(c:String): Unit ={ println(s"$a\t$b\t$c") } fun10(3)(8)("aa") 结果: 3 8 aa 应用: 一个可变参数想多个类型,可以拆成多个可变参数 def fun11(a:Int*)(b:String*): Unit ={ a.foreach(print) println() b.foreach(print) } fun11(1,2,3)("a","b","c") 结果: 123 abc
- 函数赋值
def fun12(): Unit ={
println("我是context")
}
val context = fun12
println(context)
val func = fun12 _ 这样写只有函数调用的时候才打印
func()结果:
我是context
()
我是context