change struct-object state
I would like to change the state of the objects andre and blastoise, adding a new property(attribute/state) to the object... the name of this new property I want to add is "tax". I tryed the code below but didnt work out... help me plz:
(def andre {:owner "Andre" :ty开发者_开发问答pe "car" :cur-speed 101 :license-plate "ABC"})
(def blastoise {:owner "Blastoise" :type "truck" :cur-speed 120 :license-plate "XYZ"})
(def car-tax[andre blastoise])
(defn calculate-car-tax [v]
(for [element v]
(if (> (element :cur-speed) 100) (dosync (alter element tax :20)))
)
)
(calculate-car-tax car-tax)
try
(assoc andre :tax 20)
From the docs:
user=> (doc assoc)
-------------------------
clojure.core/assoc
([map key val] [map key val & kvs])
assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector).
nil
user=>
Edit to include function:
(defn calculate-car-tax [v]
(for [element v]
(if (> (element :cur-speed) 100)
(dosync (assoc element :tax 20)))))
精彩评论