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

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
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
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
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
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
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
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
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: