📜  Clojure-代理

📅  最后修改于: 2020-11-05 04:06:09             🧑  作者: Mango


正如多次指出的那样,Clojure是一种编程语言,其中许多数据类型是不可变的,这意味着更改变量值的唯一方法是创建一个新变量并为其分配新值。但是,Clojure确实提供了一些可以创建可变状态的元素。我们已经看到,这可以通过原子数据类型来实现。可以通过代理实现的另一种方式。

代理为各个位置提供独立的异步更改。代理在其整个生命周期内都绑定到一个存储位置,并且仅允许该位置由于操作而发生突变(变为新状态)。动作是函数(具有可选的其他参数),这些函数被异步应用于代理的状态,并且其返回值成为代理的新状态。

关于代理,在Clojure中可以进行以下操作。

Sr.No. Operations & Description
1 agent

An agent is created by using the agent command.

2 send

This function is used to send across a value to the agent.

3 shutdown-agents

This function is used to shut down any running agents.

4 send-off

There are instances wherein an agent is assigned a function which is blocking in nature.

5 await-for

Since there is a delay when a value of an agent is updated, Clojure provided a ‘await-for’ function which is used to specify time in milliseconds to wait for the agent to be updated.

6 await

Blocks the current thread (indefinitely!) until all actions dispatched thus far, from this thread or agent, to the agent(s) have occurred. Will block on failed agents.

7 agent-error

Returns the exception thrown during an asynchronous action of the agent, if the agent fails. Returns nil if the agent does not fail.