(let [ms [{:text "asdad", :value "123"} {:text "asdad", :value "123"} {:text "asdad", :value "123"}] ks (distinct (mapcat keys ms))] (for [k ks] (cons (name k) (map k ms))))
(("text" "asdad" "asdad" "asdad") ("value" "123" "123" "123"))
(defmacro heck-> ([x] x) ([x form1] `(let [x1# ~x x2# (-> x1# ~form1)] (if (nil? x2#) x1# x2#))) ([x form1 & more] `(heck-> (heck-> ~x ~form1) ~@more)))
#'user/heck->
(let [l [1 1 1 2 3 3 1 1 4 4 4 4]] (reduce #(if (= (last %1) %2) %1 (conj %1 %2)) [(first l)] (rest l)))
[1 2 3 1 4]
(let [m {:a 3, :b 1, :c 5, :d 7, :e 8, :f 9, :g 10, :h 11, :i 12}] (into (sorted-map-by #(compare (m %) (m %2))) m))
{:a 3, :b 1, :c 5, :d 7, :e 8, :f 9, :g 10, :h 11, :i 12}
(let [a (into-array [1 2 3 4 5]) s (doall (seq a))] (println (first s) ".") (aset a 0 42) (println (first s) ".") (println (aget a 0) "."))
nil"1 .\n42 .\n42 .\n"(let [start {"name" "databases", "columns" ["name" "id"], "values" [["testdb" 1] ["mydb" 2]]}] (map (fn [vcoll] (into {} (mapcat (fn [a b] {(keyword a) b}) (start "columns") vcoll))) (start "values")))
({:id 1, :name "testdb"} {:id 2, :name "mydb"})
(let [[xs ys zs] (vals (into (sorted-map) {\a [1 2 3], \d [4 5 6], \c [7 8 9]}))] (for [x xs y ys z zs] [x y z]))
([1 7 4] [1 7 5] [1 7 6] [1 8 4] [1 8 5] [1 8 6] [1 9 4] [1 9 5] [1 9 6] [2 7 4] [2 7 5] [2 7 6] [2 8 4] [2 8 5] [2 8 6] [2 9 4] [2 9 5] [2 9 6] [3 7 4] [3 7 5] [3 7 6] [3 8 4] [3 8 5] [3 8 6] [3 9 4] [3 9 5] [3 9 6])
(let [sums (reductions + [1 2 3 4 5 6 7 8 9 4]) total (last sums)] (->> (partition 2 1 sums) (map (fn [[a b]] [a b total]))))
([1 3 49] [3 6 49] [6 10 49] [10 15 49] [15 21 49] [21 28 49] [28 36 49] [36 45 49] [45 49 49])
((fn [& ms] (let [base (zipmap (mapcat keys ms) (repeat []))] (apply merge-with conj base ms))) {:a 1, :b 2, :c 3} {:a 3, :b 4, :d 4} {:a 5})
{:a [1 3 5], :b [2 4], :c [3], :d [4]}
(let [a [[1 23] [3 65]] b (map #(- (first %1) (first %2) 1) a (cons [0] a))] (mapcat #(concat (repeat %1 0) [(second %2)]) b a))
(23 0 65)
((fn qsort [[x & xs]] (if x (let [smaller? (partial >= x)] (concat (->> xs (filter smaller?) qsort) [x] (->> xs (remove smaller?) qsort))))) (repeatedly 20 (partial rand-int 256)))
(0 3 36 38 59 60 63 63 83 85 91 92 185 192 215 225 227 244 252 253)
(defmacro delay* [& body] `(let [value# (atom nil)] (reify clojure.lang.IDeref (deref [this#] (:value (swap! value# (fn [{done# :done, :as curr#}] (if done# curr# {:done true, :value (do ~@body)}))))))))
#'user/delay*
(loop [primes [] [f & r] ((comp rest rest) (range))] (let [sieved (remove #(zero? (mod % f)) r)] (if (= (count primes) 10) primes (recur (conj primes f) sieved))))
[2 3 5 7 11 13 17 19 23 29]
(defn argmin [[arg & args] f] (first (reduce (fn [[arg min] arg] (let [v (apply f arg)] (if (< v min) [arg v] [arg min]))) [arg (f arg)] f)))
#'user/argmin
(defn fill-rect ([g this] (let [foo (.getBounds this)] (fill-rect g (.getX foo) (.getY foo) (.getWidth foo) (.getHeight foo)))) ([g x y w h] (.fillRect g x y w h)))
#'user/fill-rect
(defmacro record "Dynamic factory for defrecords." ([name] `(record ~name {})) ([name vals-map] `(let [con# (first (.getDeclaredConstructors ~name)) num# (alength (.getParameterTypes con#))] (merge (.newInstance con# (make-array Object num#)) ~vals-map))))
#'user/record
(let [na Double/NaN nana (* 0.0 Double/NaN) nanana (Double. Double/NaN) watman (Double. (* 0.0 Double/NaN))] (for [ogod [na nana nanana watman] wat [na nana nanana watman]] (= ogod wat)))
(true false false false false true false false false false true false false false false true)
(let [{:keys [a b c], {:keys [e f g]} :d} {:a 0, :b 1, :c 2, :d {:e 3, :f 4, :g 5}}] [a b c e f g])
[0 1 2 3 4 5]
(reduce (fn [x y] (let [a (get x 1) b (get y 1)] (if (neg? (compare a b)) x y))) [["Coke" "16" ""] ["Coke" "35" "3"] ["Coke" "2" "3"]])
["Coke" "16" ""]
(let [ps (partition-by :id [{:id 1, :t "a"} {:t "b"} {:t "c"} {:id 2, :t "d"} {:id 3, :t "e"}])] (map (fn [parent children] [parent children]) ps (rest ps)))
([({:id 1, :t "a"}) ({:t "b"} {:t "c"})] [({:t "b"} {:t "c"}) ({:id 2, :t "d"})] [({:id 2, :t "d"}) ({:id 3, :t "e"})])
(let [m {:a 3, :b 1, :c 5, :d 7, :e 8, :f 9, :g 10, :h 11, :i 12}] (into (sorted-map-by #(compare (m %2) (m %))) m))
{:a 3, :b 1, :c 5, :d 7, :e 8, :f 9, :g 10, :h 11, :i 12}
(let [a [[1 23] [3 65]] b (map #(- (first %1) (first %2) 1) a (cons [-1] a))] (mapcat #(concat (repeat %1 0) [(second %2)]) b a))
(0 23 0 65)
(let [m {:a 0, :b 1, :c 2}] (reduce (fn [m' [k :as e]] (if (= (find m k) e) m' (conj m' e))) {} {:a 0, :b 1, :c 3}))
{:c 3}
(let [maps '({:N 1, :string "stuff", :more "stuff"} {:N 2, :string "hello", :more "world"}) test {:string "stuff", :more "stuff"}] (first (filter #(= test (dissoc % :N)) maps)))
{:N 1, :more "stuff", :string "stuff"}
((fn [& ms] (let [base (zipmap (set (mapcat keys ms)) (repeat []))] (apply merge-with conj base ms))) {:a 1, :b 2, :c 3} {:a 3, :b 4, :d 4} {:a 5})
{:a [1 3 5], :b [2 4], :c [3], :d [4]}
(defn inc-double [x] (loop [x' (* 2 x)] (let [half-delta (/ (- x' x) 2) x'' (+ x half-delta)] (if (or (= x x'') (= x' x'')) x' (recur x'')))))
#'user/inc-double
(sort (comparator (fn [a b] (if (> (count a) (count b)) nil #(let [x (sort (list % %2))] (if (.equals (first x) %) 1 -1))))) ["a" "booc" "aood" "b"])
("b" "a" "aood" "booc")
(filter #(re-matches #"[sb]a[gd]" %) (let [abc (map char (range (int \a) (inc (int \z))))] (for [x abc y abc z abc] (str x y z))))
("bad" "bag" "sad" "sag")
(letfn [(rl [xs] (lazy-seq (when (seq xs) (let [[h t] (split-with (partial = (first xs)) (rest xs))] (cons h (rl t))))))] (rl [1 1 1 2 2 3 3 3]))
((1 1) (2) (3 3))
(let [sqrt2 #(loop [x %2 i 0] (if (> i %3) x (recur (/ (+ x (/ %1 x)) 2) (inc i))))] (float (sqrt2 (/ 41 20) 2 8)))
1.4317821(let [log-level [:debug :info :warning] inverse-log-level (apply hash-map (interleave log-level (range))) access-log (fn ([log-key] (get inverse-log-level log-key :not-found)) ([log-key modifier] (get log-level (+ (inverse-log-level log-key) modifier) :not-found)))] (access-log :debug 1))
:info((fn [n] (let [f (fn [n f] (if (< n 2) 1 (+ (f (- n 1) f) (f (- n 2) f)))) f (memoize f)] (f n f))) 6)
13(let [sqrt2 #(loop [x %2 i 0] (if (> i %3) x (recur (/ (+ x (/ %1 x)) 2) (inc i))))] (float (sqrt2 (/ 41 20) 2 7)))
1.4317821((fn [n] (let [f (fn [n f] (if (<= n 2) 1 (+ (f (- n 1) f) (f (- n 2) f)))) f (memoize f)] (f n f))) 6)
8(let [_cons (fn [a b] (fn [c] (c a b))) car (fn [c] (c (fn [a b] a))) cdr (fn [c] (c (fn [a b] b)))] (cdr (_cons 1 nil)))
nil(let [_cons (fn [a b] (fn [c] (c a b))) car (fn [c] (c (fn [a b] a))) cdr (fn [c] (c (fn [a b] b)))] (car (_cons 1 nil)))
1(let [log-level [:debug :info :warning] inverse-log-level (apply hash-map (interleave log-level (range))) access-log (fn ([log-key] (get inverse-log-level log-key :not-found)) ([log-key modifier] (get log-level (+ (inverse-log-level log-key) modifier) :not-found)))] (access-log :debug 99))
:not-found(let [seconds 72010 hours (mod seconds 3600) minutes (mod (- seconds (* hours 3600)) 60)] {:hours hours, :minutes minutes, :seconds (- seconds (+ (* hours 3600) (* minutes 60)))})
{:hours 10, :minutes 10, :seconds 35410}
(let [a (long-array [0 1 2 3 4])] (= (seq (amap a i _ (inc (aget a i)))) (seq (amap a i r (aset r i (inc (aget a i)))))))
true(letfn [(parse-fraction [xs] (let [[m n] (reduce (fn [[a b] x] [(+ x (* a 10)) (* b 10)]) [0 1.0] xs)] (/ m n)))] (parse-fraction [1 2 3 4]))
0.1234(defn slice [coll start & end] (let [len (count coll) start (max start 0) end (if (nil? end) len (min end len))] (take (- end start) (drop start coll))))
#'user/slice
(let [s "Leonidas " f1 (fn [s res] (some #(re-find % s) res)) f2 (fn [[key res]] (when (f1 s res) key))] (some f2 {"found leonidas" #{#"Leonidas\S*"}}))
"found leonidas"(let [has? (fn [haystack needle] (some #{needle} haystack))] [(has? [1 nil 3 5 :a] :b) (has? [1 nil 3 5 :a] :a) (has? [1 nil 3 5 :a] 3)])
[nil :a 3]
((fn [& [a b c :as args]] (let [defaults [1 2 3] bindings (map #(or % %2) (concat args (repeat nil)) defaults) [a b c] bindings] bindings)) 42 43)
(42 43 3)
(let [l [1 2 3 4 4 3 2 1] p (partition 2 1 l)] (split-at (inc (count (first (split-with #(not (= (first %) (first (rest %)))) p)))) l))
[(1 2 3 4) (4 3 2 1)]
(reduce (fn [ranges [start end :as el]] (let [[s e] (peek ranges)] (if (> start e) (conj ranges el) (conj (pop ranges) [s end])))) [[3 3]] [[1 2] [4 5]])
[[3 2] [4 5]]
(let [a [[0 12] [1 23] [3 65]] b (map #(- (first %1) (first %2) 1) a (cons [0] a))] (mapcat #(concat (repeat %1 0) [(second %2)]) b a))
(12 23 0 65)
(let [v [{:name "Chris", :age 10, :id 0} {:name "Chris", :age 28, :id 1} {:name "Adam", :age 25, :id 2}] m (zipmap (map :id v) v)] (update-in m [0 :age] inc))
{0 {:age 11, :id 0, :name "Chris"}, 1 {:age 28, :id 1, :name "Chris"}, 2 {:age 25, :id 2, :name "Adam"}}
(loop [x 0 y 0 iterations 0] (let [new-x (+ x (dec (rand-int 3))) new-y (+ y (dec (rand-int 3)))] (if (= new-x new-y 5) iterations (recur new-x new-y (inc iterations)))))
2188(let [s (str '(1 2 3)) s1 (binding [*print-length* 1] (str '(1 2 3))) s2 (binding [*print-length* 2] (str '(1 2 3)))] [(count s) (count s1) (count s2)])
[7 7 7]

