The 2nd op in aggregate does not work

In source scala\src\library\scala\collection\TraversableOnce.scala (2.12.x) the implementation of aggregate is

def aggregate[B](z: =>B)(seqop: (B, A) => B, combop: (B, B) => B): B = foldLeft(z)(seqop)

It seems the combop is not used. But the related doc says:
As an example, consider summing up the integer values of a list of chars. The initial value for the sum is 0. First, seqop transforms each input character to an Int and adds it to the sum (of the partition). Then, combop just needs to sum up the intermediate results of the partitions

I think aggregate was meant for the parallel collections. When you use it on a non-parallel collection it doesn’t make much sense to split it in multiple partitions; it would just make the operation slower. That’s also why the documentation says it’s perfectly legal for combop to be called 0 times.
The parallel collections will be removed from the standard library, so in the future aggregate will probably no longer exist anyway.

Why are the parallel collections being removed from the standard library?

They are moved to a separate module. See https://github.com/scala/scala-dev/issues/323.

I think it will be convenient to use aggregate to do following:

max(of x(1)-x(2),x(2)-x(3), ……, x(p-1)-x(p))
sum( of (x(1)< x(2)), (x(2)< x(3)),……(x(p-1)<x(p)))

etc.

This is why i want to use aggregate.

If I understood you correctly, you can do that with a sliding window:

List(3,7,8,9,4,2,4,6,2,1,7).sliding(2).map{ case List(a,b) => a - b }.max

aggregate is not a sliding window. It’s a fold operation.

or

List(3,7,8,9,4,2,4,6,2,1,7).sliding(2).foldLeft(Int.MinValue) {
  case (m, List(a,b)) => m max (a - b)
}

since it’s a fold.

But WHY are the parallel collections being taken out of the standard library? That will just make it less convenient to use them.

I guess because … they were not up to par.

1 Like