intro-coroutines
intro-coroutines copied to clipboard
Sorting is not stable enough
Request6ProgressKtTest.testProgress failed with this:
Wrong intermediate result after 3200: expected:<[User(login=user-2, contributions=50), User(login=user-1, contributions=50)]> but was:<[User(login=user-1, contributions=50), User(login=user-2, contributions=50)]>
Expected :[User(login=user-2, contributions=50), User(login=user-1, contributions=50)]
Actual :[User(login=user-1, contributions=50), User(login=user-2, contributions=50)]
https://github.com/kotlin-hands-on/intro-coroutines/blob/c51a4033c735451e79503ec64944152ea6f92a5b/src/tasks/Aggregation.kt#L20 (in the solution branch) should be updated to
.sortedWith(compareByDescending<User> { it.contributions }
.thenByDescending { it.login })
This is my solution for Request6Progress.kt:
suspend fun loadContributorsProgress(
service: GitHubService,
req: RequestData,
updateResults: suspend (List<User>, completed: Boolean) -> Unit
) {
val repos = service
.getOrgRepos(req.org)
.also { logRepos(req, it) }
.body() ?: listOf()
var allUsers = emptyList<User>()
repos.forEachIndexed { index, repo ->
val users = service
.getRepoContributors(req.org, repo.name)
.also { logUsers(repo, it) }
.bodyList()
allUsers += users
// allUsers = (allUsers + users).aggregate() // uncomment this line to make the test happy
updateResults(allUsers.aggregate(), index == repos.lastIndex)
}
}