Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract values from each of the values in a map of maps?

Tags:

clojure

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")?

like image 594
ChrisR Avatar asked Jul 05 '11 07:07

ChrisR


People also ask

How do I extract a value from a map?

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.

How do I print the value of keys on a map?

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.


1 Answers

(defn get-titles [] (map :title (vals distributions)))
like image 114
Goran Jovic Avatar answered Oct 23 '22 03:10

Goran Jovic