Bruno Lee
Bruno Lee

Reputation: 1977

Clojure - How i can count vector of entry maps

How i can count mobile and web access discarding a nil values from a list of maps? the output should be anything like this " Statistic mobile = 1 web = 2", but all is imutable on other languagens a simple i++ resolve but how is in clojure. thanks.

        def data [{:name "app1" :type "mobile" }
        {:name "site1" :type "web" }
        {:name "site1" :type "web" }
        {:name "boot" :type nil }]

Upvotes: 0

Views: 139

Answers (2)

Ankur
Ankur

Reputation: 33637

user=> (for [[k v] (group-by :type data) :when k] [k (count v)])
(["mobile" 1] ["web" 2])

Upvotes: 1

Rörd
Rörd

Reputation: 6681

(frequencies (map :type data))

gives

{"mobile" 1, "web" 2, nil 1}

Upvotes: 7

Related Questions