clojure atom 可变

http://clojuredocs.org/clojure_core/clojure.core/atom

  • (atom x)
  • (atom x & options)
Creates and returns an Atom with an initial value of x and zero or
more options (in any order):

:meta metadata-map

:validator validate-fn

If metadata-map is supplied, it will be come the metadata on the
atom. validate-fn must be nil or a side-effect-free fn of one
argument, which will be passed the intended new state on any state
change. If the new state is unacceptable, the validate-fn should
return false or throw an exception.

1Example top

  •  
    01user=> (defmy-atom (atom0))
    02#'user/my-atom
    03  
    04user=> @my-atom
    050
    06  
    07user=> (swap!my-atom inc)
    081
    09  
    10user=> @my-atom
    111
    12  
    13user=> (swap!my-atom (fn[n](* (+ n n)2)))
    144

 

swap!

clojure.core

  • (swap! atom f)
  • (swap! atom f x)
  • (swap! atom f x y)
  • (swap! atom f x y & args)
Atomically swaps the value of atom to be:
(apply f current-value-of-atom args). Note that f may be called
multiple times, and thus should be free of side effects. Returns
the value that was swapped in.

2Examples top

  •  

    01

    user> (defplayers (atom()))

    02

    #'user/players

    03

      

    04

    user> (swap!players conj:player1)

    05

    (:player1)

    06

      

    07

    user> (swap!players conj:player2)

    08

    (:player2:player1)

    09

      

    10

    user> (derefplayers)

    11

    (:player2:player1)

     

  •  

    1

    user> (defcounter (atom0))

    2

    #'user/counter

    3

      

    4

    user> (swap!counter inc)

    5

    1

    6

      

    7

    user> (swap!counter inc)

    8

    2

 

deref

clojure.core

  • (deref ref)
  • (deref ref timeout-ms timeout-val)
Also reader macro: @ref/@agent/@var/@atom/@delay/@future/@promise. Within a transaction,
returns the in-transaction-value of ref, else returns the
most-recently-committed value of ref. When applied to a var, agent
or atom, returns its current state. When applied to a delay, forces
it if not already forced. When applied to a future, will block if
computation not complete. When applied to a promise, will block
until a value is delivered. The variant taking a timeout can be
used for blocking references (futures and promises), and will return
timeout-val if the timeout (in milliseconds) is reached before a
value is available. See also - realized?.

1Example top

  •  
    01user=> (defa (atom0))
    02#'user/a
    03user=> @a
    040
    05user=> (derefa)
    060
    07  
    08user=> (defb (ref1))
    09#'user/b
    10user=> @b
    111
    12user=> (derefb)
    131
    14  
    15user=> (defc (agent2))
    16#'user/c
    17user=> @c
    182
    19user=> (derefc)
    202
    21  
    22user=> (defd (future3))
    23#'user/d
    24user=> @d
    253
    26user=> (derefd)
    273

 

future

clojure.core

  • (future & body)
Takes a body of expressions and yields a future object that will
invoke the body in another thread, and will cache the result and
return it on all subsequent calls to deref/@. If the computation has
not yet finished, calls to deref/@ will block, unless the variant of
deref with timeout is used. See also - realized?.

2 Examples top

  •  
    01;; A future's calculation is started here and it runs in another thread
    02user=> (def f (future (Thread/sleep 10000) (println "done") 100))
    03#'user/f
    04;;if you wait 10 seconds before dereferencing it you'll see "done"
    05  
    06;; When you dereference it you will block until the result is available.
    07user=> @f
    08done
    09100
    10  
    11;; Dereferencing again will return the already calculated value.
    12=> @f
    13100
  •  
    01;; save the example in a script (e.g. test-future.clj) then run it in the console
    02;;
    03;; > clojure test-future.clj
    04  
    05(println "[Main] calculate the answer to life the universe and everything")
    06  
    07;; Used Thread/sleep to simulate long running process
    08(def what-is-the-answer-to-life (future 
    09        (println "[Future] started computation")
    10        (Thread/sleep 3000) ;; running for 3 seconds
    11        (println "[Future] completed computation")
    12        42))
    13          
    14(println "[Main] created future")
    15  
    16(Thread/sleep 1000)
    17(println "[Main] do other things while waiting for the answer")
    18(println "[Main] get the answer")
    19(println "[Main] the result" @what-is-the-answer-to-life)
    20(shutdown-agents)
    21  
    22  
    23;; You may get something like this
    24;;
    25;; [Main] calculate the answer to life the universe and everything
    26;; [Future] started computation
    27;; [Main] created future
    28;; [Main] do other things while waiting for the answer
    29;; [Main] get the answer
    30;; [Future] completed computation
    31;; [Main] the result 42
    32  
    33  
    34;; Note: If you leave out the call to (shutdown-agents), the program will on
    35;; most (all?) OS/JVM combinations "hang" for 1 minute before the process exits.
    36;; It is simply waiting for background threads, created by the future call, to
    37;; be shut down.  shutdown-agents will shut them down immediately, or
    38;; (System/exit <exit-status>) will exit immediately without waiting for them
    39;; to shut down.
    40  
    41;; This wait occurs even if you use futures indirectly through some other Clojure
    42;; functions that use them internally, such as pmap or clojure.java.shell/sh
    43  
    44;; http://dev.clojure.org/jira/browse/CLJ-124 is a ticket opened against Clojure,
    45;; as this 1-minute wait is not considered desirable behavior.

agent

clojure.core

  • (agent state & options)
Creates and returns an agent with an initial value of state and
zero or more options (in any order):

:meta metadata-map

:validator validate-fn

:error-handler handler-fn

:error-mode mode-keyword

If metadata-map is supplied, it will be come the metadata on the
agent. validate-fn must be nil or a side-effect-free fn of one
argument, which will be passed the intended new state on any state
change. If the new state is unacceptable, the validate-fn should
return false or throw an exception. handler-fn is called if an
action throws an exception or if validate-fn rejects a new state --
see set-error-handler! for details. The mode-keyword may be either
:continue (the default if an error-handler is given) or :fail (the
default if no error-handler is given) -- see set-error-mode! for
details.

1 Example top

  •  
    01; Agents provide shared access to mutable state. They allow non-blocking (asynchronous as opposed to synchronous atoms) and independent change of individual locations (unlike coordinated change of multiple locations through refs).
    02  
    03; agent creates one:
    04  
    05user=> (def counter (agent 0))
    06#'user/counter
    07  
    08; send changes its value:
    09user=> (send counter inc)
    10  
    11; @ or deref provides a snapshot of the current state:
    12user=> @counter
    131
    14  
    15; agents can reference any data structure:
    16  
    17user=> (def pulp-fiction (agent {}))
    18#'user/pulp-fiction
    19user=> (send pulp-fiction assoc :act-one "PROLOGUE")
    20user=> @pulp-fiction
    21{:act-one "PROLOGUE"}
    22user=> (send pulp-fiction assoc :act-two "VINCENT VEGA & MARSELLUS WALLACE'S WIFE")
    23user=> @pulp-fiction
    24{:act-two "VINCENT VEGA & MARSELLUS WALLACE'S WIFE", :act-one "PROLOGUE"}
    25  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值