Skip to content

Commit

Permalink
Fix off-by-one error in FakeMessagingService
Browse files Browse the repository at this point in the history
  • Loading branch information
ilitosh authored Mar 10, 2023
1 parent c158a49 commit 8fd3567
Showing 1 changed file with 4 additions and 4 deletions.
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.

0 comments on commit 8fd3567

Please sign in to comment.