Clojure 學習入門(12)—— map/pmap

來源:互聯網
上載者:User

clojure map資料結構的常用方法,小結一下


map 各種排序

(println (sorted-map-by > 1 "a", 2 "b", 3 "c"))     ;; {3 c, 2 b, 1 a};; 相等 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};; 全不等 value (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 升序(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 降序(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 升序(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 降序(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};; 預設升序排列(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};; 取排序後最末一個(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];; 自訂key降序排序(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, :2013-12-19 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}


先排序後取值

;; 先對日期排序,然後取前num天(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, :2013-12-20 3600, :2013-12-24 0, :2013-12-18 0, :2013-12-19 3600}]  (take-sort-data  data 6)  (take-sort-data  data 4)  );; (take-sort-data  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  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-by

compare


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.