I’ve been thinking about this one for a while and I’m still a bit hesitant.
Most of the time I wish for this I’m actually dealing with static keys, and despite being non-optimal the take syntax is much more compact than the pattern-matching version and has much less repetition:
Enum.map(rows, &Map.take!(&1, [:my_long_field, :another_long_field]))vs:
Enum.map(rows, fn %{my_long_field: my_long_field, another_long_field: another_long_field} ->
%{my_long_field: my_long_field, another_long_field: another_long_field}
end)In the absence of Map.take!/2, I sometimes still default to Map.take/2 just for the readability even if I’d like to fail on missing keys. So Map.take!/2 would be an improvement to at least avoid silent failures.
A map_take!/2 macro could generate optimal code for static keys and keep the conciseness (best of both worlds), but I don’t think there’s a place where it fits well (the Map module has no macros, and it would be weird in Kernel perhaps?).
Another way here could be the field punning feature which is frequently discussed (example:
https://groups.google.com/g/elixir-lang-core/c/P6VprVlRd6k/m/q4Jaq49eAgAJ).
Enum.map(rows, fn %{my_long_field, another_long_field} ->
%{my_long_field, another_long_field}
end)TLDR to answer your question Jose: yes I still think Map/Keyword take!/drop! would be useful additions, since the shortcomings also apply to Map.take/2.
But I suspect many actual usages could have a better solution.