uhe
07/24/2017, 3:43 PMmap
, flatMap
etc
I often find these easier to process than "oldschool" java loops. Just to give you an idea (completely untested and may not meet your needs):
fun searchParent(id: String): Module? =
modules
.asSequence()
.mapNotNull { searchParent(it, id) }
.firstOrNull()
fun searchParent(root: Module, id: String): Module? =
root.steps
?.asSequence()
?.flatMap { it.steps?.asSequence() ?: emptySequence() }
?.firstOrNull { it.id == id }
scruffyfox
07/25/2017, 7:07 AM.asSequence()
, modules
is already type List<Module>
so surely that is redundant?uhe
07/25/2017, 8:13 AMval list = listOf(1, 2, 3, 4, 5)
val result = list
.asSequence()
.mapNotNull {
println("mapping $it")
if (it == 3) 3 else null
}
.firstOrNull()
println(result)