Map.remove
func Map.remove[K, V](m: Map[K, V], k: K): V | nullMap.remove removes the entry for k from m and returns its value, or null when k is missing. It mutates m.
Details
- When
Vincludesnull, a result ofnulldoes not tell a removednullfrom a missing key. Ask Map.has first. - A key removed and set again is a new entry at the end of the map's order.
Basic examples
Remove an entry and get its value back:
let ages = Map{ ada: 36, linus: 54 }
Map.remove(ages, "ada") // 36
ages // Map{ "linus": 54 }A missing key gives null and leaves the map as it was:
let ages = Map{ ada: 36 }
Map.remove(ages, "grace") // null
ages // Map{ "ada": 36 }Scope
An empty map has nothing to remove:
let empty: Map[string, int] = Map{}
empty.remove("a") // nullThe other entries keep their order:
let m = Map{ a: 1, b: 2, c: 3 }
m.remove("b") // 2
m // Map{ "a": 1, "c": 3 }Keys compare the way == does, so a fresh record with the same fields finds the entry:
type Point = { x: int, y: int }
let labels = Map{ (Point{ x: 1, y: 2 }): "home" }
labels.remove(Point{ x: 1, y: 2 }) // "home"
labels // Map{}String keys compare by code points, with no normalization:
let m = Map{ "é": 1 }
m.remove("e\u{301}") // null
m.remove("é") // 1When V includes null, a stored null comes back as null too:
let settings: Map[string, string | null] = Map{ theme: null }
settings.remove("theme") // null
settings // Map{}Call it through ., with a pipe, or with named arguments in any order:
let m = Map{ a: 1, b: 2, c: 3 }
m.remove("a") // 1
m | Map.remove("b") // 2
Map.remove(k: "c", m: m) // 3
m // Map{}Applications
Take a value out with a fallback, using ??:
let stock = Map{ apple: 3 }
stock.remove("apple") ?? 0 // 3
stock.remove("pear") ?? 0 // 0Hand out one-time codes, each accepted once:
let codes = Map{ "x7k2": "ada", "p9q4": "linus" }
func redeem(code: string): string {
let user = codes.remove(code) ?? return "invalid or used: ${code}"
"welcome, ${user}"
}
println(redeem("x7k2"))
println(redeem("x7k2"))
println(redeem("zzzz"))welcome, ada
invalid or used: x7k2
invalid or used: zzzzMove an entry to the end, to keep the most recently used key last:
func touch(m: Map[string, int], k: string) {
let v = m.remove(k) ?? return
m[k] = v
}
let recent = Map{ a: 1, b: 2, c: 3 }
touch(recent, "a")
println(recent.keys())["b", "c", "a"]Properties and relations
After the call, m has no entry for k, and the result is what Map.get gave before it:
let m = Map{ a: 1, b: 2 }
for k in ["a", "z"] {
let before = m.get(k)
assert(m.remove(k) == before)
assert(!m.has(k))
}The size drops by one exactly when the key was there:
let m = Map{ a: 1, b: 2 }
for k in ["a", "z"] {
let had = m.has(k)
let size = m.size()
m.remove(k)
assert(m.size() == if had { size - 1 } else { size })
}It removes from the map itself, so every other name for it sees the change:
let m = Map{ a: 1 }
let alias = m
assert(m.remove("a") == 1)
assert(Map.same(alias, m))
alias // Map{}Possible issues
The result is V | null, not V, so it does not go where a V is expected:
let m = Map{ a: 1 }
let n: int = m.remove("a") // error: cannot use int | null where int is expectedGive a fallback with ??, or narrow it first:
let m = Map{ a: 1 }
let n: int = m.remove("a") ?? 0
n // 1The key must have the map's key type:
let m = Map{ a: 1 }
m.remove(1) // error: cannot use int where string is expectedSetting a key to null stores null; use Map.remove to drop the entry:
let m: Map[string, int | null] = Map{ a: 1 }
m["a"] = null
m.size() // 1
m // Map{ "a": null }NaN is not == to itself, so an entry keyed by NaN cannot be removed:
let m = Map{ (Math.NaN): 1 }
m.remove(Math.NaN) // null
m // Map{ NaN: 1 }Removing entries during a for over the map is allowed. An entry removed before the loop reaches it is not visited:
let m = Map{ a: 1, b: 2, c: 3 }
let seen: Array[string] = []
for e in m {
if e.key == "a" {
m.remove("c")
}
seen.push(e.key)
}
seen // ["a", "b"]To remove the entries that match a test, Map.filter returns a new map instead:
let m = Map{ a: 1, b: 2, c: 3 }
m.filter({ x.value != 2 }) // Map{ "a": 1, "c": 3 }See also
Map.set · Map.get · Map.has · Map.filter · Map.size · Set.remove
