ClojureDocs

Nav

Namespaces

send

clojure.core

Available since 1.0 (source)
  • (send a f & args)
Dispatch an action to an agent. Returns the agent immediately.
Subsequently, in a thread from a thread pool, the state of the agent
will be set to the value of:
 (apply action-fn state-of-agent args)
2 Examples
user=> (def my-agent (agent 100))
#'user/my-agent
user=> @my-agent
100

;; Note the following happens asynchronously in a thread
;; pool
user=> (send my-agent + 100)
#<Agent@5afc0f5: 200>

;; Assuming the addition has completed the value will
;; now be updated when we look at it.
user=> @my-agent
200
;; update agent value
user => (def foo (agent 100)) 
#'user/my-agent
user => @foo
100

;; function get "old value"
user => (send foo (fn [old-foo] 
                      (println  old-foo "foo will change")
                      (+ old-foo 100)))
100 foo will change
#agent[{:status :ready, :val 200} 0x000000]

user => @foo
200
See Also

Initiates a shutdown of the thread pools that back the agent system. Running actions will complete...

Added by gstamp

Dispatch a potentially blocking action to an agent. Returns the agent immediately. Subsequently, i...

Added by gstamp

Creates and returns an agent with an initial value of state and zero or more options (in any order...

Added by gstamp
1 Note
    By , created 10.4 years ago

    See "send-off" for the differences between "send" and "send-off".