I have some data clases
data class Data(val docNumber: Int?,
val docType: Int?,
val fiscprops: List<FiscProp>,
val name: String?
) {
fun getFiscProp(tag: Int) = fiscprops.firstOrNull { it.tag == tag }
}
data class FiscProp(val caption: String?,
val printable: String?,
val tag: Int?,
val value: Any?,
val fiscprops: List<FiscProp>?)
I need to find FiscProp
by tag
in nested lists
. If I use getFiscProp
I can find FiscProp
only if it is on first level of hierarchy.
How can I find element on the all levels? I don't know how many levels will be.
val FiscProp.allProps: Sequence<FiscProp>
get() = sequence {
yield(this@allProps)
fiscprops?.forEach {
yieldAll(it.allProps)
}
}
class Data(...) {
fun getFiscProp(tag: Int) = fiscprops.asSequence()
.flatMap { it.allProps}
.firstOrNull { it.tag == tag }
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments