0

Having a look at the forall method implementation, how could this method be parallel?

  def forall(p: A => Boolean): Boolean = {
    var result = true
    breakable {
      for (x <- this)
        if (!p(x)) { result = false; break }
    }
    result
  }

As I understand for better parallelization, avoid using var's and prefer val's. How is this now supposed to work if I use forall?

1 Answer 1

2

You are looking at a non-parallel version of forall.

A parallel version looks like this (provided by a parallel collection, in this case ParIterableLike):

def forall(pred: T => Boolean): Boolean = {
  tasksupport.executeAndWaitResult
    (new Forall(pred, splitter assign new DefaultSignalling with VolatileAbort))
}

To get a parallel collection, insert a .par, for example:

List.range(1, 10).par.forall(n => n % 2 == 0)

If you use an IDE like IntelliJ, just "zoom" into that forall, and you'll see the code above. Otherwise here is the source of ParIterableLike (thanks @Kigyo for sharing the URL).

Sign up to request clarification or add additional context in comments.

4 Comments

Where did you get this from? Can you post the path to the API?
It's here: github.com/scala/scala/blob/2.12.x/src/library/scala/collection/… ParIterableLike also defines several other parallel methods
Just "zoom" in, otherwise look for ParIterableLike.scala
@Kigyou Thanks for your comment - I have added it to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.