[erlang-questions] lists module functions for maps?
zxq9
zxq9@REDACTED
Wed Sep 25 10:22:31 CEST 2019
On 2019/09/24 21:10, Dmitry Belyaev wrote:
> To me presence of lists:key... functions is a serious legacy which is
> justified only because there are tons of code using it.
Lists of records are used all over the place in new code as well and the
lists:key*/N functions are the standard way of dealing with them. Some
shops have simply forgotten that maps are NOT a replacement for records.
The only strange thing about those functions is that they are in the
lists module instead of some module written specifically for
manipulating lists of records.
Lists of maps fall into the same category. It is easy to see how lists
of maps would manifest many places when dealing with data from external
systems (JSON, XML, Python dicts, YAML, etc.). This probably doesn't
belong in the lists module, but something available somewhere would
reduce the noisiness of the code needed to be written for consumers of
that kind of data.
I was stuck at home today sick, so wrote this as a starting
point/substitute/copypasta for anyone who might find it useful:
https://gitlab.com/zxq9/lom/blob/master/src/lom.erl
lom.erl exports
[new/0, store/3, take/3, replace/3, delete/3, find/3, member/3,
keymap/3, sort/2, merge/3]
And as Vance noted earlier, is a very natural thing to pair with a JSON
decoder. The typespec doesn't match Vance's proposal in every case but
only because in some cases you must provide both the key's name and its
value.
-Craig
More information about the erlang-questions
mailing list