A concatenated iterator example in Kotlin

Alessandro Diaferia on April 06, 2019

Some times you might need to iterate through multiple lists. This happens, for example, when implementing a tree traversal algorithm. Thanks to t... [Read Full]
markdown guide
 

Hm...what about

fun countAll(nodes: List<Node>): Int
    = nodes.size + nodes.sumBy { countAll(it.children) }

?

I'm more curious about how you came up with that flamegraph :) Looks useful.

 

Hi Max A. That recursive version is definitely more efficient than my tail recursive one as its stack size is smaller and doesn't have the overhead of the accumulation. What I wanted to focus on is the iteration across multiple lists and couldn't come up with a better example. Regarding the profiling tool, it's a pretty useful one and it's here: github.com/jvm-profiling-tools/asy...

Enjoy :)

code of conduct - report abuse