Skip to content

Add a dict.map_keys function #834

Open
@llakala

Description

@llakala

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions