kovrik
11/07/2017, 4:31 AMorangy
( (1,2), (3,4) )
to (1,2,3,4)
. For a generic T, what should it do?kovrik
11/07/2017, 7:32 AMorangy
kovrik
11/07/2017, 7:44 AMprivate fun flatten(seq: Sequence<Any?>) = mutableListOf<Any?>().apply {
val queue = LinkedList<Any?>().apply { addAll(seq); }
while (!queue.isEmpty()) {
val e = queue.remove()
when (e) {
is Sequence<*> -> queue.addAll(e)
else -> add(e)
}
}
}
But this doesn’t work as expectedprintln(flatten(listOf(1, listOf(2, 3).asSequence(), 4).asSequence())) // prints [1, 4, 2, 3], not [1, 2, 3, 4]
orangy
kovrik
11/07/2017, 8:09 AM