Hello!
maps_in is a library to handle nested maps. It contains almost the same functions as the standard maps module, the difference is that the key is always a list, for example:
get/2
1> Map = #{my => #{nested => map}}.
#{my => #{nested => map}}
2> maps_in:get([my, nested], Map).
map
put/3
1> Map = #{my => #{more => #{deep => #{}}}}.
#{my => #{more => #{deep => #{}}}}
2> maps_in:put([my, more, deep], #{nested => map}, Map).
#{my => #{more => #{deep => #{nested => map}}}}
update_with/3
1> Map = #{someone => #{age => 17}}.
#{someone => #{age => 17}}
2> maps_in:update_with([someone, age], fun(Age) -> Age + 1 end, Map).
#{someone => #{age => 18}}
I was facing some trouble creating dynamic functions using maps in Zotonic templates, so I decided to create this helper library.
The _in
functions family from Elixir Kernel (get_in, put_in, update_in, etc.) was the lib name’s inspiration.
List of functions:
- filter/3
- filtermap/3
- find/3
- fold/4
- foreach/3
- get/2
- get/3
- keys/2
- is_key/3
- iterator/2
- map/3
- merge/3
- merge_with/4
- put/3
- remove/3
- size/2
- take/3
- to_list/2
- update/3
- update_with/3
- update_with/4
- values/2
- with/3
- without/3
It’s also available in hex.
If you find some of the functions really useful, leave a comment and maybe a proposal for the OTP lib can be made.
Feels free to contribute o/
Note: docs and tests should be improved.