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!

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
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
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
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
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
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
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: