map的方法filter
3、
object gaojie {
def main(args: Array[String]): Unit = {
(1 to 10).map("*" * _).foreach(println _);
//数组1 到 10 调用高阶函数map()映射 将每一行乘以对应的*
//然后在依次打印这个数组
}
}
4、reduce化简
object gaojie {
def main(args: Array[String]): Unit = {
val arr = Array(1,3,4,5);
// val result = arr.reduceLeft(_ + _);
// val result = arr.reduceRight(_ * _);
//val result = arr.reduceRight(_ - _);
//1-(3-(4-5))
val result = arr.reduceLeft(_ - _);
//(((1-3)-4)-5)
println(result);
}
}
5、柯里化的过程
两参变一参
object gaojie {
def main(args: Array[String]): Unit = {
def fun(a:Int) = {(x : Int)=> a * x;}
println(fun(1)(2));
}
}
6、控制抽象
object controllAbstract {
def newThread(block: Unit){
new Thread(){
override def run() ={
block;
}
}.start();
}
def main(args: Array[String]): Unit = {
newThread{
( 1 to 10).foreach(e => {
val tname = Thread.currentThread().getName();
println(tname + ":"+e);
})
}
}
}
打印结果:
main:1
main:2
main:3
main:4
main:5
main:6
main:7
main:8
main:9
main:10