Search code examples
kotlinarrow-kt

Cut pairs with empty values from map


I'd like to filter out all the pairs with empty values

val mapOfNotEmptyPairs: Map<String, String> = mapOf("key" to Some("value"), "secondKey" to None)

expected:

print(mapOfNotEmptyPairs)
// {key=value}

Solution

  • val mapOfNotEmptyPairs =
            mapOf("key" to Some("value"), "secondKey" to None)
                .filterValues { it is Some<String> } // or { it !is None } or { it.isDefined() }
                .mapValues { (_, v) -> (v as Some<String>).t }