Reputation: 14470
I looked at docs but its description does not give any hint/example about usage for index function except the following description.
Usage: (index xrel ks)
Returns a map of the distinct values of ks in the xrel mapped to a
set of the maps in xrel with the corresponding values of ks.
Please share some code examples for index
function
Upvotes: 2
Views: 165
Reputation: 3418
There are several examples available on Grimoire. Grimoire often has more extensive examples than the official Clojure docs.
(use '[clojure.set :only (index)]) ;; Suppose you have a set of descriptions of the weights of animals: user=> (def weights #{ {:name 'betsy :weight 1000} {:name 'jake :weight 756} {:name 'shyq :weight 1000} }) ;; You want the names of all the animals that weight 1000. One way to do ;; that uses `index`. First, you can group the set elements (the maps) ;; so that those with the same weights are in the same group. user=> (def by-weight (index weights [:weight])) #'user/by-weight ;; index returns a map. The keys are maps themselves, where {:weight ;; 756} and {:weight 1000} are taken from the maps in the weights set. The ;; values in the map returned by index are sets that contain map entries ;; from the above weights set. user=> by-weight {{:weight 756} #{{:name jake, :weight 756}}, {:weight 1000} #{{:name shyq, :weight 1000} {:name betsy, :weight 1000}}}
Upvotes: 3