Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created November 12, 2013 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiensonqin/7426695 to your computer and use it in GitHub Desktop.
Save tiensonqin/7426695 to your computer and use it in GitHub Desktop.
;; Lists
(def l (list))
(cons 1 l)
(conj (conj l 1) 1)
(rest (conj l 1))
(pop l)
;; vectors
(def v [1 2 3])
(replace {1 :hello 2 :world} v)
(rseq v)
(mapv #(+ %1 %2) v [2 3 4])
(filterv pos? [1 2 3 0 -1])
(defn sum-new
[init k v]
(+ init k v))
(reduce-kv sum-new 10 [1 2 3 4])
;; sets
(def s #{:hello :world})
(contains? s :hello)
(disj #{} :world)
(def first-relation #{{:a 1} {:a 2}})
(def second-relation #{{:b 1} {:b 2}})
(clojure.set/join first-relation second-relation)
(def family
#{{:name "tienson" :weight 140}
{:name "jessica" :weight 100}
{:name "manu" :weight 100}})
(clojure.set/index family [:weight])
(clojure.set/rename-keys {:hello 1 :world 2} {:hello :really})
(clojure.set/rename family {:name :username})
(clojure.set/map-invert {:hello 1 :world 2})
(clojure.set/subset? #{1 2} #{5 1 0 2 3 4})
(clojure.set/superset? #{1 2 3 4 5} #{2 4})
;; maps
(hash-map :hello "world" :really "bingo")
(array-map :hello "world" :really "bingo")
(zipmap [:hello :really] ["world" "bingo"])
(sorted-map :hello 1 :world 2 :a 3)
(frequencies [1 2 3 4 2 3])
((group-by #(> % 5) [1 2 3 4 5 6 7 8 9 10]) false)
(:hello {:hello "world"})
(get-in {:hello {:world "bingo"}} [:hello :world])
(contains? {:hello "world"} "world")
(find {:hello "world" :world "really"} :hello)
(keys {:hello "world" :echo "nice"})
(vals {:hello "world" :echo "nice"})
(def m {:hello "world" :echo "nice"})
(assoc-in {:hello {:world "really"}} [:hello :world] {:a :b})
(dissoc {:hello "world"} :hello)
(merge {:hello "world"} {:nice "bingo"})
(merge-with + {:a 1 :b 2 :c 3} {:a 2 :d 4})
(select-keys {:a 1 :b 2 :c 3} [:a :b])
(update-in {:hello {:world "really"}} [:hello :world] #(str % " world"))
(doseq [[k v] m]
(prn (str k " " v)))
(def sm (sorted-map :a 1 :c 2 :b 3 :d 4 :e 5))
(rseq sm)
(subseq sm >= :b <= :d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment