-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Access.values/0 #14350
Merged
josevalim
merged 5 commits into
elixir-lang:main
from
xxdavid:add_access_values_and_keys
Mar 24, 2025
Merged
Add Access.values/0 #14350
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68a60eb
Add Access.values/0 and Access.keys/0
xxdavid f7705f4
Add @doc since attributes
xxdavid b118ad6
Drop keys/0
xxdavid 9f0405a
Support for keyword lists + fix order in doctests + add unit tests
xxdavid 8b4aa0f
Assert that all keys in a keyword list are atoms
xxdavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -999,6 +999,126 @@ defmodule Access do | |||||||
raise ArgumentError, "Access.slice/1 expected a list, got: #{inspect(data)}" | ||||||||
end | ||||||||
|
||||||||
@doc """ | ||||||||
Returns a function that accesses all values in a map. | ||||||||
|
||||||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`, | ||||||||
`Kernel.get_and_update_in/3`, and friends. | ||||||||
|
||||||||
See `keys/0` for a function that accesses all keys in a map. | ||||||||
|
||||||||
## Examples | ||||||||
|
||||||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}} | ||||||||
iex> get_in(users, [Access.values(), :age]) | ||||||||
[27, 23] | ||||||||
iex> update_in(users, [Access.values(), :age], fn age -> age + 1 end) | ||||||||
%{"john" => %{age: 28}, "meg" => %{age: 24}} | ||||||||
iex> put_in(users, [Access.values(), :planet], "Earth") | ||||||||
%{"john" => %{age: 27, planet: "Earth"}, "meg" => %{age: 23, planet: "Earth"}} | ||||||||
|
||||||||
By returning `:pop` from an accessor function, you can remove the accessed key and value | ||||||||
from the map: | ||||||||
|
||||||||
iex> require Integer | ||||||||
iex> numbers = %{one: 1, two: 2, three: 3, four: 4} | ||||||||
iex> get_and_update_in(numbers, [Access.values()], fn num -> | ||||||||
...> if Integer.is_even(num), do: :pop, else: {num, to_string(num)} | ||||||||
...> end) | ||||||||
{[1, 2, 3, 4], %{one: "1", three: "3"}} | ||||||||
|
||||||||
An error is raised if the accessed structure is not a map: | ||||||||
|
||||||||
iex> get_in([1, 2, 3], [Access.values()]) | ||||||||
** (RuntimeError) Access.values/0 expected a map, got: [1, 2, 3] | ||||||||
""" | ||||||||
@doc since: "1.19.0" | ||||||||
@spec values() :: Access.access_fun(data :: map(), current_value :: list()) | ||||||||
def values do | ||||||||
&values/3 | ||||||||
end | ||||||||
|
||||||||
defp values(:get, data = %{}, next) do | ||||||||
Enum.map(data, fn {_key, value} -> next.(value) end) | ||||||||
end | ||||||||
|
||||||||
defp values(:get_and_update, data = %{}, next) do | ||||||||
{reverse_gets, updated_data} = | ||||||||
Enum.reduce(data, {[], %{}}, fn {key, value}, {gets, data_acc} -> | ||||||||
case next.(value) do | ||||||||
{get, update} -> {[get | gets], Map.put(data_acc, key, update)} | ||||||||
:pop -> {[value | gets], data_acc} | ||||||||
end | ||||||||
end) | ||||||||
|
||||||||
{Enum.reverse(reverse_gets), updated_data} | ||||||||
end | ||||||||
|
||||||||
defp values(_op, data, _next) do | ||||||||
raise "Access.values/0 expected a map, got: #{inspect(data)}" | ||||||||
end | ||||||||
|
||||||||
@doc """ | ||||||||
Returns a function that accesses all keys in a map. | ||||||||
|
||||||||
The returned function is typically passed as an accessor to `Kernel.get_in/2`, | ||||||||
`Kernel.get_and_update_in/3`, and friends. | ||||||||
|
||||||||
Beware that returning the same key multiple times in `Kernel.put_in/3`, `Kernel.update_in/3`, | ||||||||
or `Kernel.get_and_update_in/3` will cause the previous values of the same key to be | ||||||||
overwritten as maps cannot have duplicate keys. | ||||||||
|
||||||||
See `values/0` for a function that accesses all values in a map. | ||||||||
|
||||||||
## Examples | ||||||||
|
||||||||
iex> data = %{users: %{"john" => %{age: 27}, "meg" => %{age: 23}}} | ||||||||
iex> get_in(data, [:users, Access.keys()]) | ||||||||
["john", "meg"] | ||||||||
iex> update_in(data, [:users, Access.keys()], fn name -> String.upcase(name) end) | ||||||||
%{users: %{"JOHN" => %{age: 27}, "MEG" => %{age: 23}}} | ||||||||
|
||||||||
By returning `:pop` from an accessor function, you can remove the accessed key and value | ||||||||
from the map: | ||||||||
|
||||||||
iex> require Integer | ||||||||
iex> numbers = %{1 => "one", 2 => "two", 3 => "three", 4 => "four"} | ||||||||
iex> get_and_update_in(numbers, [Access.keys()], fn num -> | ||||||||
...> if Integer.is_even(num), do: :pop, else: {num, to_string(num)} | ||||||||
...> end) | ||||||||
{[1, 2, 3, 4], %{"1" => "one", "3" => "three"}} | ||||||||
|
||||||||
An error is raised if the accessed structure is not a map: | ||||||||
|
||||||||
iex> get_in([1, 2, 3], [Access.keys()]) | ||||||||
** (RuntimeError) Access.keys/0 expected a map, got: [1, 2, 3] | ||||||||
""" | ||||||||
@doc since: "1.19.0" | ||||||||
@spec keys() :: Access.access_fun(data :: map(), current_value :: list()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
def keys do | ||||||||
&keys/3 | ||||||||
end | ||||||||
|
||||||||
defp keys(:get, data = %{}, next) do | ||||||||
Enum.map(data, fn {key, _value} -> next.(key) end) | ||||||||
end | ||||||||
|
||||||||
defp keys(:get_and_update, data = %{}, next) do | ||||||||
{reverse_gets, updated_data} = | ||||||||
Enum.reduce(data, {[], %{}}, fn {key, value}, {gets, data_acc} -> | ||||||||
case next.(key) do | ||||||||
{get, update} -> {[get | gets], Map.put(data_acc, update, value)} | ||||||||
:pop -> {[key | gets], data_acc} | ||||||||
end | ||||||||
end) | ||||||||
|
||||||||
{Enum.reverse(reverse_gets), updated_data} | ||||||||
end | ||||||||
|
||||||||
defp keys(_op, data, _next) do | ||||||||
raise "Access.keys/0 expected a map, got: #{inspect(data)}" | ||||||||
end | ||||||||
|
||||||||
defp normalize_range(%Range{first: first, last: last, step: step}, list) | ||||||||
when first < 0 or last < 0 do | ||||||||
count = length(list) | ||||||||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in f7705f4. Thanks for noticing!