Sequence.flatMap { iterable }?

I think it would be nice to have a version of flatMap on Sequence that takes an Iterable.

For example the second asSequence() call feels superfluous and reminds me of Java’s .stream() / .collect() ceremony.

val mobileNumbers = contacts.asSequence()
    .flatMap { it.phoneNumbers.asSequence() }
    .filter { it.type == "mobile" }
    .map { it.number }

Here’s the full code with the collection based variant.

data class PhoneNumber(val type: String, val number: String)
data class Contact(val name: String, val phoneNumbers: List<PhoneNumber>)

val mobileNumbers = contacts
    .flatMap { it.phoneNumbers }
    .filter { it.type == "mobile" }
    .map { it.number }
4 Likes

This has gotten a few likes and no comments, so I created https://youtrack.jetbrains.com/issue/KT-34506

Some votes would be great!