Hi everyone,
I would like to discuss the possibility of adding two new functions to each
of the Map and Keyword modules; replace_many/2 and update_many/3. Their
purpose is to update maps and keyword lists providing either a keyword list
of the key-value pairs which need to updated, or a list of keys and a
function which operates on the existing values of those keys.
Far too often I find myself needing to call replace!/3 or update!/3 several
times from within a pipeline, or even needing to use a for-comprehension or
Enum.reduce/3 to update a map in "one shot", when it feels like there
should be a function for this.
There are a number of reasons as to why I think these functions should be
considered, but I'll provide only two for now:
1. There is already a way of updating multiple key-value pairs
simultaneously for maps using %{map | k1 => v1, k2 => v2}. But this
unfortunately does not support passing a literal keyword list after the
cons operator.
- My first instinct was to see if I could expand the special update
syntax to handle keyword lists, but I wasn't able to find where it is in
the codebase. If someone could point that out for me because I'd like to
learn how it works, I'd greatly appreciate it.
2. It would be somewhat analogous to Kernel.struct!/2, where keyword
lists can be passed as the second argument to update several fields within
a struct. Seeing as how structs are maps, it only makes sense there should
be a way that maps could be updated in a similar manner from within the Map
module.
I have already implemented the four functions, complete with docs,
examples, and passing tests. But I wanted confirmation from the core team
if a PR is welcome for this addition. Any opinions?
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/7c79a2b8-3a3c-48f9-a4d0-7d2e07c851e4n%40googlegroups.com.