Kotlin, Programming, Software Development, Functional Programming, Coding.
valid URL, so this is where flatMap comes into play. val potentialUrls = listOf("https://example.com/page", "invalid-url", "https://another-example.com/resource") val domains = potentialUrls.flatMap { url -> runCatching { URL(url).host }.getOrNull()?.let { listOf(it) } ?: emptyList() } // Result: