ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

scala之aggregate函数

2021-12-02 14:59:17  阅读:275  来源: 互联网

标签:10 20 函数 17 scala seqop ._ aggregate 21


函数aggregate

格式

def aggregate[S](z: => S)(seqop: (S, Int) => S,combop: (S, S) => S): S

原理

a.par.aggregate((0,0))((x,y)=>{(x._1+y,x._2+1)},(x,y)=>{(x._1+y._1,x._2+y._2)})

​ 可能会把List分为多个区,例如3个,p1(1,2,3,4),p2(5,6,7),p3(8,9),第一个方法区内聚合,(1+2+3+4,1+1+1+1),(5+6+7,1+1+1),(8+9,1+1),第二个方法区间聚合,(10+18+17,4+3+2)

数组

val a = List(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)

统计所有数之和

​ 不分区

scala> a.aggregate(0)((x,y)=>{println("seqop",x,y,x+y);x+y},(x,y)=>{println(x,y,x+y);x+y})
(seqop,0,1,1)
(seqop,1,2,3)
(seqop,3,3,6)
(seqop,6,4,10)
(seqop,10,5,15)
(seqop,15,6,21)
(seqop,21,7,28)
(seqop,28,8,36)
(seqop,36,9,45)
(seqop,45,10,55)
(seqop,55,11,66)
(seqop,66,12,78)
(seqop,78,13,91)
(seqop,91,14,105)
(seqop,105,15,120)
(seqop,120,16,136)
(seqop,136,17,153)
(seqop,153,18,171)
(seqop,171,19,190)
(seqop,190,20,210)
(seqop,210,21,231)
(seqop,231,22,253)
(seqop,253,23,276)
(seqop,276,24,300)
res96: Int = 300

​ 分区

scala> a.par.aggregate(0)((x,y)=>{println("seqop",x,y,x+y);x+y},(x,y)=>{println(x,y,x+y);x+y})
(seqop,0,1,1)
(seqop,0,20,20)
(seqop,0,12,12)
(seqop,0,22,22)
(seqop,0,23,23)
(seqop,23,24,47)
(seqop,0,2,2)
(seqop,0,11,11)
(11,12,23)
(seqop,0,19,19)
(seqop,0,7,7)
(seqop,0,10,10)
(10,23,33)
(seqop,0,6,6)
(seqop,0,13,13)
(seqop,0,4,4)
(seqop,0,14,14)
(seqop,0,16,16)
(seqop,0,9,9)
(seqop,0,5,5)
(22,47,69)
(seqop,0,8,8)
(8,9,17)
(7,17,24)
(24,33,57)
(seqop,0,21,21)
(seqop,0,3,3)
(20,21,41)
(5,6,11)
(seqop,0,17,17)
(seqop,14,15,29)
(seqop,17,18,35)
(16,35,51)
(4,11,15)
(19,41,60)
(2,3,5)
(60,69,129)
(13,29,42)
(1,5,6)
(42,51,93)
(6,15,21)
(93,129,222)
(21,57,78)
(78,222,300)
res103: Int = 300

统计大于10的个数(用二元组)

a.par.aggregate((10,0))(
    (x,y)=>{
        if(y>x._1){
            println("seqop",x,y)
            (x._1,x._2+1)
        }else{
        x
        }
    },
    (x,y)=>{
        println(x,y)
        (x._1,x._2+y._2)
    }
)

输出

((10,0),(10,0))
(seqop,(10,0),23)
(seqop,(10,0),16)
(seqop,(10,0),20)
(seqop,(10,0),21)
(seqop,(10,0),22)
(seqop,(10,0),19)
((10,0),(10,0))
(seqop,(10,0),11)
((10,0),(10,0))
(seqop,(10,0),13)
(seqop,(10,1),12)
(seqop,(10,0),15)
((10,0),(10,0))
((10,1),(10,1))
(seqop,(10,0),18)
(seqop,(10,0),24)
(seqop,(10,0),14)
(seqop,(10,0),17)
((10,1),(10,1))
((10,1),(10,1))
((10,1),(10,2))
((10,0),(10,0))
((10,0),(10,2))
((10,1),(10,2))
((10,1),(10,2))
((10,1),(10,1))
((10,3),(10,3))
((10,0),(10,2))
((10,1),(10,2))
((10,0),(10,2))
((10,3),(10,3))
((10,6),(10,6))
((10,2),(10,12))
res102: (Int, Int) = (10,14)

统计大于10和大于20的个数(用四元组)

a.par.aggregate((10,0,20,0))(
    (x,y)=>{
        println("seqop",x,y);
        if(y>x._1&&y<=x._3){
            (x._1,x._2+1,x._3,x._4)
        }else if(y>x._3){
            (x._1,x._2,x._3,x._4+1)
        }else{
            x
        }
    },
    (x,y)=>{
        println(x,y)
        (x._1,x._2+y._2,x._3,x._4+y._4)
    }
)

输出

(seqop,(10,0,20,0),1)
(seqop,(10,0,20,0),2)
(seqop,(10,0,20,0),8)
(seqop,(10,0,20,0),19)
(seqop,(10,0,20,0),20)
(seqop,(10,1,20,0),21)
((10,1,20,0),(10,1,20,1))
(seqop,(10,0,20,0),10)
(seqop,(10,0,20,0),7)
(seqop,(10,0,20,0),13)
(seqop,(10,0,20,0),6)
(seqop,(10,0,20,0),5)
((10,0,20,0),(10,0,20,0))
(seqop,(10,0,20,0),4)
(seqop,(10,0,20,0),24)
(seqop,(10,0,20,0),16)
(seqop,(10,0,20,0),14)
(seqop,(10,0,20,0),12)
(seqop,(10,0,20,0),23)
(seqop,(10,0,20,0),9)
(seqop,(10,0,20,0),11)
(seqop,(10,0,20,0),22)
(seqop,(10,0,20,0),3)
((10,1,20,0),(10,1,20,0))
((10,0,20,0),(10,0,20,0))
((10,0,20,1),(10,0,20,1))
((10,0,20,1),(10,0,20,2))
(seqop,(10,1,20,0),15)
((10,1,20,0),(10,2,20,0))
(seqop,(10,0,20,0),18)
(seqop,(10,0,20,0),17)
((10,0,20,0),(10,0,20,0))
((10,1,20,0),(10,1,20,0))
((10,2,20,1),(10,0,20,3))
((10,0,20,0),(10,0,20,0))
((10,0,20,0),(10,2,20,0))
((10,0,20,0),(10,0,20,0))
((10,0,20,0),(10,2,20,0))
((10,1,20,0),(10,2,20,0))
((10,0,20,0),(10,0,20,0))
((10,3,20,0),(10,3,20,0))
((10,0,20,0),(10,0,20,0))
((10,6,20,0),(10,2,20,4))
((10,0,20,0),(10,2,20,0))
((10,2,20,0),(10,8,20,4))
res99: (Int, Int, Int, Int) = (10,10,20,4)

标签:10,20,函数,17,scala,seqop,._,aggregate,21
来源: https://blog.csdn.net/qq_51903852/article/details/121677340

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有