8000 feat(Map): add `map` and `copy` for `Map` by peter-jerry-ye · Pull Request #2228 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(Map): add map and copy for Map #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ fn[K, V] Map::capacity(Self[K, V]) -> Int
fn[K, V] Map::clear(Self[K, V]) -> Unit
fn[K : Hash + Eq, V] Map::contains(Self[K, V], K) -> Bool
fn[K : Hash + Eq, V : Eq] Map::contains_kv(Self[K, V], K, V) -> Bool
fn[K, V] Map::copy(Self[K, V]) -> Self[K, V]
fn[K, V] Map::each(Self[K, V], (K, V) -> Unit raise?) -> Unit raise?
fn[K, V] Map::eachi(Self[K, V], (Int, K, V) -> Unit raise?) -> Unit raise?
fn[K : Hash + Eq, V] Map::from_array(Array[(K, V)]) -> Self[K, V]
Expand All @@ -268,6 +269,7 @@ fn[K, V] Map::is_empty(Self[K, V]) -> Bool
fn[K, V] Map::iter(Self[K, V]) -> Iter[(K, V)]
fn[K, V] Map::iter2(Self[K, V]) -> Iter2[K, V]
fn[K, V] Map::keys(Self[K, V]) -> Iter[K]
fn[K, V, V2] Map::map(Self[K, V], (K, V) -> V2) -> Self[K, V2]
fn[K, V] Map::new(capacity~ : Int = ..) -> Self[K, V]
fn[K : Hash + Eq, V] Map::of(FixedArray[(K, V)]) -> Self[K, V]
#deprecated
Expand Down
63 changes: 63 additions & 0 deletions builtin/linked_hash_map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,66 @@ pub fn[K : Hash + Eq, V] Map::from_iter(iter : Iter[(K, V)]) -> Map[K, V] {
pub impl[K, V] Default for Map[K, V] with default() {
Map::new()
}

///|
/// Applies a function to each key-value pair in the map and returns a new map with the results, using the original keys.
pub fn[K, V, V2] Map::map(self : Map[K, V], f : (K, V) -> V2) -> Map[K, V2] {
// copy structure
let other = {
capacity: self.capacity,
entries: FixedArray::make(self.capacity, None),
size: self.size,
capacity_mask: self.capacity_mask,
grow_at: self.grow_at,
head: None,
tail: self.tail,
}
if self.size == 0 {
return other
}
guard self.entries[self.tail] is Some(last)
loop (last, self.tail, None) {
({ prev, psl, hash, key, value, .. }, idx, next) => {
let new_value = f(key, value)
let new_entry = { prev, next, psl, hash, key, value: new_value }
other.entries[idx] = Some(new_entry)
if prev != -1 {
continue (self.entries[prev].unwrap(), prev, Some(new_entry))
} else {
other.head = Some(new_entry)
}
}
}
other
}

///|
/// Copy the map, creating a new map with the same key-value pairs and order of insertion.
pub fn[K, V] Map::copy(self : Map[K, V]) -> Map[K, V] {
// copy structure
let other = {
capacity: self.capacity,
entries: FixedArray::make(self.capacity, None),
size: self.size,
capacity_mask: self.capacity_mask,
grow_at: self.grow_at,
head: None,
tail: self.tail,
}
if self.size == 0 {
return other
}
guard self.entries[self.tail] is Some(last)
loop (last, self.tail, None) {
({ prev, psl, hash, key, value, .. }, idx, next) => {
let new_entry = { prev, next, psl, hash, key, value }
other.entries[idx] = Some(new_entry)
if prev != -1 {
continue (self.entries[prev].unwrap(), prev, Some(new_entry))
} else {
other.head = Some(new_entry)
}
}
}
other
}
48 changes: 48 additions & 0 deletions builtin/linked_hash_map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,51 @@ test "map::contains_kv" {
fail("map is not { \"a\": 1, \"b\": 2, \"c\": 3 }")
}
}

///|
test "Map::map" {
let map = { "a": 1, "b": 2, "c": 3 }
let v = map.map(fn(k, v) { k + v.to_string() })
inspect(
v,
content=
#|{"a": "a1", "b": "b2", "c": "c3"}
,
)
map["d"] = 10
map["e"] = 20
map.remove("c")
let v = map.map(fn(k, v) { k + v.to_string() })
inspect(
v,
content=
#|{"a": "a1", "b": "b2", "d": "d10", "e": "e20"}
,
)
let v : Map[String, String] = {}.map(fn(k, v) { k + v })
inspect(v, content="{}")
}

///|
test "Map::copy" {
let map = { "a": 1, "b": 2, "c": 3 }
let copy = map.copy()
inspect(
copy,
content=
#|{"a": 1, "b": 2, "c": 3}
,
)
map["d"] = 10
map["e"] = 20
map.remove("c")
let copy = map.copy()
inspect(
copy,
content=
#|{"a": 1, "b": 2, "d": 10, "e": 20}
,
)
let copy : Map[String, String] = {}.copy()
inspect(copy, content="{}")
}
0