[erlang-questions] adding maps:merge_nested/2
Vans S
vans_163@REDACTED
Sat Nov 12 23:05:48 CET 2016
These days I am working more and more with javascript on the front-end, erlang on the back. The map type fits really well and is simple.
I usually try to keep my data flat but often this is not the reality. For example parsing some JSON from the frontend into a map is dead simple, I know I rather just send erlang binary term format, but not everyone has a fully working erlang term serializer for their language.
Also the recent addition of with and without to maps shows that indeed there is a need for more functionality out of maps.
As neoncontrails recently told me, "a list is just a map", let that sink in! Indeed a list is simply a map, with the index being the key. Its maps, maps all the way down, then more maps.
Another useful function is to diff two maps, but this now I think is indeed beyond scope of the maps module.
Also Haskell style lens or similar to make accessing/updating nested properties would be useful, but again beyond the scope of this merge_nested.
PS. And you know what just happened :)
On Saturday, November 12, 2016 4:58 PM, Guilherme Andrade <g@REDACTED> wrote:
Throwing my two cents out there: I would find 'merge_nested' useful (I'm pretty sure I've had the need of something similar in the past) but it looks a bit too specific for the maps module; particularly, one can think of it as a "map of (maps of ...)" method, not simply a generic, flattened maps one.
Then again, the lists module includes (very dearly needed) functions for dealing with 'lists of tuples', so what do I know :)
Perhaps an alternative would be having a merge/N function that receives a custom value-merger function (besides the unmerged maps), and use it recursively to solve your particular problem.
On 12 November 2016 at 20:51, Vans S <vans_163@REDACTED> wrote:
What do you think of a function merge_nested added to maps module?
The function would merge two maps and all nested maps
Example:
#{k1=> v1, k2=> #{k3=> 5, k4=> #{k4=> v2}}}
= maps:merge_nested(
#{k1=> v1, k2=> #{k3=> 5},
#{k1=> v1, k2=> #{k4=> #{k4=> v2}}})
Also perhaps extending maps:merge to maps:merge/1 to allow passing a list:
maps:merge([#{k1=> v1}, #{k2=> v2}, #{k3=> v3}])
______________________________ _________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/ listinfo/erlang-questions
--
Guilherme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161112/3c9b1b87/attachment.htm>
More information about the erlang-questions
mailing list