gleam-lang / gleam-lang/stdlib

Add a `dict.map_keys` function

Open
#834 6 comments 2 reactions 0 assignees View on GitHub
Dominant language
Gleam
Stars
710
Forks
225
Avg merge
5h 52m
Merged PRs (30d)
3

Description

I found myself wanting to print out a dictionary with the keys mapped to a string representation (not just through `string.inspect`) for easier readability. However, the `dict` module only provides `dict.map_values`. I wrote up a little function that would accomplish this behavior, based on the existing `do_map_values` function:
```gleam
fn do_map_keys(
func: fn(key) -> new_key,
dict: dict.Dict(key, value),
) -> dict.Dict(new_key, value) {
let fold_func = fn(dict, key, value) { dict.insert(dict, func(key), value) }
dict.fold(dict, from: dict.new(), with: fold_func)
}
```
I think this would be nice to have in the stdlib. Providing a `map_values` without a `map_keys` function feels like an omission, and I think it's reasonable to expect its dual in the stdlib.

If this is decided to be within scope, I can make a PR.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.