Add take!/2 and drop!/2 to Map and Keyword#14884
Closed
wojtekmach wants to merge 4 commits intoelixir-lang:mainfrom
Closed
Add take!/2 and drop!/2 to Map and Keyword#14884wojtekmach wants to merge 4 commits intoelixir-lang:mainfrom
take!/2 and drop!/2 to Map and Keyword#14884wojtekmach wants to merge 4 commits intoelixir-lang:mainfrom
Conversation
Member
|
We should probably then add |
Map.take!/2 and Keyword.take!/2take!/2 and drop!/2 to Map and Keyword
1dcf174 to
4caaf71
Compare
wojtekmach
commented
Oct 31, 2025
Comment on lines
+1318
to
+1331
| def take!(keywords, keys) when is_list(keywords) and is_list(keys) do | ||
| allowed_keys = Keyword.keys(keywords) | ||
|
|
||
| :lists.foreach( | ||
| fn key -> | ||
| if not :lists.member(key, allowed_keys) do | ||
| raise KeyError, key: key, term: keywords | ||
| end | ||
| end, | ||
| keys | ||
| ) | ||
|
|
||
| :lists.filter(fn {k, _} -> :lists.member(k, keys) end, keywords) | ||
| end |
Member
Author
There was a problem hiding this comment.
This is not efficient. To limit traversals, one idea I had was:
def take!(keywords, keys) when is_list(keywords) and is_list(keys) do
take!(keywords, MapSet.new(keys), MapSet.new(), _acc = [])
catch
key ->
raise KeyError, key: key, term: keywords
end
def take!([{key, value} | rest], keys, used_keys, acc) do
if MapSet.member?(keys, key) do
take!(rest, keys, MapSet.put(used_keys, key), [{key, value} | acc])
else
take!(rest, keys, MapSet.put(used_keys, key), acc)
end
end
def take!([], keys, used_keys, acc) do
case MapSet.to_list(MapSet.difference(keys, used_keys)) do
[] ->
:lists.reverse(acc)
[key | _] ->
throw(key)
end
endbut I haven't benchmarked it yet.
Member
There was a problem hiding this comment.
The simplest imlpementation is probably one that tracks which keys have been found and which ones haven't. Something like this:
{taken, not_found, _found} =
Enum.reduce(kw, {[], keys, []}, fn {key, value}, {acc, not_found, found} ->
case pop_member(not_found, key, []) do
{:ok, not_found} -> {[{key, value} | acc], not_found, [key | found]}
:error ->
case :lists.member(key, found) do
true -> {[{key, value} | acc], not_found, found}
false -> {acc, not_found, found}
end
end
end)
if not_found != [] do
raise key not found
end
:lists.reverse(taken)
where:
defp pop_member([key | rest], key, acc), do: {:ok, rest ++ acc}
defp pop_member([head | rest], key, acc), do: pop_member(rest, key, acc)
defp pop_member([], _key, _acc), do: :error
Contributor
There was a problem hiding this comment.
I was thinking the same -- Instead of raising key not found, it would be great to return found and not found keys.
Member
|
I will close this one for now because we cannot actually type this function (and most type systems used at scale can't type it either):
I will write a blog post on the topic and publish it either on dashbit.co or elixir-lang.org. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.