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

Fix off-by-one error in FakeMessagingService #2

Open
wants to merge 1 commit into
base: initial
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class FakeMessageService : MessageService {

override fun latest(): List<MessageVM> {
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()

Expand Down Expand Up @@ -244,15 +244,15 @@ 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()

MessageVM(userQuote, user, Instant.now(), Random.nextBytes(10).toString())
}.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()`

Expand Down Expand Up @@ -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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FakeMessageService : MessageService {

override fun latest(): List<MessageVM> {
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()

Expand Down