Javaru

Javaru

Kotlin Coroutine Confidence: Add `parallel/v9` (pg 125)

When I ran the parallel/v8 example on page 125, I did not get the results described of not seeing the timer’s output until the program almost exits. I got the timer output right from the start, and every second thereafter. After a few minutes of trying to figure out why, I gave up and kept reading. Once I got to page 126, I saw why. It was because the slowHash() function was already a suspend function with the yield() in it. This is shown as a change to the code on page 126, but it was not a change since it was already present in the downloaded code, and therefore present for the run on page 125. Thus the different result.

Since I am running each example as soon as I first encounter it, (and up to this point it has always worked doing that :slight_smile:) this is unexpected behavior. As I said, I spent a few minutes trying to figure things out. I didn’t even look a the slowHash() function since it wasn’t a point of discussion. (And I’m not sure I would have noticed the issue had I looked at the function since that function is not discussed in v8 until it is modified on page 126.)

As a suggestion, I think the changes to the slowHash() function on page 126 should be broken out to a parallel/v9 instance. And the parallel/v8 slowHash() function should not be a suspend function or have the yield(). That way the “bad” result occurs when the code is run on page 125. Another option is to just not include the suspend and yield() in the parallel/v8 version and have the user make the change in their code when they read page 126. But that may be less desirable since until now, there have not been any code changes needed by the user, and it breaks the “incremental versions” paradigm in use. (And it may prevent you from testing things properly),

Assuming you go with a v9, then the sentence:

Run either of the two previous programs again with this upgraded slowHash()
function, and everything works the way we wanted. Our extra timer coroutine can print its output every one second, and our timeout code can cancel the work promptly.

can change to something like:

You can also run the previous timeout program with this upgraded slowHash()
function, and everything will work the way we wanted. Our timeout code can cancel the work promptly.

book-kotlin-coroutine-confidence version B3

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

I hadn’t considered this! Thanks for pointing it out. I guess I was trying to be clever by combining two steps into a single version of the code. I can see how that would cause a problem, since the code in the download no longer behaves the way the book is describing.

I think adding a v9 is a good solution; thanks for the suggestion.

Thank you for pointing out that the sentence after the code example could be a problem too, since it refers to programs from two different versions of the downloaded code. I’d still like to mention that the modified slowHash() function fixes both of them, but I’ll try to make it clear that you’d have to backport the changes to a previous version of the app by hand if you wanted to test both.

As ever, I really appreciate the feedback!

Popular Pragmatic Bookshelf topics Top

Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New