auua

auua

Access.pop - unexpected behavior

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!

Most Liked

alco

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 module Keyword has this to say about duplicate keys:

For example, Keyword.get/3 will get the first entry matching the given key, regardless if duplicated entries exist. Similarly, Keyword.put/3 and Keyword.delete/3 ensure all duplicated entries for a given key are removed when invoked.

A handful of functions exist to handle duplicated keys, in particular, Enum.into/2 allows creating new keywords without removing duplicated keys, get_values/2 returns all values for a given key and delete_first/2 deletes just one of the existing entries.

NobbZ

NobbZ

The first value for a given key is considered inserted last and therefore “overwrites” any older entry.

axelson

axelson

Scenic Core Team

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:

iex(1)> Keyword.pop_first([a: 1, a: 2], :a)
{1, [a: 2]}

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement