///|
test "create" {
let empty : @hashmap.HashMap[String, Int] = @hashmap.new()
inspect(empty.length(), content="0")
let single = @hashmap.singleton("a", 1)
debug_inspect(single.get("a"), content="Some(1)")
let from_arr = @hashmap.HashMap([("a", 1), ("b", 2)])
inspect(from_arr.length(), content="2")
}///|
test "add_get_remove" {
let map = @hashmap.new().add("a", 1).add("b", 2)
debug_inspect(map.get("a"), content="Some(1)")
inspect(map.contains("b"), content="true")
inspect(map["a"], content="1")
let map2 = map.remove("a")
debug_inspect(map2.get("a"), content="None")
// Original map is unchanged
debug_inspect(map.get("a"), content="Some(1)")
}///|
test "iteration" {
let map = @hashmap.singleton("x", 10)
let sum = map.fold(init=0, (acc, _k, v) => acc + v)
inspect(sum, content="10")
let keys = map.keys().to_array()
debug_inspect(keys, content="[\"x\"]")
let vals = map.values().to_array()
debug_inspect(vals, content="[10]")
}///|
test "transform" {
let map = @hashmap.HashMap([("a", 1), ("b", 2), ("c", 3)])
let doubled = map.map((_k, v) => v * 2)
debug_inspect(doubled.get("b"), content="Some(4)")
let filtered = map.filter((_k, v) => v > 1)
inspect(filtered.contains("a"), content="false")
inspect(filtered.contains("b"), content="true")
}///|
test "index_access" {
let map = @hashmap.HashMap([("x", 10), ("y", 20)])
@test.assert_eq(map["x"], 10)
@test.assert_eq(map["y"], 20)
}///|
test "iteration_full" {
let map = @hashmap.singleton("a", 1)
// each
let buf = []
map.each(fn(k, v) { buf.push((k, v)) })
debug_inspect(buf, content="[(\"a\", 1)]")
// fold
let sum = map.fold(init=0, fn(acc, _k, v) { acc + v })
@test.assert_eq(sum, 1)
// keys / values
debug_inspect(map.keys().to_array(), content="[\"a\"]")
debug_inspect(map.values().to_array(), content="[1]")
// to_array
debug_inspect(map.to_array(), content="[(\"a\", 1)]")
}///|
test "set_operations" {
let m1 = @hashmap.HashMap([("a", 1), ("b", 2)])
let m2 = @hashmap.HashMap([("b", 20), ("c", 3)])
// Union prefers right map on conflicts
let u = m1.union(m2)
debug_inspect(u.get("b"), content="Some(20)")
debug_inspect(u.get("c"), content="Some(3)")
// union_with: custom merge function
let u2 = m1.union_with(m2, fn(_k, v1, v2) { v1 + v2 })
debug_inspect(u2.get("b"), content="Some(22)") // 2 + 20
// intersection_with: custom merge
let i = m1.intersection_with(m2, fn(_k, v1, v2) { v1 * v2 })
debug_inspect(i.get("b"), content="Some(40)") // 2 * 20
inspect(i.contains("a"), content="false")
// Difference keeps keys only in left
let d = m1.difference(m2)
debug_inspect(d.get("a"), content="Some(1)")
inspect(d.contains("b"), content="false")
}test {
let m = @hashmap.HashMap([(1, "one"), (2, "two")])
@test.assert_eq(m.get(1), Some("one"))
@test.assert_eq(m.get(2), Some("two"))
}#deprecated("Use @immut/hashmap.HashMap([...]) instead")
#as_free_fn(of, deprecated="Use @immut/hashmap.HashMap([...]) instead")
#alias(of, deprecated="Use @immut/hashmap.HashMap([...]) instead")
#as_free_fn(deprecated="Use @immut/hashmap.HashMap([...]) instead")
fn[K : Eq + Hash, V] HashMap::from_array(arr : ArrayView[(K, V)]) -> HashMap[K, V]Install
Installed by default