Explanation of the aggregate scala function

前端 未结 5 1773
我寻月下人不归
我寻月下人不归 2021-01-31 04:35

I do not get to understand yet the aggregate function:

For example, having:

val x = List(1,2,3,4,5,6)
val y = x.par.aggregate((0, 0))((x, y) => (x._1          


        
5条回答
  •  爱一瞬间的悲伤
    2021-01-31 05:17

    Adding to Rashmit answer.

    • CombOp is called only if the collection is processed in parallel mode.

    See below example :

    val listP: ParSeq[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).par

    val aggregateOp1 = listP.aggregate[String]("Aggregate-")((a, b) => a + b, (s1, s2) => {
      println("Combiner called , if collections is processed parallel mode")
      s1 + "," + s2
    })
    println(aggregateOp1)
    

    OP : Aggregate-1,Aggregate-2,Aggregate-3,Aggregate-45,Aggregate-6,Aggregate-7,Aggregate-8,Aggregate-910

    val list: Seq[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    val aggregateOp2 = list.aggregate[String]("Aggregate-")((a, b) => a + b, (s1, s2) => {
      println("Combiner called , if collections is processed parallel mode")
      s1 + "," + s2
    })
    println(aggregateOp2)
    

    }

    OP : Aggregate-12345678910

    In above example, combiner operation is called only if collection is operated in parallel

提交回复
热议问题