简体   繁体   中英

Clojure: Working with vector and ref

Please someone who worked with ref and vector could help-me ?

This vector in each line there is a (ref {:desc "text" :amount double}) :

[#<Ref@1cda773e: {:desc "DESC1", :amount 100}
 #<Ref@468dd81: {:desc "DESC2", :amount 200}
 #<Ref@1e95e716: {:desc "DESC3",:amount -50}>]

Is it possible take all amounts above and put it in a new vector like this : [ 100, 200, -50] .

Working with refs is quite difficult and I don't know to to access them.

Here's one way to do it:

user=> (def data [{:desc "DESC1", :amount 100} {:desc "DESC2", :amount 200} {:desc "DESC3",:amount -50}])
#'user/data
user=> (def my-refs (map #(ref %) data))
#'user/my-refs
user=> (vec (map #(-> % deref :amount) my-refs))
[100 200 -50]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM