(defmacro named [kwargs defaults & body] `(let [{:keys [~@(take-nth 2 defaults)], :or ~(apply array-map defaults)} (apply array-map ~kwargs)] ~@body))
#'user/named
(let [[date d m y :as foo] ((comp re-find re-matcher) #"(\d{1,2})\/(\d{1,2})\/(\d{4})" "12/02/1975")] foo)
["12/02/1975" "12" "02" "1975"]
(let [f #(* % %) g (fn [f x] {:input x, :output (f x)})] (map #(g f %) (range 5)))
({:input 0, :output 0} {:input 1, :output 1} {:input 2, :output 4} {:input 3, :output 9} {:input 4, :output 16})
(let [a (set (range 0 1e5)) b (set (range 0 1e5 3))] (reduce #(if (a %2) (inc %) %) 0 b))
33334(let [[match x y z] (re-find #"x(.*)y(.*)z(.*)w" "x123y45z6w")] {:match match, :x x, :y y, :z z})
{:match "x123y45z6w", :x "123", :y "45", :z "6"}
(mapcat (fn [m] (let [k (key m) vx (val m)] (zipmap vx (repeat k)))) {:a [1 2 3], :b [4 5 6]})
([1 :a] [2 :a] [3 :a] [4 :b] [5 :b] [6 :b])
(let [s [1 2 3 4 5 4 5 6 7 6 7 8]] (every? true? (map > s (cons 0 s))))
false(let [sds [[:a :b :c] [:d :e :f :g]]] (doseq [i (range (count sds))] (-> ((vec sds) i) rest rest first println)))
nil":c\n:f\n"(let [songs (repeatedly 1000 #(rand-int 1e8)) fingerpseq (map #(rem % 10) songs) fingerpmap (zipmap fingerpseq songs)] (vals fingerpmap))
(91981830 24399087 4324051 11295574 34367456 91618083 18476742 19663139 67028625 966998)
(prn (let [xs [3 2 1]] (mapcat (fn [[x idx]] (for [y (range x)] (assoc xs idx y))) (map vector xs (range)))))
nil"([0 2 1] [1 2 1] [2 2 1] [3 0 1] [3 1 1] [3 2 0])\n"((fn linspace [min max n] (let [step (/ (- max min) (dec n))] (range min (+ max step) step))) 0 20 3)
(0 10 20)
(println ",(let [bindings \"should have an even number of forms\"\ufeff bindings \ufeff\"BETTER HAVE AN EVEN NUMBER OF FORMS\"\ufeff bindings] bindings)")
nil",(let [bindings \"should have an even number of forms\" bindings \"BETTER HAVE AN EVEN NUMBER OF FORMS\" bindings] bindings)\n"(defn safe-comp [& fs] (fn [& args] (reduce #(let [r (apply %2 %1)] (if (nil? r) (reduced nil) [r])) args fs)))
#'user/safe-comp
(let [ensure-coll #(if (coll? %) % (vector %))] (merge-with #(mapcat ensure-coll %&) {:a 1} {:a [2]} {:a [3], :b 1}))
{:a (1 2 3), :b 1}
(let [[first-name last-name & aliases] (list "Rich" "Hickey" "The Clojurer" "Go Time" "Lambda Guru")] (str first-name last-name (clojure.string/join " aka " aliases)))
"RichHickeyThe Clojurer aka Go Time aka Lambda Guru"(let [f [{:a 1, :b 2, :c 9} {:a 3, :b 4, :c 10}]] (for [i f] [(:a i) (:b i)]))
([1 2] [3 4])
(let [f (fn [a b] (if (vector? a) (conj a b) (conj [a] b)))] (merge-with f {1 "a"} {1 "b"} {2 "c"}))
{1 ["a" "b"], 2 "c"}
(let [a (zipmap (range 100) (range 100)) b (assoc a :c 1) c (dissoc b :c)] [(= a c) (identical? a c)])
[true false]
(let [m1 {:foo 1, :bar 2, :baz 3} m2 {:foo 4, :bar 5}] (->> (keys m1) (map (juxt identity m2)) (into {})))
{:bar 5, :baz nil, :foo 4}
(let [songs (repeatedly 1000 #(rand-int 1e8)) fingerpseq (map #(rem % 10) songs) fingerpset (zipmap fingerpseq songs)] (vals fingerpset))
(45647550 31222587 44996181 60949294 28715666 3550113 84534502 65625199 15019555 97385328)
(let [vs [[1 2] [3 4] [5 6]] [before after] (split-at 1 vs)] (apply conj (vec before) [7 8] [9 10] after))
[[1 2] [7 8] [9 10] [3 4] [5 6]]
(#(let [[[a] [_ b] [_ _ c]] %] [a b c]) '((1 2 3) (4 1 6) (7 8 1)))
[1 1 1]
(let [i (atom 0)] {:filter (first (filter #(do (swap! i inc) (odd? %)) '(1 2 3 4 5))), :i i})
{:filter 1, :i #object [clojure.lang.Atom 0x80b54e {:status :ready, :val 1}]}
(let [x [{:id 1, :foo :bar} {:id 3, 1 "hello", 2 "hi"}]] (into {} (map #(vector (% :id) (dissoc % :id)) x)))
{1 {:foo :bar}, 3 {1 "hello", 2 "hi"}}
(defn cartprod [xss] (if (empty? xss) [[]] (let [things (cartprod (rest xss))] (mapcat (fn [x] (map #(cons x %) things)) (first xss)))))
#'user/cartprod
(let [in [{:hello 1} {:hello 3} {:hello 4}] ks (distinct (mapcat keys in)) init (zipmap ks (repeat []))] (apply merge-with conj init in))
{:hello [1 3 4]}
(let [a 1 b 2] [(+ a b) '(+ a b) [+ a b] '[+ a b]])
[3 (+ a b) [#object [clojure.core$_PLUS_ 0x35b52c2b "clojure.core$_PLUS_@35b52c2b"] 1 2] [+ a b]]
(let [id1 {:id 1} id2 {:id 2} id3 {:id 3}] (= (set '({:id 1} {:id 2})) (set (list id1 id2 id3))))
false(let [f (fn [& {:keys [a b c], :or {a 1, b 2, c 3}, :as params}] (println a b c))] (f))
nil"1 2 3\n"(apply str (let [a "eda?we ie oc hk otShswnntr"] (map #(get a (mod (* 15 (- % 4)) 26)) (range 26))))
"Since when does that work?"(let [r 255 g 255 b 255] (-> r (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left b 16))))
16777215(let [m [{:k1 [{:k2 42}]} {:k1 [{:k2 77}]}]] (map #(update % :k1 (fn [v] [{:k2 (* 2 (:k2 (first v)))}])) m))
({:k1 [{:k2 84}]} {:k1 [{:k2 154}]})
(let [nodes (range 6) decorators (-> (map (fn [a b] a) (repeat "|--") (rest nodes)) (concat ["`--"]))] (map str nodes decorators))
("0|--" "1|--" "2|--" "3|--" "4|--" "5`--")
(let [[[id1 ks1 :as foo] [id2 ks2] res extra] [[1 2] [3 4] 5 6]] [id1 ks1 foo id2 ks2 res extra])
[1 2 [1 2] 3 4 5 6]
(defn swap! [a f & args] (loop [] (let [old-val @a new-val (apply f old-val args)] (if (compare-and-set! a old-val new-val) new-val (recur)))))
#'user/swap!
(let [cpcount #(.codePointCount % 0 (.length %)) a "\ud80c" b "\udc00"] (map (juxt count cpcount) [a b (str a b)]))
([1 1] [1 1] [2 1])
(defn unzip-with [pred coll] (let [checked (map #([(pred %) %]) coll)] [(map second (filter first checked)) (map second (remove first checked))]))
#'user/unzip-with
(let [{{:keys [a b c], :as p} :p, :as r} {:p {:a 1, :b 2, :c 3}}] [a b c p r])
[1 2 3 {:a 1, :b 2, :c 3} {:p {:a 1, :b 2, :c 3}}]
(let [r 0 g 0 b 0] (-> r (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left b 16))))
0(let [r 1 g 90 b 180] (-> r (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left g 8)) (bit-or (bit-shift-left b 16))))
11819521(let [roll (fn [g p] (conj g p)) roll-list (fn [g l] (reduce roll (conj g l)))] (roll-list [] [1 2 3]))
[1 2 3]
(defn move-up [v i] (let [pos (.indexOf v i)] (if (pos? pos) (assoc v pos (v (dec pos)) (dec pos) i) v)))
#'user/move-up
(defn read-seq [reader & [recursive?]] (let [eof (Object.)] (take-while #(not (identical? eof %)) (repeatedly #(read reader false eof (boolean recursive?))))))
#'user/read-seq
(defn safe-comp [& fs] (fn [& args] (reduce #(let [r (apply %2 %1)] (if (nil? r) (reduced nil) r)) args fs)))
#'user/safe-comp
(let [foo [1 1 1]] (second (reduce (fn [[a x] b] [a (and x (= a b))]) [(first foo) true] (rest foo))))
true(let [midpoint (fn [idx coll] [(take idx coll) (nth coll idx) (drop (inc idx) coll)])] (midpoint 3 [1 2 3 4 5]))
[(1 2 3) 4 (5)]
(defn my-subs [st from end] (let [f (if (neg? from) (+ (count st) (mod from (count st))) from)] (subs st f end)))
#'user/my-subs
(let [printables (map char (range 33 127)) random-printables (repeatedly #(nth printables (long (rand (count printables)))))] [(take 10 random-printables) (take 10 random-printables)])
[(\C \Z \D \M \9 \+ \P \Z \m \%) (\C \Z \D \M \9 \+ \P \Z \m \%)]
(doseq [x '({:user "a", :reps {:a 1, :b 2}})] (let [{a :user, rs :reps} x] (for [to (keys rs)] (print to))))
nil(map #(let [per-pile (quot 9 3)] (+ 1 (* (mod (- per-pile 1 %) per-pile) per-pile) (quot % per-pile))) (range 9))
(7 4 1 8 5 2 9 6 3)

