码迷,mamicode.com
首页 > 其他好文 > 详细

第37讲:List的foldLeft、foldRight、sort操作代码实战

时间:2015-08-27 00:12:21      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

其实flodLeft和foldRight就是折叠操作,我让们看下下列的函数

 

 

折叠操作
 
 def sum(xs:List[Int]):Int = ( 0 /: xs)(_ +_)
    def product(xs:List[Int]):Int=( 1 /: xs)(_ *_)
   
    (fruit.head /: fruit.tail)(_ + " " + _) //用空格连接列表中每个元素
 
 
 
 
 
列表排序 sort
 
 
 println(List(1,-4,3,2,6).sortWith(_<_))//List(-4, 1, 2, 3, 6)
    println(List(1,-4,3,2,6).sortWith(_>_))//List(6, 3, 2, 1, -4)
 
 
 

分享下更多的scala资源吧:

 

百度云盘:http://pan.baidu.com/s/1gd7133t

 

微云云盘:http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416

 

360云盘: http://yunpan.cn/cQN9gvcKXe26M (提取码:13cd)

 

信息来源于 DT大数据梦工厂微信公众账号:DT_Spark

 

关注微信账号,获取更多关于scala学习内容

第37讲:List的foldLeft、foldRight、sort操作代码实战

标签:

原文地址:http://www.cnblogs.com/qingyuesama/p/4761911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!