Prelude.Map

Functions operating on maps.

Source

Summary

append_list(map, key, val)

Appends to an array value in a map, creating one if the key does not exist

atomify(map)

Turns all string map keys into atoms, leaving existing atoms alone (only top level)

deep_put(map, path, val)

Put an arbitrarily deep key into an existing map

del_in(object, path, item)

Remove a map key arbitrarily deep in a structure, similar to put_in

group_by(maps, groups)

Group a map by an array of keys

stringify(map)

Turns all atom map keys into strings, leaving existing strings alone (only top level)

switch(map)

Switch the keys with the values in a map

to_atom(x)

Converts strings to atoms, but leaves existing atoms alone

Functions

append_list(map, key, val)

Appends to an array value in a map, creating one if the key does not exist

Source
atomify(map)

Turns all string map keys into atoms, leaving existing atoms alone (only top level)

Source
deep_put(map, path, val)

Put an arbitrarily deep key into an existing map

If a value already exists at that level, it is turned into a list

For example:

iex> map_deep_put(%{}, [:a, :b, :c], "0")
%{a: %{b: %{c: "0"}}}

iex> map_deep_put(%{a: %{b: %{c: "1"}}}, [:a, :b, :c, :d], "2")
%{a: %{b: %{c: [{:d, "2"}, "1"]}}}
Source
del_in(object, path, item)

Remove a map key arbitrarily deep in a structure, similar to put_in

For example:

iex> a = {a: %{b: %{c: %{d: 1, e: 1}}}}
...> del_in(a, [:a, :b, :c], :d)
%{a: %{b: %{c: %{e: 1}}}}
Source
group_by(maps, groups)

Group a map by an array of keys

Provide a list of maps, and a list of keys to group by. All maps must have all the group_by fields, other fields can vary.

For example:

iex> group_by(
...>  [%{name: "stian", group: 1, cat: 2},
...>   %{name: "per",   group: 1, cat: 1}],
...>  [:group, :cat])
%{1 =>
  %{1 => %{cat: 1, group: 1, name: "per"},
    2 => %{cat: 2, group: 1, name: "stian"}}}
Source
stringify(map)

Turns all atom map keys into strings, leaving existing strings alone (only top level)

Source
switch(map)

Switch the keys with the values in a map

Source
to_atom(x)

Converts strings to atoms, but leaves existing atoms alone

Source