Let there is a map config: Map[String, String]
.
And there are several keys: "foo", "bar", ...
I need to make sure that all keys are present in config
. And if they are present, I need to call a function with values for these keys in the config
map:
fun(config("foo"), config("bar"), config(...), ...)
The following is a solution:
val res = Option.when(config.contains("foo") & config.contains("bar") & config.contains(...) & ...)
( fun(config("foo"), config("bar"), config(...), ...) )
Or maybe:
val set = Set("foo", "bar", ...)
val res = Option.when(config.view.filterKeys(set).size == set.size)
( fun(config("foo"), config("bar"), config(...), ...) )
Both approaches look ugly and ineffective. Is there more concise way to implement the same behavior?