“&”如何操作员工作? [英] How does the '&' operator work?
问题描述
在以下情况下,我无法理解'&'运算符。
I am having trouble understand the '&' operator in the following context.
@doc "Marks a task as executed"
def put_task(task, project) do
item = {task, project}
Agent.update(__MODULE__, &MapSet.put(&1, item))
end
在这种情况下,& 1似乎是指地图对象本身,但是我对此工作方式感到好奇。我是否在文档中对此进行了讨论,但无法确定这是否正是事实呢?如果有人可以帮助我了解到底发生了什么以及& 1所指的内容,以及如果它所指的是MapSet的话,这是怎么可能的,我将不胜感激。
It seems that in this case the '&1' is referring to the map object itself, but I am curious as to how this works. Is it passing itself in as an argument I looked into this in the docs but couldn't find out if this was exactly what was going on. I would be grateful if somebody could help me understand what is exactly going on and what &1 refers to and if it refers to the MapSet how is this possible.
推荐答案
如果有人可以帮助我了解发生了什么,以及; 1引用,如果引用了MapSet,这怎么可能。
I would be grateful if somebody could help me understand what is exactly going on and what &1 refers to and if it refers to the MapSet how is this possible.
Agent.update / 3
调用给定函数当前状态,并将返回值存储为新状态。由于& MapSet.put(& 1,item)
与 fn x->相同, MapSet.put(x,item)结尾
, x
在这里变为旧状态,而新的 MapSet $
MapSet.put / 2
返回的c $ c>成为代理的新状态。
Agent.update/3
calls the given function with the current state, and stores the returned value as the new state. Since &MapSet.put(&1, item)
is the same as fn x -> MapSet.put(x, item) end
, x
here becomes the old state, and the new MapSet
returned by MapSet.put/2
becomes the new state of the agent.
对于此代码要正常运行,必须使用名称为的
和返回 Agent.start
或 Agent.start_link
进行调用:__MODULE __ {:ok,map_set}
的函数,其中 map_set
是任意值 MapSet
在代码中的某个位置。
For this code to function, there must be a call to Agent.start
or Agent.start_link
with name: __MODULE__
and a function that returns {:ok, map_set}
where map_set
is any MapSet
somewhere in the code.
这篇关于“&”如何操作员工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!