arekolek
08/31/2017, 2:00 PMinline fun <T> Collection<T>.flattenBy(transform: (T) -> Collection<T>?): List<T> {
val destination = ArrayList<T>()
val queue = LinkedList<T>(this)
while (queue.isNotEmpty()) {
queue.removeFirst().let {
destination.add(it)
transform(it)?.run { queue.addAll(0, this) }
}
}
return destination
}