如何访问原子地图内的地图?

时间:2013-01-09 11:31:52

标签: clojure

我正在尝试访问原子地图中的地图。我有这个:

(def database (atom {:henk {:username "henk" :password "johnson" :session "test"}
:steve {:username "steve" :password "boldwin" :session "test2"}
:cane {:username "cane" :password "john" :session "test3"}}))

我尝试过使用swap!在dosync内,但我似乎无法访问所需的值。 你能救我吗?

1 个答案:

答案 0 :(得分:4)

 user=> (def database (atom {:henk {:username "henk" :password "johnson" :session "test"}
                             :steve {:username "steve" :password "boldwin" :session "test2"}
                             :cane {:username "cane" :password "john" :session "test3"}}))
 #'user/database
 user=> (:henk @database)
 {:session "test", :username "henk", :password "johnson"}

修改:

user=> (swap! database assoc-in [:henk :session] "test + swap")
{:cane {:session "test3", :username "cane", :password "john"}, 
 :henk {:session "test + swap", :username "henk", :password "johnson"}, 
 :steve {:session "test2", :username "steve", :password "boldwin"}}

检查值是否已更新:

user=> @database
{:cane {:session "test3", :username "cane", :password "john"}, 
 :henk {:session "test + swap", :username "henk", :password "johnson"}, 
 :steve {:session "test2", :username "steve", :password "boldwin"}}