cold

cold

What is the value of &1 in this Agent.get call?

Hi, I’m studying agents in elixir reading this page https://elixir-lang.org/getting-started/mix-otp/agent.html.
I don’t completely got how is this line working:

                                       Agent.get(bucket, &Map.get(&1, key))

I understand that to use a named function as a parameter we need to use ‘&’ before the function’s name but what does &1 means in this case? Is it the bucket? I tried to verify it by updating a map to the bucket:

                                     Agent.update(bucket, fn _x -> %{milk: 3} end)

And then I used Map.get like this ~> Map.get(bucket, :milk) , so I got the following error:

" ** (BadMapError) expected a map, got: pid<0.100.0>
(elixir) lib/map.ex:437: Map.get(pid<0.100.0>, :milk, nil)"

So I guess that the &1 is not the bucket, so what it is?

Marked As Solved

idi527

idi527

:waving_hand:

&Map.get(&1, key)) gets translated into fn first_arg -> Map.get(first_arg, key) end where first_arg is &1.

So I guess that the &1 is not the bucket, so what it is?

&1 is the state of an agent and in case of your particular agent it is indeed the bucket (as a map). The error is probably due to you using the pid of the agent instead of its state.


iex(1)> {:ok, agent} = Agent.start_link(fn -> _bucket = %{} end)
{:ok, #PID<0.113.0>}
iex(2)> is_pid(agent)
true
iex(3)> is_map(agent)
false
iex(4)> bucket = Agent.get(agent, fn bucket -> bucket end)
%{}
iex(5)> is_map(bucket)
true
iex(7)> Agent.update(agent, fn bucket -> Map.put(bucket, :milk, 3) end)
:ok
iex(8)> Agent.get(agent, fn bucket -> bucket end)
%{milk: 3}
iex(9)> Agent.get(agent, fn bucket -> Map.get(bucket, :milk) end)
3
iex(10)> Agent.get(agent, &Map.get(&1, :milk))
3
iex(11)> Agent.get(agent, & &1)
%{milk: 3}

Also Liked

cold

cold

Now I understand, thanks!

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement