5 ms·
Clojure data structures are immutable, so getters and setters are mostly out. In CLojure, maps are functions and keywords are also functions. But you could do:
by LandR 7y ago
Clojure data structures are immutable, so getters and setters are mostly out. In CLojure, maps are functions and keywords are also functions.
But you could do:
(def foo {:color "brown" :name "fred"})
Anytime you want to get the color out you can just call the :color function and pass the map
(:color foo)
=> "brown"
(:name foo)
=> "fred"
Likewise, we can call the map as the function and pass the key as the argument
(foo :name)
=> "fred"
We also have nice ways to destructure and bind values in maps when passed as parameters to functions
(defn my-func [{:keys [color name]}]
(println "Color: " color)
(println "Name: " name))
(my-func foo)
=>
Color: brown
Name: fred