Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:06:45+00:00 2026-06-17T18:06:45+00:00

I tried to make it optional to run a map operation sequentially or in

  • 0

I tried to make it optional to run a map operation sequentially or in parallel, for example using the following code:

val runParallel = true
val theList = List(1,2,3,4,5)
(if(runParallel) theList.par else theList) map println //Doesn't run in parallel

What I noticed is that the ‘map’ operation does not run in parallel as I’d expected. Although without the conditional, it would:

theList.par map println   //Runs in parallel as visible in the output

The type of the expression (if(runParallel) theList else theList.par) which I expect to be the closest common ancestor of both types of theList and theList.par is a scary type that I won’t paste here but it’s interesting to look at (via scala console:)

:type (if(true) theList else theList.par)

Why doesn’t the map on the parallel collection work in parallel?

UPDATE: This is discussed in SI-4843 but from the JIRA ticket it’s not clear why this was happening on Scala 2.9.x.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:06:46+00:00Added an answer on June 17, 2026 at 6:06 pm

    The explanation of why it happens is a long story: in Scala 2.9.x (I don’t know about the other versions) those collections methods such as filter or map relies on the CanBuildFrom mechanism. The idea is that you have an implicit parameter which is used to create a builder for the new collection:

    def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
        val b = bf(repr)
        b.sizeHint(this) 
        for (x <- this) b += f(x)
        b.result
      }
    

    Thanks to this mechanism, the map method is defined only in the TraversableLike trait and its subclasses do not need to override it. As you see, inside the method map signature there are many type parameters. Let’s look at the trivial ones:

    • The B which is the type of the elements of the new collection
    • The A is the type of the elements in the source collection

    Let’s look at the more complicated ones:

    • That is the new type of collection, which can be different from the current type. A classical example is when you map for example a BitSet using a toString:

       scala> val a = BitSet(1,3,5)
       a scala.collection.immutable.BitSet = BitSet(1, 3, 5)
       scala>  a.map {_.toString}
       res2: scala.collection.immutable.Set[java.lang.String] = Set(1, 3, 5)
      

    Since it is illegal to create a BitSet[String] your map result will be a Set[String]
    Finally Repr is the type of the current collection. When you try to map a collection over a function, the compiler will resolve a suitable CanBuildFrom using the type parameters.

    As it is reasonable, the map method has been overridden in parallel collections in ParIterableLike as the following:

     def map[S, That](f: T => S)(implicit bf: CanBuildFrom[Repr, S, That]): That = bf ifParallel { pbf =>
        executeAndWaitResult(new Map[S, That](f, pbf, splitter) mapResult { _.result })
      } otherwise seq.map(f)(bf2seq(bf))
    

    As you can see the method has the same signature, but it uses a different approach: it test whether the provided CanBuildFrom is parallel and otherwise falls back on the default implementation.
    Therefore, Scala parallel collections use special CanBuildFrom (parallel ones) which create parallel builders for the map methods.

    However, what happens when you do

    (if(runParallel) theList.par else theList) map println //Doesn't run in parallel
    

    is the map method gets executed on the result of

      (if(runParallel) theList.par else theList) 
    

    whose return type is the first common ancestors of both classes (in this case just a certain number of traits mixed togethers). Since it is a common ancestor, is type parameter Repr will be some kind of common ancestors of both collections representation, let’s call it Repr1.


    Conclusion

    When you call the map method, the compiler should find a suitable CanBuildFrom[Repr, B, That] for the operation. Since our Repr1 is not the one of a parallel collection, there won’t be any CanBuildFrom[Repr1,B,That] capable of providing a parallel builder. This is actually a correct behaviour with respect to the implementation of Scala collections, if the behaviour would be different that would mean that every map of non parallel collections would be run in parallel as well.

    The point here is that, for how Scala collections are designed in 2.9.x there is no alternative. If the compiler does not provide a CanBuildFrom for a parallel collection, the map won’t be parallel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to make an asynchronous UDP client using boost::asio I get some code
Using mod_rewrite, what RewriteRule(s) would make the following two examples function properly? Example #1
I tried to make admin panel and I am using sessions , but have
I tried using make defconfig to compile the kernel, but as expected, it failed
I tried to make ASP.NET Web Api CRUD operation in VS 2010 web application,
I am using multiple joins in a statement and Have tried to make linq-to-SQl
Code where I tried to make connection: DataSource ds = (DataSource)servlet.getServletContext().getAttribute(dbSource); System.out.println(ds1 : +ds);
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
I tried to make an alert popup with A with: var a = \u0041;
I tried to make a layout look like it It looks like kinda button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.