I am relatively new to Kotlin and I try to overcome a special case. I am filtering a books store and want to verify that the length of the obtained list is exactly one unit shorter than the original one. Further I need to verify that the discarded element is under a specific state. Here is my example:
fun BookStoreVerified(bookStore: BookStore): Boolean {
val specialChapter = bookStore.stores
.flatMap { it.books }
.flatMap { it.chapters }.filter { it != null && it.state == Chapter.SPECIAL }
val total = bookStore.stores
.flatMap { it.books }
.flatMap { it.chapters }
.filterNotNull()
val finalChapters = book.stores
.flatMap { it.books }
.flatMap { it.chapters }
.filter { it != null && it.state.isCorrect }
return (finalChapters.size + specialChapterFigure.size == total.size) && (specialChapter.size == 1)
}
My question is if there is a smarter way to compute the above operation. I would like to know if ander a scope like filter, map can we make reference to the previous object? ( get the length of the original list for instance ?)
You have
Books where eachBookcontains a list ofChapters. You want to partition chapters from all the books according to some criteria.With this in mind the partition function can be useful:
Now that you have
specialChsandregularChs, you can check whatever invariants you want.For example:
Edit: It is possible to abstract away the existence of null chapters inside a
Book:then in your code you can
flatMap { it.safeChapters }instead of.flatMap { it.chapters ?: emptyList() }