Explanation of the aggregate scala function

前端 未结 5 1767
我寻月下人不归
我寻月下人不归 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:06

    def aggregate[B](z: ⇒ B)(seqop: (B, A) ⇒ B, combop: (B, B) ⇒ B): B
    

    Breaking that down a little :

    aggregate(accumulator)(accumulator+first_elem_of_list, (seq1,seq2)=>seq1+seq2)

    Now looking at the example:

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

    Here:
    Accumulator is (0,0)
    Defined list is x
    First elem of x is 1

    So for each iteration, we are taking the accumulator and adding the elements of x to position 1 of the accumulator to get the sum and increasing position 2 of the accumulator by 1 to get the count. (y is the elements of the list)

    (x, y) => (x._1 + y, x._2 + 1)
    

    Now, since this is a parallel implementation, the first portion will give rise to a list of tuples like (3,2) (7,2) and (11,2). index 1 = Sum, index 2 = count of elements used to generate sum. Now the second portion comes into play. The elements of each sequence are added in a reduce fashion.

    (x,y) =>(x._1 + y._1, x._2 + y._2)
    

    rewriting with more meaningful variables:

    val arr = Array(1,2,3,4,5,6)
    arr.par.aggregate((0,0))((accumulator,list_elem)=>(accumulator._1+list_elem, accumulator._2+1), (seq1, seq2)=> (seq1._1+seq2._1, seq1._2+seq2._2))
    

提交回复
热议问题