clojure.set googides

clojure.set

select

To get elements from a set which pred is true.

;; Instead of using
(set (filter pos? #{1 2 3 -1 -2 0}))
;; use
(clojure.set/select pos? #{1 2 3 -1 -2 0})

project

(project xms ks), equivalent to (set (map #(select-keys % ks) xms)), use it to strip out unwanted key-val pairs in a collection of maps.

(clojure.set/project [{:a 1 :b 2 :c 3} {:a 6 :b 8 :c 9} {:a 3 :b 2 :c 0}] [:a :c])
;; #{{:a 6, :c 9} {:a 1, :c 3} {:a 3, :c 0}}

rename-keys, rename

rename-keys renames keys in a map.

(clojure.set/rename-keys {:a 1 :b 2 :c 3} {:a :e})
;; {:b 2, :c 3, :e 1}

rename renames all keys in a collection of maps.

(clojure.set/rename [{:a 1 :b 2 :c 3} {:a 6 :b 8 :c 9} {:a 3 :b 2 :c 0}] {:a :e})
;; #{{:b 2, :c 0, :e 3} {:b 8, :c 9, :e 6} {:b 2, :c 3, :e 1}}

index

Group a collection of maps by values of keys. Note the following two examples:

(clojure.set/index [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 9} {:a 3 :b 2 :c 0}] [:a :b])
;; {{:a 1, :b 2} #{{:a 1, :b 2, :c 9} {:a 1, :b 2, :c 3}}, {:a 3, :b 2} #{{:a 3, :b 2, :c 0}}}

(group-by (juxt :a :b) [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 9} {:a 3 :b 2 :c 0}])
;; {[1 2] [{:a 1, :b 2, :c 3} {:a 1, :b 2, :c 9}], [3 2] [{:a 3, :b 2, :c 0}]}

map-invert

Returns the map with the vals mapped to the keys.

(clojure.set/map-invert {:a 1 :b 1 :c 3})
;; {1 :b, 3 :c}

join

Join two collection of maps. If an optional key-map is provided, join on the corresponding keys.

(def hd [{:date "2017-08-26" :price 12.2 :code 223} {:date "2017-08-25" :price 12.1 :code 223} {:date "2017-08-25" :price 6.3 :code 221}])
(def basics [{:code 221 :name "jfs"} {:code 223 "dhzy"}])

(clojure.set/join hd basics {:code :code}) ;; or simply (clojure.set/join hd basics)
;; #{{:code 223, :name "dhzy", :date "2017-08-26", :price 12.2} {:code 221, :name "jfs", :date "2017-08-25", :price 6.3} {:code 223, :name "dhzy", :date "2017-08-25", :price 12.1}}

clojure.inspector

(take 5 rows)
;; ({:amount 4.61734176E8, :open 26.3, :date 1198684800000, :close 28.07, :volume 3306181.0, :high 28.07, :id 6783675, :code 2202, :low 26.12} {:amount 1.04054144E9, :open 29.03, :date 1198771200000, :close 27.36, :volume 7270235.0, :high 29.22, :id 6783670, :code 2202, :low 26.72} {:amount 6.9684832E8, :open 27.08, :date 1199203200000, :close 25.52, :volume 5340076.0, :high 27.08, :id 6783665, :code 2202, :low 24.62} {:amount 6.8585011E8, :open 25.91, :date 1199289600000, :close 28.07, :volume 4908126.0, :high 28.07, :id 6783659, :code 2202, :low 25.52} {:amount 6.6101242E8, :open 28.83, :date 1199376000000, :close 29.03, :volume 4372894.0, :high 30.39, :id 6783655, :code 2202, :low 28.46})

(clojure.inspector/inspect rows)
(clojure.inspector/inspect-table rows)
(clojure.inspector/inspect-tree (clojure.reflect/reflect (java.util.Date.)))

clojure.instant

Convert a String to java.util.Date or java.util.Calender or java.sql.Timestamp. If no timezone info is included in the input string, GMT is assumed.

(use 'clojure.instant)
(read-instant-date "2017-08-23T10:22:22")
(read-instant-calendar "2017-08-23T10:22:22")
(read-instant-timestamp "2017-08-23T10:22:22")

clojure.string

Most functions in clojure.string namespace calles Java String methods, passing nil value to these functions will cause NullPointerException.

reverse

(require '[clojure.string :as str])
(str/reverse "h中")
;; "中h"

escape

Escape characters in a string using a character mapping.

(str/escape "1 < 2 " {\< "&lt;"})
;; "1 &lt; 2 "

clojure.walk

walk, postwalk, prewalk

Use walk, postwalk, prewalk to transform nested data structures.

(require '[clojure.walk :as w])
(def m [{:a {:b 1, :c 2}} {:a {:b 1, :c 8}} {:a {:b 3, :c 7}}])
;; Multiple every number in the nested map by two
(w/postwalk #(if (number? %) (* 2 %) %) m)
[{:a {:b 2, :c 4}} {:a {:b 2, :c 16}} {:a {:b 6, :c 14}}]

keywordize-keys, stringify-keys

(defn keywordize-keys
  "Recursively transforms all map keys from strings to keywords."
  [m]
  (let [f (fn [[k v]] (if (string? k) [(keyword k) v] [k v]))]
    (postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))

postwalk-replace, prewalk-replace

(def m [{:a {:b 1, :c 2}} {:a {:b 1, :c 8}} {:a {:b 3, :c 7}}])
(w/postwalk-replace {:a :d :b :e :k :m} m)
;; [{:d {:e 1, :c 2}} {:d {:e 1, :c 8}} {:d {:e 3, :c 7}}]
Comment