auua
Hi guys! I’ve been learning some elixir following Dave’s Programming Elixir 1.6 and I’m lovin it!
I just came across Access.pop/2 and noticed something weird when experimenting with the function.
Access.pop([ a: 1, a: 2], :a)
{1, []}
(The key names aren’t good I know, just keeping it simple. And the example might not be a practical one too, please bear with me.)
I expected the output to be {[1, 2], []}
From the docs,
Removes the entry with a given key from a container (a map, keyword list, or struct that implements the Access behaviour).
Returns a tuple containing the value associated with the key and the updated container. nil is returned for the value if the key isn’t in the container.
In this case, there are multiple values with the same key. Am I wrong in expecting the Access.pop/2 function to return a list of values ([1, 2]) with the key :a?
Or am I missing something obvious? Can someone explain this behavior?
Thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 3 of 3 Posts
NobbZ
The first value for a given key is considered inserted last and therefore “overwrites” any older entry.
alco
[a: 1, a: 2]is a keyword list and most functions working on keyword lists assume them to have unique keys. The introduction in the documentation for the moduleKeywordhas this to say about duplicate keys:axelson
If you want a function that works specifically with the scenario of a keyword list with duplicated keys then should look at
Keyword.pop_first/3. When you use it it looks like this: