I was going to suggest something very similar to `Map.rename_key/3`.

To add to what Jonar already said, I think that the big plus of a 
`Map.rename_key/3` would be the readability of pipe chains, specially when 
you have to do a few renames. For example:

```
# Before
map
|> Map.put(:new_key, map.old_key)
|> Map.delete(:old_key)

# After
map
|> Map.rename_key(:old_key, :new_key)
```

At the end of the day this is just a bit of syntactic sugar over functions 
that already exist, so I'm curious to know what the core team thinks about 
bringing something like this into the language.

I'm happy to open a PR if we think it makes sense to do this.

On Wednesday, 19 August 2020 at 19:53:29 UTC+1 [email protected] wrote:

> I think that this can be done as a library. I’ve contributed to one 
> https://github.com/byjpr/MapRewire that works pretty well. I’ve got 
> another library that I need to extract from our source code and release as 
> a package, but it *could* probably be extended (trivially) to allow key 
> renames as well as its current function (safe atomization of map keys as 
> well as case conversion).
>
> -a
>
> On Mon, Aug 17, 2020 at 5:35 PM Jonathan Arnett <[email protected]> 
> wrote:
>
>> I often times find myself in situations where I need to rename one or 
>> more keys in a Map, when for instance, converting from Strings to Atoms:
>>
>> new_map = %{
>>   foo: Map.fetch!(old_map, "foo"),
>>   bar: Map.fetch!(old_map, "bar"),
>>   ...
>> }
>>
>> or otherwise taking a Map from one part of the system and renaming keys 
>> to make it work in another part of the system:
>>
>> new_map = %{
>>   user_id: post.author_id,
>>   content: post.body,
>>   ...
>> }
>>
>> With a Map.rename_key/3 function, we could instead write: 
>>
>> new_map =
>>   old_map
>>   |> Map.rename_key("foo", :foo)
>>   |> Map.rename_key("bar", :bar)
>>   ...
>>
>> or:
>>
>> new_map =
>>   post
>>   |> Map.rename_key(:user_id, :author_id)
>>   |> Map.rename_key(:body, :content)
>>   ...
>>
>> In the situation that you're performing a bulk update of key names (as 
>> seems to be implied above), Map.rename_keys/3 makes more sense:
>>
>> new_map = Map.rename_keys(old_map, %{"foo" => :foo, "bar" => :bar, ...})
>>
>> or:
>>
>> new_map = Map.rename_keys(post, %{author_id: :user_id, body: :content, 
>> ...})
>>
>> With the Map.rename_key[s]/3 functions, the intent is very explicit, and 
>> induces less cognitive load to understand.
>>
>> Renaming Map keys is a situation I run into at least monthly, and I do 
>> wonder whether the rest of the community has this problem as well so as to 
>> make it a worthwhile addition.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "elixir-lang-core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/elixir-lang-core/70108df1-6c5e-475b-8dbd-b4222e920e0bo%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/elixir-lang-core/70108df1-6c5e-475b-8dbd-b4222e920e0bo%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Austin Ziegler • [email protected][email protected]
> http://www.halostatue.ca/http://twitter.com/halostatue
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/d6276452-1e72-4e79-9b25-614c69d7648an%40googlegroups.com.

Reply via email to