From 8fd356716991aedb794e5bbb1c477b92c987a1d5 Mon Sep 17 00:00:00 2001 From: ilitosh Date: Fri, 10 Mar 2023 12:25:09 +0000 Subject: [PATCH] Fix off-by-one error in FakeMessagingService --- README.adoc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.adoc b/README.adoc index b717726..44defe6 100644 --- a/README.adoc +++ b/README.adoc @@ -213,7 +213,7 @@ class FakeMessageService : MessageService { override fun latest(): List { val count = Random.nextInt(1, 15) - return (0..count).map { + return (1..count).map { val user = users.values.random() val userQuote = usersQuotes.getValue(user.name).invoke() @@ -244,7 +244,7 @@ The main task of the `FakeMessageService` class is to generate a random number o [source,kotlin] ----- val count = Random.nextInt(1, 15) -return (0..count).map { +return (1..count).map { val user = users.values.random() val userQuote = usersQuotes.getValue(user.name).invoke() @@ -252,7 +252,7 @@ return (0..count).map { }.toList() ----- -In Kotlin, to generate a https://kotlinlang.org/docs/reference/ranges.html[range] of integers all we need to do is say `(0..count)`. We then apply a `map()` function to transform each number into a message. +In Kotlin, to generate a https://kotlinlang.org/docs/reference/ranges.html[range] of integers all we need to do is say `(1..count)`. We then apply a `map()` function to transform each number into a message. Notably, the selection of a random element from any collection is also quite simple. Kotlin provides an extension method for collections, which is called `random()`. We use this extension method to select and return a user from the list: `users.values.random()` @@ -1414,4 +1414,4 @@ fun `test that messages streamed to the API is stored`() { } ----- -This was the final part in the tutorial. We started with a simple chat application in which the UI was polling for new messages while the backend was blocking when running the database queries. We gradually added features to the application and migrated it to the reactive Spring stack. The backend is now fully asynchronous, making use of Spring WebFlux and Kotlin coroutines. \ No newline at end of file +This was the final part in the tutorial. We started with a simple chat application in which the UI was polling for new messages while the backend was blocking when running the database queries. We gradually added features to the application and migrated it to the reactive Spring stack. The backend is now fully asynchronous, making use of Spring WebFlux and Kotlin coroutines.