stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Add a `dict.map_keys` function

Open llakala opened this issue 6 months ago • 5 comments
trafficstars

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:

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.

llakala avatar May 19 '25 11:05 llakala