簡體   English   中英

clojure.set索引函數的用法示例

[英]example usage of clojure.set index function

我查看了docs,但除以下描述外,它的描述未提供任何有關索引功能用法的提示/示例。

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功能的代碼示例

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}}} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM