Fox.MapExt

Utility functions for common extension to maps

Source

Summary

present_put(map, key, value)

Puts the given value to map if and only if the value isn’t one of the following: nil, "", %{}

present_update(map, key, update)

Runs the update function if and only if the map already has the given key

value_map(map, f)

Maps over just the value of your map while keeping all the keys the same

Functions

present_put(map, key, value)

Specs:

Puts the given value to map if and only if the value isn’t one of the following: nil, "", %{}

Examples

%{dog: 1} |> present_put(:cat, %{})
%{dog: 1}

%{dog: 1} |> present_put(:cat, 1)
%{dog: 1, cat: 1}
Source
present_update(map, key, update)

Specs:

Runs the update function if and only if the map already has the given key.

Examples

%{dog: 1, cat: 2} |> present_update(:dog, &(&1 + 1))
# %{dog: 2, cat: 2}

%{dog: 1, cat: 3} |> present_update(:serenade, &(&1 * &1))
# %{dog: 1, cat: 3}
Source
value_map(map, f)

Specs:

Maps over just the value of your map while keeping all the keys the same

Examples

%{dog: 1, cat: 2} |> value_map(&(&1 + 2))
# %{dog: 3, cat: 4}
Source