very cool - thankyou On Wednesday, October 23, 2013 5:46:00 PM UTC+11, puzzler wrote: > > https://github.com/clojure/data.priority-map/ > > A priority map is similar to a sorted map, but sorts the entries by the > values rather than the keys in the map. Think of it as a kind of priority > queue with a full map-like API to query, add, adjust, and remove items and > their priorities. > > The new 0.0.3 version addresses a common need where the items map to > complex values which contain the priority, or from which the priority can > be derived. > For example, consider a map: > {:apple {:weight 2, :color :red}, :orange {:weight 2, color :orange}, > :banana {:weight 3, color :yellow}} > where you want to sort the map by the fruit's weight. > > A common pitfall was to try to solve this with a custom comparator which > compares only on weight. This doesn't work properly because of the > Clojure/Java rule that valid comparators must be "total orders". Among > other things, this means the comparator can't have "ties" between unequal > objects (known as the trichotomy property). > > [See > https://groups.google.com/forum/#!msg/clojure/VKvH_eg_FtE/Kpy18zGtio4Jfor one > such discussion about this pitfall] > > Users were instructed to rework their comparator into a total order, but > for some use cases, this can be a tricky proposition. > > To address this need, the new version allows one to specify a "keyfn" > which extracts or computes the sort keys (i.e., priority) from the map's > values. > > This is done with one of two new constructors: > priority-map-keyfn (takes a custom keyfn) > priority-map-keyfn-by (takes a custom keyfn and custom comparator) > > So for example, the above map could be expressed as: > (priority-map-keyfn :weight > :apple {:weight 2, :color :red}, :orange {:weight 2, color :orange}, > :banana {:weight 3, color :yellow}) > > See the README for further explanation, and let me know if you have any > questions. >
-- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.