大数据系列修炼-Scala课程34
2016-09-18 09:40
357 查看
大数据系列修炼-Scala课程34
核心内容:
1、scala中对List进行高效的排序
2、Scala中对List进行倒排序
1>List的排序方式有两种:通过相应的Scala API进行排序(速度慢),通过一定的算法进行排序(如归并排序)
2>函数的定义中为了应对各种数据类型,可以使用泛型的方式
3>借助柯里化,一个函数中可以有多个形式参数列表
实例程序:
运行结果:
在上面的程序中,我们可以复习之前学过的一些知识:
实例程序:
运行结果:
核心内容:
1、scala中对List进行高效的排序
2、Scala中对List进行倒排序
1、Scala中对List进行高效的排序和倒排序代码实战 |
2>函数的定义中为了应对各种数据类型,可以使用泛型的方式
3>借助柯里化,一个函数中可以有多个形式参数列表
实例程序:
object App { //本节程序是对List进行排序:算法归并排序 def main(args:Array[String]):Unit= { def mergedsort[T](less:(T,T)=>Boolean)(input:List[T]):List[T]= { /* * @param xList 要合并的有序列表 * @param yList 要合并的有序列表 * @return 合并后的列表 * */ def merge(xList:List[T],yList:List[T]):List[T]= (xList,yList) match { case (Nil,_) => yList case (_,Nil) => xList case (x::xtail,y::ytail)=> if(less(x,y)) x::merge(xtail,yList) else y::merge(xList,ytail) } val n = input.length / 2 if( n ==0 ) input else { val (x,y) = input.splitAt(n) merge(mergedsort(less)(x),mergedsort(less)(y)) } } println("排序之后的List列表:") println(mergedsort((x:Int,y:Int)=>x<y)(List(3,7,9,5))) val reversed_mergedsort = mergedsort((x:Int,y:Int)=>x>y)_ //柯里化列表一个为空也是可以的 println("倒排序之后的List列表:") println(reversed_mergedsort(List(3,7,9,5))) } }
运行结果:
排序之后的List列表: List(3, 5, 7, 9) 倒排序之后的List列表: List(9, 7, 5, 3)
在上面的程序中,我们可以复习之前学过的一些知识:
实例程序:
object App8 { def main(args:Array[String]):Unit= { //一阶函数与高阶函数的定义与使用 def fun1(x:Int,y:Int):Int= { println("运行结果是:") x * y } def fun2(f:(Int,Int)=>Int)(x:Int,y:Int)= //借助柯里化,一个函数可以有多个形式参数列表 { fun1(x,y) } println("kelihua") println(fun2(fun1)(10,20)) //在Scala中通过泛型T与Any的运用可以实现函数的重载 def fun3(x:Any,y:Any):Any = println("x is:" + x+"\t" +"y is:"+ y) fun3(10,20) fun3("zhang","ming") //或者: def fun4(x:Any,y:Any):Any = println("x is:" + x+"\t" +"y is:"+ y) fun4(10,20) fun4("zhang","ming") //函数的内部嵌套一个函数 def fun5(str1:String,str2:String)= { def fun6()= { println(str1+"\t"+str2) } } fun5("spark","hadoop") //但是函数没有输出 //在模式匹配中使用泛型 def merge[T](xList:List[T],yList:List[T]):List[T]= (xList,yList) match { case (Nil,_) => yList case (_,Nil) => xList case _ => xList:::yList } println(merge(List(10,20,30),List(40,50,60))) //List(10, 20, 30, 40, 50, 60) println(merge(List(),List(40,50,60))) //List(40, 50, 60) println(merge(List(10,20,30),List())) //List(10, 20, 30) println("-----------") //将一个list拆分成两个list集合 val list = List(10,20,30,40,50,60) val (x,y) = list.splitAt(3) println(x) //List(10, 20, 30) println(y) //List(40, 50, 60) val ele1::ele2::Nil = List(10,20) println(ele1) println(ele2) println(Nil) //List() } }
运行结果:
kelihua 运行结果是: 200 x is:10 y is:20 x is:zhang y is:ming x is:10 y is:20 x is:zhang y is:ming List(10, 20, 30, 40, 50, 60) List(40, 50, 60) List(10, 20, 30) ----------- List(10, 20, 30) List(40, 50, 60) 10 20 List()
相关文章推荐
- 大数据系列修炼-Scala课程33
- 大数据系列修炼-Scala课程54
- 大数据系列修炼-Scala课程110
- 大数据系列修炼-Scala课程10
- 大数据系列修炼-Scala课程55
- 大数据系列修炼-Scala课程85
- 大数据系列修炼-Scala课程13+14
- 大数据系列修炼-Scala课程99
- 大数据系列修炼-Scala课程25
- 大数据系列修炼-Scala课程51
- 大数据系列修炼-Scala课程03
- 大数据系列修炼-Scala课程93
- 大数据系列修炼-Scala课程04
- 大数据系列修炼-Scala课程29
- 大数据系列修炼-Scala课程61
- 大数据系列修炼-Scala课程01
- 大数据系列修炼-Scala课程05
- 大数据系列修炼-Scala课程74
- 大数据系列修炼-Scala课程09
- 大数据系列修炼-Scala课程84