Map.isEmpty
func Map.isEmpty[K, V](m: Map[K, V]): boolMap.isEmpty reports whether m has no entries.
Details
- An entry whose value is
nullis still an entry, so a map that holds one is not empty.
Basic examples
Map.isEmpty(Map{ ada: 36, linus: 54 }) // false
let none: Map[string, int] = Map{}
Map.isEmpty(none) // trueCall it through . on any map:
let ages = Map{ ada: 36 }
ages.isEmpty() // falseScope
An entry whose value is null keeps the map from being empty:
let seen: Map[string, int | null] = Map{ ada: null }
seen.isEmpty() // falseThe answer follows the map as it changes:
let counts: Map[string, int] = Map{}
counts.isEmpty() // true
counts["go"] = 1
counts.isEmpty() // false
counts.remove("go") // 1
counts.isEmpty() // trueThe result of another function, such as Map.filter, is often what you test:
Map{ ada: 36, linus: 54 }.filter({ x.value > 60 }).isEmpty() // trueIn a pipeline, the map is the first argument:
Map{ ada: 36 } | Map.isEmpty() // falseAs a function value, it maps an array of maps to an array of bools:
let empty: Map[string, int] = Map{}
[Map{ a: 1 }, empty].map(Map.isEmpty) // [false, true]For a map that may be null, ?. yields null instead of calling:
let headers: Map[string, string] | null = null
headers?.isEmpty() // null
headers?.isEmpty() ?? true // trueApplications
Print a word count, or a note when there are no words:
func report(text: string) {
let counts: Map[string, int] = Map{}
for word in text.fields() {
counts[word] = (counts[word] ?? 0) + 1
}
if counts.isEmpty() {
println("no words")
return
}
for e in counts.entries() {
println("${e.key}: ${e.value}")
}
}
report("to be or not to be")
report(" ")to: 2
be: 2
or: 1
not: 1
no wordsReport the missing fields of a form, or accept it:
func missing(form: Map[string, string], required: Array[string]): Map[string, string] {
let problems: Map[string, string] = Map{}
for name in required {
if (form[name] ?? "").isEmpty() {
problems[name] = "required"
}
}
problems
}
let form = Map{ name: "Ann", email: "" }
let problems = missing(form, ["name", "email"])
println(if problems.isEmpty() { "ok" } else { "fix: ${problems.keys()}" })fix: ["email"]Properties and relations
Map.isEmpty agrees with Map.size, len, Map.keys, and == against an annotated empty map:
let none: Map[string, int] = Map{}
let cases: Array[Map[string, int]] = [Map{}, Map{ a: 1 }, Map{ a: 1, b: 2 }]
for m in cases {
assert(m.isEmpty() == (m.size() == 0))
assert(m.isEmpty() == (len(m) == 0))
assert(m.isEmpty() == m.keys().isEmpty())
assert(m.isEmpty() == (m == none))
}Possible issues
A map is not a condition. Ask Map.isEmpty explicitly:
let m = Map{ a: 1 }
if m { // error: cannot use Map[string, int] where bool is expected
println("has entries")
}An empty map literal has no key and value types to infer. Annotate it:
println(Map{}.isEmpty()) // error: cannot infer the key and value types of an empty map; annotate itAn operand of == gives no expected type, so m == Map{} cannot infer the types either. Use Map.isEmpty:
let m = Map{ a: 1 }
println(m == Map{}) // error: cannot infer the key and value types of an empty map; annotate itA nullable map must be narrowed or reached through ?. first:
let m: Map[string, int] | null = null
println(Map.isEmpty(m)) // error: cannot use Map[string, int] | null where Map[K, V] is expectedMap.isEmpty takes only maps. Arrays, strings, and sets have their own:
println(Map.isEmpty(Set.of([1]))) // error: cannot use Set[int] where Map[K, V] is expectedSee also
Map.size · len · Map.has · Map.remove · Map.filter · Array.isEmpty · Set.isEmpty · String.isEmpty
