You're viewing version 1.2.0 of *agent*. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16



The agent currently running an action on this thread, else nil

1 Example top

  • ;; *agent* is often used with send or send-off to set up a repeated
    ;; transformation of an agent's value. For example, to repeatedly 
    ;; increment the integer value of an agent 'myagent' until some 
    ;; flag value 'running' evaluates to false:
    ;; Create an agent set to an initial value of 0:
    (def myagent (agent 0))
    ;; Define a variable to act as a boolean flag:
    (def running true)
    ;; Define a function to increment agent value repeatedly:
    (defn inc-while-running [agent-value]
      (when running
        (send-off *agent* inc-while-running)) ;sets up another call
      (inc agent-value))
    ;; Dereference myagent to confirm it is set to value 0:
    user=> @myagent
    ;; Start the fun:
    user=> (send-off myagent inc-while-running)
    #<Agent@5fb9f88b: 20>
    ;; The agent has already been incremented many times (20 when I ran this)
    ;; by the time the REPL prints.
    ;; Redefine running as false to stop repeated send-off:
    (def running false)
    ;; Dereference myagent to find its new value:
    user=> @myagent
    ;; Dereference again to make sure incrementation has stopped:
    user=> @myagent
Log in to add / edit an example.

See Also top

  • 0

    Creates and returns an agent with an initial value of state and zer

Log in to add a see also.

Comments top

No comments for *agent*. Log in to add a comment.