Skip to content

Instantly share code, notes, and snippets.

@suhininalex
Created January 20, 2016 13:25
Show Gist options
  • Save suhininalex/f90f0865fa8e8a59b8ae to your computer and use it in GitHub Desktop.
Save suhininalex/f90f0865fa8e8a59b8ae to your computer and use it in GitHub Desktop.
fun <T> Iterable<Iterable<T>>.flatten() = object: Iterable<T> {
val collection = this@flatten.Ifilter { it.isEmpty() }.iterator()
var element = collection.nextOrNull()?.iterator()
override fun iterator() = iterate {
if (element.hasNext())
return@iterate element!!.next()
else if (collection.hasNext()) {
element = collection.next().iterator()
return@iterate element!!.next()
}
else
return@iterate null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment