PassiveSupport.Map.change_key
You're seeing just the function
change_key
, go back to PassiveSupport.Map module for more information.
Link to this function
change_key(map, key, fun)
Specs
Returns a new map with key
replaced by new_key
or the return of fun
If key
is not found within map
, returns the map
unaltered.
Useful for when you're shuffling values around inside of a map, or, I dunno, going through your music collection and you discover you accidentally attributed an entire Beatles album to the Monkees.
Although how you did that is beyond me. You monster.
Examples
iex> change_key(%{dog: "rusty"}, :dog, :cat)
%{cat: "rusty"}
iex> change_key(%{dog: "rusty"}, :dog, &(:"best_#{&1}"))
%{best_dog: "rusty"}
iex> change_key(%{1 => "foo", 2 => "bar", 5 => "idklol"}, 1, fn _key, map -> Enum.max(Map.keys(map)) + 1 end)
%{2 => "bar", 5 => "idklol", 6 => "foo"}
iex> change_key(%{cake_boss: "you blew it"}, :no_key_here, :oops)
%{cake_boss: "you blew it"}