How to avoid mutable collection

Can someone help me with the parallel collections library?

If m is of type ParSeq[A] and f is of type List[A]=>B, then I get an confusing error on the expression m.toList.grouped(2).toSeq.par.map(f). Somehow it thinks I’m calculated a collection, and I need to explicitly calculate an immutable collection? This is somewhat surprising for several reasons. Isn’t the default type immutable anyway? It seems I should have to do something special to get a mutable collection.

Q1: Aren’t immutable collections supposed to be the default?

I know that’s a vague question, but it seems like the intended intuition. Right?

Error:(39, 63) type mismatch;
 found   : ParSeq[B] (in scala.collection.parallel) 
 required: ParSeq[B] (in scala.collection.parallel.immutable) 
    val reduced:ParSeq[B] = m.toList.grouped(2).toSeq.par.map { b =>

Here is a MWE of the code.

The second reason why this is confusing is that if it try the same code in 2.13, it get completely different errors. Q2: Should we not be using the parallel collections library anymore?

object parallel is not a member of package collection

Q3: What is the correct way to calculate a ParSeq[B] in scala.collection.parallel.immutable, rather than a ParSeq[B] in scala.collection.parallel?

That’s an excellent intuition. Unfortunately, in Scala 2.12, it’s incorrect. It wasn’t until 2.13 (after many years of everyone agreeing that this was a wart in the standard library) that the immutable collections became the default.

1 Like

Why do you need to do this on a collection which is already par?

@batman, if I understand your question correctly, it seems that even though m is par, m.toList.grouped(2).toSeq is not.

Yes, it seems to compile as expected in 2.13, after updating the sbt configuration to download the library.