Clojure (12) -- map/pmap

Source: Internet
Author: User
Tags map data structure

Common Methods for clojure map data structure:


Map sorting

(Println (sorted-map-by> 1 "a", 2 "B", 3 "c"); {3 c, 2 B, 1 };; take one equal value (let [results {: A 1: B 2: C 2: D 5: E 1: F 1}] (println (into (sorted-map-by (fn [key1 key2] (compare (get results key2) (get results key1 )))) results); {: D 5,: C 2,: A 1}; all unequal values (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare (get results key2) (get results key1) results); {: D 6,: C 5,: F 4,: E 3,: B 2 ,: A 1}; value Ascending Order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results key1) key1] [(get results key2) key2]) results); {: A 1,: B 2,: E 3,: F 4,: C 5,: D 6 };; value in descending order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results key2) key2] [(get results key1) key1]) results); {: D 6,: C 5,: F 4,: E 3,: B 2,: A 1 };; value Ascending Order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key1) key1] [(get results: key2) key2]) results); {: A 1,: B 2,: C 5,: D 6,: E 3,: F 4 };; value in descending order (let [results {: A 1: B 2: F 4: E 3: D 6: C 5}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key2) key2] [(get results: key1) key1]) results); {: F 4,: E 3,: D 6,: C 5,: B 2,: A 1 };; default Ascending Order (println (into (sorted-map) {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0,: 2013-12-18 0 ,: 2013-12-19 3600}); {: 2013-12-18 0,: 2013-12-19 3600,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0 };; take the last one after sorting (println (last (into (sorted-map) {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0 ,: 2013-12-18 0,: 2013-12-19 3600}); [: 2013-12-24 0]; sort custom keys in descending order (let [results {: 2013-12-23 0,: 2013-12-22 0 ,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-24 0,: 2013-12-18 0 ,: 3600}] (println (into (sorted-map-by (fn [key1 key2] (compare [(get results: key2) key2] [(get results: key1) key1]) results); {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0}


Values after sorting

; Sort dates first, then take the previous num Day (defn take-sort-data [data num] (let [result-sorted (into (sorted-map-by (fn [key1 key2] (compare [( get data: key2) key2] [(get data: key1) key1]) data );; result (select-keys result-sorted (take num (keys result-sorted)] (println "data =" data) (println "result-sorted =" result-sorted) (println "result =" result) (println "json/write-str result =" (json/write-str result) (let [data {: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0 ,: 3600,: 0,: 2013-12-18 0,: 2013-12-19 3600}] (take-sort-data 6) (take-sort-data 4 ));; (take-sort-data 6)'s result; data = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-18 0,: 2013-12-19 3600}; result-sorted = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0 }; result = {: 2013-12-19 3600,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0}; json/write-str result = {"2013-12-19 ": 3600, "2013-12-20": 3600, "2013-12-21": 0, "2013-12-22": 0, "2013-12-23": 0, "2013-12-24": 0 };;; (take-sort-data 4)'s result; data = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-20 3600,: 2013-12-21 0,: 2013-12-22 0,: 2013-12-18 0,: 2013-12-19 3600}; result-sorted = {: 2013-12-24 0,: 2013-12-23 0,: 2013-12-22 0,: 2013-12-21 0,: 2013-12-20 3600,: 2013-12-19 3600,: 2013-12-18 0 }; result = {: 2013-12-21 0,: 2013-12-22 0,: 2013-12-23 0,: 2013-12-24 0}; json/write-str result = {"2013-12-21": 0, "2013-12-22": 0, "2013-12-23": 0, "2013-12-24": 0}



Sorted-map

Sorted-map-

Compare


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.