Vivek Modi
06/15/2022, 3:18 PMfun getConsultationCoverageList(): List<ContentWithTitle> {
val list = mutableListOf<ContentWithTitle>()
val consultation = getConsultationItem()
consultation?.consultationCategories?.forEachIndexed { _, consultationCategory ->
val paragraphs = mutableListOf<Paragraph>()
consultationCategory.consultationTopics?.forEachIndexed { _, consultationTopics ->
paragraphs.add(Paragraph(type = LIST_ITEM.type, text = consultationTopics.title))
}
list.add(ContentWithTitle(title = consultationCategory.title, content = Paragraphs(paragraphs), description = consultationCategory.description))
}
return list
}
Joffrey
06/15/2022, 3:19 PMforEachIndexed { _, x -> ... }
is pointless because it discards the index. You may as well use forEach
in the first place.paragraphs = mutableListOf()
+ forEach { paragraphs.add(something) }
can be changed using a simple map {}
operator:
val paragraphs = consultationCategory.consultationTopics.orEmpty().map {
Paragraph(type = LIST_ITEM.type, text = it.title)
}
(I added .orEmpty()
to deal with the null case)val list = mutableListOf
+ forEach { list.add(..) }
ephemient
06/15/2022, 3:22 PMgetConsultationItem()?.consultationCategories?.map { category ->
val paragraphs = category.consultationTopics?.map {
Paragraph(...)
}.orEmpty()
ContentWithTitle(...)
}.orEmpty()
Vivek Modi
06/15/2022, 3:27 PM2 foreach
the my complexity is n^2
. So what will be good effect when using map
? ThanksJoffrey
06/15/2022, 3:30 PMmap
is just a more standard way of doing the same thing. The time complexity is unchanged. It is just an abstraction to better convey your intentmap
you're instead saying "convert this list into a new list where each item is transformed this way".
The code does the same thing, but it expresses your actual intent at a higher level, which is usually faster to read and understand.Vivek Modi
06/15/2022, 3:33 PMJoffrey
06/15/2022, 3:34 PMval paragraphs = consultationCategory.consultationTopics.orEmpty().map {
Paragraph(type = LIST_ITEM.type, text = it.title)
}
You could have:
val paragraphs = consultationCategory.consultationTopics.orEmpty().map { it.toParagraph() }
If you define an extension function:
private fun ConsultationTopic.toParagraph() = Paragraph(type = LIST_ITEM.type, text = title)
This usually reads better, again because of the higher level of abstraction, hiding the detailsVivek Modi
06/15/2022, 3:38 PMephemient
06/15/2022, 3:42 PMJoffrey
06/15/2022, 3:44 PMfun getConsultationCoverageList(): List<ContentWithTitle> =
getConsultationItem()?.consultationCategories.orEmpty().map { it.toContentWithTitle() }
private fun ConsultationCategory.toContentWithTitle(): ContentWithTitle =
ContentWithTitle(title = title, content = paragraphs, description = description)
private val ConsultationCategory.paragraphs
get() = Paragraphs(consultationTopics.orEmpty().map { it.toParagraph() })
private fun ConsultationTopic.toParagraph() = Paragraph(type = LIST_ITEM.type, text = title)
Vivek Modi
06/15/2022, 3:49 PMJoffrey
06/15/2022, 3:53 PMnull
or empty list for your use case (which is the case in this piece of code), you should probably make them non-nullable in the first place in your classes. This will get rid of those .orEmpty()
everywhere.
That is, unless those classes are defined in Java and you have no choice but to deal with nullVivek Modi
06/15/2022, 3:54 PM.orEmpty()
Joffrey
06/15/2022, 3:55 PM?.
) so I assumed your lists were nullable, my badVivek Modi
06/15/2022, 3:56 PM