KitchenSink v0.0.8 KitchenSink.Map
This module is a mixin for the elixir map namespace
Summary
Functions
clean_struct take in a struct and removes the default values from it and returns a map
deep_merge overcomes a limitation of Map.merge in that it will merge trees. deep_merge will attempt to make the minimum possible change when it merges 2 trees
takes in a list of keys, and a value. creates a nested map of each successive key nested as a child, with the most
nested map having the value value
given to the funciton
merge an array of maps! using merge/1
Similar to rename_key/3
Similar to rename_key, however this uses a key_map
that is larger
rename_key remaps a value from one key to another in a map
transforms the values of a map based on a map of functions
transforms the values of a map based on a map of functions
Functions
clean_struct take in a struct and removes the default values from it and returns a map
deep_merge overcomes a limitation of Map.merge in that it will merge trees. deep_merge will attempt to make the minimum possible change when it merges 2 trees.
takes in a list of keys, and a value. creates a nested map of each successive key nested as a child, with the most
nested map having the value value
given to the funciton
merge an array of maps! using merge/1
Example:
iex> import KitchenSink.Map iex> merge [%{a: 1}, %{b: 2}, %{c: 3}, %{d: 4}] %{a: 1, b: 2, c: 3, d: 4}
Similar to rename_key/3
All of the keys that don’t have a remapping are preserved in the returned Map
Similar to rename_key, however this uses a key_map
that is larger.
key_map = %{
old_key1 => new_key1,
old_key2 => new_key2,
}
If any of the values of the key_map
are lists, then the output for that key will be a nested map based on the items
in the list.
The third argument is optional, if you use :prune then the Map returned will only contain the keys that are in the
key_map
rename_key remaps a value from one key to another in a map
Example:
iex> import KitchenSink.Map iex> rename_key %{a: 1, b: 2, c: 3, d: 4}, :a, :z %{z: 1, b: 2, c: 3, d: 4}
transforms the values of a map based on a map of functions.
input is a map of keys to functions, representing a transformer-map. %{ a: a_transform_fun, b: b_transform_fun, … }
output is a function that takes a map and applies each of the transformers in the transformer-map to the corresponding value in the map, outputing a map where each key-value in the map has been transformed. prunes the map so only transformed values are output.
fn(%{a:, b: …}) -> %{a: a_transform_fun(a), b: b_transform_fun(b) …}