timbot
Hi there,
I am currently learning Elixir by going through the Mix and OTP guide on elixir-lang.org. I have read through the Getting Started guide already.
I am having trouble getting my head around the use of placeholder arguments when capturing and partially applying named functions. The specific examples that are confusing me are on this page here https://elixir-lang.org/getting-started/mix-otp/agent.html
def get(bucket, key) do
Agent.get(bucket, &Map.get(&1, key))
end
def put(bucket, key, value) do
Agent.update(bucket, &Map.put(&1, key, value))
end
If I understand correctly, the & in &Map.get and &Map.put is capturing the named functions so that they can be passed as arguments to the Agent.get and Agent.update functions. This makes sense to me. But the &1 is causing confusion.
I think the &1 is evaluating to bucket in each of these examples. I don’t see what else could be happening here. But if this is the case, then why can’t we just write bucket instead of &1? Wouldn’t that be a whole lot clearer?
I guess I could just carry on with the tutorial without properly understanding this point, but it seems like this is a crucial enough concept to ask an embarrassingly noob question on these forums.
Thanks in advance for helping me to understand this.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 10 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
timbot
Thanks for the explanations, folks.
I have added a pull request to the documentation over at Github adding a clarifying sentence about this behavior of the Agent.
lucaong
The capture operator is just a different way to write anonymous functions, but it is perfectly equivalent to the
fn ... -> ... endform. The&1,&2, etc. are nothing else that the first argument, the second argument, etc.:The
&1,&2, etc. have no direct relation with the context around the function. They are merely the arguments explicitly passed to the anonymous function when it is called:That said, I do think that the capture operator often makes code more obscure, so I personally prefer to use the extended
fn ... -> ... endform, even if that is slightly longer.kokolegorille
That is the power of function as first class citizen, You can pass the implementation as an argument
benwilson512
There is nothing magic about how &1 is working here. Rather, it’s the Agent.get function that is doing the work.
&Map.get(&1, key)is just an anonymous function that takes a map and grabs a key from it. For example:In this example,
&1becomes whatever you pass to it, in my casemapWhen you do:
This is exactly equivalent to just doing:
where
funin that example is 100% identical to thefunin my example.SO how does it get the process state? Well, that’s the job of
Agent.get. It takes that anonymous functionsends it to the process, the process callsfunwith its state, andsends the result back to you.timbot
Oh! Now I see. The
&1is theAgent’s state, which is being passed from theAgentto the anonymous function.Maybe its just me, but that seems to be a quite different behaviour for the
&1than it had in the other examples.kokolegorille
An agent store some state…
timbot
The state of the agent? Can you please elaborate?
kokolegorille
It is the state of the Agent that get passed to the anonymous function.
timbot
Hello kokolegorille,
Thanks for your reply and the explanation of how the ampersand
&works. In general, it makes sense to me, like in the following example:I have no problems with that.
However, I am still confused by the examples of code in my original question, though, so let me try to ask more specifically about what I’m confused about.
What is the value in the placeholder
&1being passed to the function&Map.getin the following example?kokolegorille
Hello and welcome,
& is a shortcut for defining anonymous function, and &1, &2… are the parameters to this function.
You might find version with two parameters, in particular with reduce…
For example.
More on this here