Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 7 - Both Edition - listing 7.3 - Program Trace for sum #631

Open
Maatary opened this issue Jan 29, 2022 · 2 comments
Open

Chapter 7 - Both Edition - listing 7.3 - Program Trace for sum #631

Maatary opened this issue Jan 29, 2022 · 2 comments

Comments

@Maatary
Copy link

Maatary commented Jan 29, 2022

I'm trying to figure out part of the explanation that follows the listing for a while , but can't manage to make good sense of it.

This specific second part of the passage that follow the listing.

And if map2 evaluates its arguments in parallel (using whatever resource is being used to implement the parallelism, like a thread pool), that implies the left half of our computation will start executing before we even begin constructing the right half of our computation.

That second part (above) follows the following first part of the passage.

In this trace, to evaluate sum(x), we substitute x into the definition of sum, as we’ve done in previous chapters. Because map2 is strict, and Scala evaluates arguments left to right, whenever we encounter map2(sum(x),sum(y))(_ + _), we have to then evaluate sum(x) and so on recursively. This has the rather unfortunate consequence that we’ll strictly construct the entire left half of the tree of summations first before moving on to (strictly) constructing the right half. Here sum(IndexedSeq(1,2)) gets fully expanded before we consider sum(IndexedSeq(3,4)).

1) I wonder what is meant here, and if the language can be changed in the book to reflect that (in the second part of the passage)

It seems there is a mix of semantic which in my humble opinion may cause some folks (like me) to confused.
The all time, the passage at first talk about evaluation of arguments, in the context of scala strict evaluation mechanism. Then we have

And if map2 evaluates its arguments in parallel
Clearly the opposite of strict is not parallel, we are talking of strict vs lazy. So the parallel evaluation here is not about the same thing as first part the passage.
This mix of semantic does little help to understand the all passage in my humble opinion.

It feels to me that at that point we are talking about what is happening in the body of the function, as opposed to what is happening mechanically with scala strict evaluation.

Eitherway i do really not understand this statement

that implies the left half of our computation will start executing before we even begin constructing the right half of our computation.

Why ? what does it has to do with "the parallel evaluation". Parallel as opposed to what ? Synchronous ? sequential ?

Can someone manage to explain or showcase in whatever way the two scenarios and their difference ?

If map2 was "evaluating" its argument sequentially/synchronously what would be the difference ?

@Maatary
Copy link
Author

Maatary commented Jan 30, 2022

If the authors indeed means, and if Map2 run its two Par arguments, asynchronously as opposed to synchronously, or in parallel as opposed to sequentially, then it should be made clear what happens in the opposing case i.e sequential/synchronously. If it does not make any difference, then stating it, is just confusing.

If my understanding is correct, then i don't think it make any difference wether the two Par are ran in parallel or sequentially, the outcome would be the same,

The left half of our computation will start executing before we even begin constructing the right half of our computation.

would still be true.

@Maatary
Copy link
Author

Maatary commented Jan 30, 2022

Looking at the section that follows: Does the author, actually intended to mean: “And if map2 begin execution immediately” ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant