Explanation of the aggregate scala function

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

    aggregate takes 3 parameters: a seed value, a computation function and a combination function.

    What it does is basically split the collection in a number of threads, compute partial results using the computation function and then combine all these partial results using the combination function.

    From what I can tell, your example function will return a pair (a, b) where a is the sum of the values in the list, b is the number of values in the list. Indeed, (21, 6).

    How does this work? The seed value is the (0,0) pair. For an empty list, we have a sum of 0 and a number of items 0, so this is correct.

    Your computation function takes an (Int, Int) pair x, which is your partial result, and a Int y, which is the next value in the list. This is your:

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

    Indeed, the result that we want is to increase the left element of x (the accumulator) by y, and the right element of x (the counter) by 1 for each y.

    Your combination function takes an (Int, Int) pair x and an (Int, Int) pair y, which are your two partial results from different parallel computations, and combines them together as:

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

    Indeed, we sum independently the left parts of the pairs and right parts of the pairs.

    Your confusion comes from the fact that x and y in the first function ARE NOT the same x and y of the second function. In the first function, you have x of the type of the seed value, and y of the type of the collection elements, and you return a result of the type of x. In the second function, your two parameters are both of the same type of your seed value.

    Hope it's clearer now!

提交回复
热议问题