//折叠 ===》计算结果为下一个输入
def main(args: Array[String]): Unit = {
val list =List(1,2,3,4,5)
/**
*柯里化函数的应用
* (((10-1)-2)-3)-4)-5)
* */
val left_list = list.foldLeft(10)(sun)
println(left_list)
/**
* 1 - (2- (3-(4-(5-10))))
* =1 -2 +3 -4+5 -10
* = - 7
* */
val right_list = list.foldRight(10)(sun)
println(right_list)
}
def sun(num1:Int,num2:Int): Int={
num1-num2
}
//化简 ===》分治法 将集合划分 为多个小部分
object Test5 {
def main(args: Array[String]): Unit = {
val list =List(1,2,3,4,5)
// (((1-2)-3) -4)-5 =13
val list_left = list.reduceLeftOption(sum)
println(list_left.get)
//从右边先开始
// 1- ( 2- (3-(4-5))) =3
val list_right = list.reduceRightOption(sum)
println(list_right.get)
}
def sum(num1:Int,num2:Int): Int={
num1-num2
}
}
def main(args: Array[String]): Unit = {
/**
* 扫描 ==>类似于leftfold折叠的方法,但是是将中间结果返回
*
*/
val list =List(1,2,3,4,5)
//
val list_left = list.scanLeft(10)(sun)
println(list_left)
/** 将rightfold的结果逆向输出
* 1 - (2- (3-(4-(5-10))))
* =1 -2 +3 -4+5 -10
* = - 7
* */
val right_list = list.scanRight(10)(sun)
println(right_list)
}
def sun(num1:Int,num2:Int): Int={
num1-num2
}
//拉链 ===》向拉链一样,从集合取出值来
object Test7 {
def main(args: Array[String]): Unit = {
//数据不对应,会缺失数据
var list1 = List(1,2,3,4)
var list2 = List(4,5,6)
var list3 = list1.zip(list2)
println(list3)
var list4 = list3.iterator
while(list4.hasNext){
println(list4.next())
}
var enum=null;
}
}