I've just started learning Clojure, if I define the following map:
(def distributions {:normal {:title "Normal" :mean 0 :sd 1}
:beta {:title "Beta" :a 1 :b 3}
:gamma {:title "Gamma" :rate 1/2 :shape 1}})
how would I write (defn get-titles [] ...)
a function that would return
("Normal", "Beta", "Gamma")
?
HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
Within the for-each loop, you can use Map's getValue() and getKey() methods to iterate over key-value pairs. This is the most systematic way to print a map and should be used if it is required to print both map keys and values.
(defn get-titles [] (map :title (vals distributions)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With