clojure.set 索引函数的用法示例
example usage of clojure.set index function
我查看了 docs,但它的描述没有给出任何 hint/example 关于索引函数的用法,除了下面的描述。
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.
请分享一些 index
函数
的代码示例
有几个examples available on Grimoire。 Grimoire 的示例通常比官方的 Clojure 文档更广泛。
(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}}}
我查看了 docs,但它的描述没有给出任何 hint/example 关于索引函数的用法,除了下面的描述。
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.
请分享一些 index
函数
有几个examples available on Grimoire。 Grimoire 的示例通常比官方的 Clojure 文档更广泛。
(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}}}