-
Notifications
You must be signed in to change notification settings - Fork 126
Add map
and map_with_key
Methods to Map
#2210
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
Conversation
…ence methods, now they can handle branchs
…ence methods, now they can handle branchs
…o feature/20250522_HAMT
Add set operations to @immut/hash{map, set} and @internal/sparse_array Summary and re-fmt
…ap implementation
…empty map handling
…s/core into feature/map-enhancement
Pre-allocation optimization missing for partition methodCategory Fold method documentation could be more descriptiveCategory /// Folds the map from an initial value by applying a function to each key-value pair and the accumulator.
/// The order of folding is not guaranteed.
/// Example:
/// ```
/// let m = {1: 10, 2: 20}
/// let sum = m.fold(0, fn(acc, k, v) { acc + v }) // 30
/// ```
**Reasoning**
More detailed documentation with examples helps users understand the purpose and usage of the method better, especially for functional programming concepts like fold.
</details>
<details>
<summary> Implicit return in for loop could be made explicit </summary>
**Category**
Correctness
**Code Snippet**
for e in self {
acc = f(acc, e.0, e.1)
}
acc
**Recommendation**
Consider making the mutation and return more explicit:
```moonbit
let mut acc = init
for e in self {
acc = f(acc, e.0, e.1)
}
return acc Reasoning |
Pull Request Test Coverage Report for Build 7130Details
💛 - Coveralls |
…s/core into feature/map-enhancement
There are many hash calculations that can be avoided here. |
builtin/builtin.mbti
Outdated
fn[K : Hash + Eq, V] Map::filter(Self[K, V], (K, V) -> Bool?Error) -> Self[K, V]?Error | ||
fn[K : Hash + Eq, V, A] Map::fold(Self[K, V], A, (A, K, V) -> A?Error) -> A?Error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please focus on the functionalities that you described in the introduction; do not add too many stuff in one PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok👍
There are too many unrelated modifications. I'm closing this PR for the new one. |
Summary
Based on #1890
This PR adds two new methods to
Map
inbuiltin/linked_hash_map.mbt
:map(f: (V) -> V2) -> Map[K, V2]
Applies a function to each value in the map and returns a new map with the same keys and transformed values.
map_with_key(f: (K, V) -> V2) -> Map[K, V2]
Applies a function to each key-value pair and returns a new map with the same keys and transformed values.
Implementation Details
map
is implemented by callingmap_with_key
and ignoring the key.Example Usage
Impact
Map
.