2012-10-10 17 views
7

bir atom saatler tüm anahtarları almanın bir yolu var mı?olsaydı Clojure

(get-watches a) 
;; => [:watcher :watcher2] 

cevap

10

(atom {})clojure.lang.IRef arabirim uygulayan soyut sınıf clojure.lang.ARef uzanan tip clojure.lang.Atom bir nesne oluşturur. IRef, ARef'da uygulanan getWatches yöntemini bildirir.

(def a (atom {})) 
(add-watch a :watcher println) 
(println (-> a .getWatches keys)) 

O clojure.coreget-watches yok garip:

İşte çözüm. Yansıtma add-watch uygulama elde ederiz:

(defn get-watches 
    "Returns list of keys corresponding to watchers of the reference." 
    [^clojure.lang.IRef reference] 
    (keys (.getWatches reference))) 
2
(:watches (bean a)) 

veya

(keys (:watches (bean a))) 
2

Ivan'ın cevabı JVM üzerinde Clojure için mükemmeldir. ClojureScript'te şu şekilde yapın: