Skip to content

Instantly share code, notes, and snippets.

@KronicDeth
Last active August 29, 2015 14:25
Show Gist options
  • Save KronicDeth/22d790015b5001d61175 to your computer and use it in GitHub Desktop.
Save KronicDeth/22d790015b5001d61175 to your computer and use it in GitHub Desktop.
Map keys and drop unmapped keys with support for nil and false as new keys
t = %{old1: :new1, old2: :new2, old4: nil, old5: false};
IO.inspect Enum.reduce(
%{old1: 1, old2: 2, old3: 3, old4: 4, old5: 5},
%{},
fn ({old_key, value}, new_map) ->
case Map.fetch(t, old_key) do
:error ->
new_map
{:ok, new_key} ->
Map.put(new_map, new_key, value)
end
end
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment