def addCurry(x: Int)(y: Int) = x + y | | V def addCurry(x: Int)=(y: Int) = x + y | 相当于调用addCurry(x:Int) 返回一个函数 | addCurry(1) <--(y:Int)=1+y V var result = (y:Int)=1+y | | V result(2) | | V 3
Demo
1 2 3 4 5 6 7 8 9 10
deffoldLeft[B](z: B)(f: (B, A) => B): B = { var acc = z var these = this while (!these.isEmpty) { acc = f(acc, these.head) these = these.tail } acc }
1 2 3 4 5
deffoldtest() = { val list = List(1, 2, 3) val strResleft = list.foldLeft("res:")((x: String, y:Int) => x + y) println(strResleft) }
自己实现While循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
objectRecursiveDemo2{ defmain(args: Array[String]): Unit = { var a = 0 //一句代码可以省略{} //一坨代码可以省略() loop(a<=100){ println(a) a+=1 } }
defloop(condition: =>Boolean)(op: => Unit):Unit={ if(condition){ op loop(condition)(op) } } }