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
分享到:
相关推荐
NULL 博文链接:https://clojure.iteye.com/blog/1741375
NULL 博文链接:https://clojure.iteye.com/blog/1742475
一个用于FoundationDB的Java API的精简Clojure包装器。- vedang / clj_fdb-源码
clojure-utils, 各种小型但方便的clojure实用程序函数库 各种小型但方便的Clojure实用程序函数库特别关注:Clojure.java - 用于从Java调用Clojure的实用工具函数的Java类arrays.clj - 操作Java数组core.clj - 应该在...
Clojure入门教程
Clojure学习教程.pdf
扑克问题FIXME:说明安装从下载。用法FIXME:说明$ java -jar poker-problem-0.1.0-standalone.jar [args]选项FIXME:此应用程序接受的选项列表。例子...虫子...任何其他部分你以为可能有用执照版权所有:copyright:...
Clojure入门教程- Clojure – Functional Programming for the JVM中文版
这个库是Java Agent for Memory Measurements的一个瘦包装器。它允许在运行时检查对象及其所有子字段...添加com.clojure-goes-fast/clj-memory-meter到您的依赖项: 更多详情、使用方法,请下载后阅读README.md文件
Clojure入门介绍: Clojure - Functional Programming for the JVM
Clojure学习文档,Clojure-Web开发学习,覆盖了Clojure由浅入深的基本学习
学习: (俄语) (方案)文章 http://www.paradiso.cc/why-use-clojure/ http://www.reddit.com/r/Clojure/ 我使用 Clojure 的前六个月 - 2013.09 集成开发环境灯台开源,看起来很棒! ...
本次演讲中,庄晓丹从AVOSCloud这个完全基于Clojure构建的移动云平台出发,分享Avos团队在Clojure上的实践和经验,为国内Clojure的传播贡献一份力量。
#lang clojure 该项目是Racket中Clojure兼容语言的存根。 它的主要目的是让我练习编写宏和使用Racket的语言扩展工具。... 使用模块/命名空间模拟Clojure命名空间模拟Clojure宏模拟Clojure协议clojure/*下的更多兼容性
Silverlight DataSet 在silverlight中好用的DataSet
jsonista, 用于快速JSON编码和解码的Clojure库 jsonista jsonissa/jsonista/jsoniin, jsonilla/jsonilta/jsonille用于快速JSON编码和解码的Clojure库。显式配置使用 jackson主要用Java编写的速度
Clojure Copyright (c) Rich Hickey. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) ...
[2009] Programming Clojure.(Stuart Halloway).[1934356336].pdf [2010] Functional Programming with Clojure - Simple Concurrency on the JVM.(Tim Berglund, Matthew McCullough).[193650202X].pdf [2010] ...
函数式编程之Clojure.pdf [2015] Clojure for the Brave and True - Learn the Ultimate Language and Become a Better Programmer.(Daniel Higginbotham).[1593275919].rar [2015] Clojure Web Development ...
maven 简单实用的配置,打包,获取svn版本号、仓库等等实用