(let [grid [[1 2 3] [4 5 6] [7 8 9]]] (for [x (range (count (first grid))) y (range (count grid))] (print (nth (nth grid y) x))))
(nil nil nil nil nil nil nil nil nil)
"147258369"(loop [n 10345678 result '()] (let [q (int (/ n 10)) r (rem n 10)] (if (= q 0) (cons r result) (recur q (cons r result)))))
(1 0 3 4 5 6 7 8)
(let [m {:a {:x 5, :y 6}, :b {:x 7, :y 8}}] (zipmap (keys m) (for [m2 (vals m)] (zipmap (keys m2) (map inc (vals m2))))))
{:a {:x 6, :y 7}, :b {:x 8, :y 9}}
(let [X {:a 565, :b 878, :c 565, :d 997} R #{565}] (reduce (fn [m [k v]] (if (R v) (dissoc m k) m)) X X))
{:b 878, :d 997}
((fn rle [s] (when (seq s) (let [run (take-while #(= (first s) %) s) c (count run)] (cons [c (first s)] (rle (drop c s)))))) "111111222222333333abccccd")
([6 \1] [6 \2] [6 \3] [1 \a] [1 \b] [4 \c] [1 \d])
(let [as [0 1 2 3] bs [9 8 7 6]] (doseq [[a b] (map vector as bs)] (println (format "a %d and b %d" a b))))
nil"a 0 and b 9\na 1 and b 8\na 2 and b 7\na 3 and b 6\n"(let [raw [{:a 0, :b 1} {:a 1, :c "OK"} {:b 2, :c "hello"}] base-keys (into #{} (mapcat keys raw))] (apply merge-with conj (zipmap base-keys (repeat [])) raw))
{:a [0 1], :b [1 2], :c ["OK" "hello"]}
(let [na Double/NaN nana (* 0.0 Double/NaN) nanana (Double. Double/NaN) watman (Double. (* 0.0 Double/NaN))] [(= na na) (= nana nana) (= nanana nanana) (= watman watman)])
[true true true true]
(defmacro cond->-> [x & pairs] (if-let [[c form & more] (seq pairs)] `(let [x# ~x] (cond->-> (if (-> x# ~c) (-> x# ~form) x#) ~@more)) x))
#'user/cond->->
(let [a (make-array Double/TYPE 5 5) b (into-array (map aclone a))] (aset b 2 0 -9.3) (println (aget b 2 0)) (println (aget a 2 0)))
nil"-9.3\n0.0\n"(let [a (atom []) res (partition-by #(do (swap! a conj %) (zero? (mod % 5))) (iterate inc 0)) forced (doall (take 3 res))] [forced @a])
[((0) (1 2 3 4) (5)) [0 1 1 2 3 4 5 5]]
(let [idx (take 10 (repeatedly #(rand-int 1000))) data (vec (repeat 1000 0)) updated (reduce #(assoc %1 %2 1) data idx)] (filter #{1} updated))
(1 1 1 1 1 1 1 1 1 1)
(into {} (map (juxt first #(let [lvals (rest %1)] (if (= (count lvals) 1) (first lvals) (vec lvals)))) [[:a 1 2 3] [:b 4 5 6] [:c 7]]))
{:a [1 2 3], :b [4 5 6], :c 7}
(let [tree ["one" "two" ["three" "four"]] reps {"one" 1, "three" 3} doit (fn doit [x] (if (coll? x) (map doit x) (get reps x x)))] (map doit tree))
(1 "two" (3 "four"))
(let [m {365 [{:a 1, :b 2} {:a 4, :b 6}], 366 [{:a 11, :b 22} {:a 44, :b 66}]}] (into {} (for [[id [mps] :as orig] m] orig)))
{365 [{:a 1, :b 2} {:a 4, :b 6}], 366 [{:a 11, :b 22} {:a 44, :b 66}]}
(for [x [0 1 10 100 1000 10000]] (let [m (transient (apply hash-map (range (* 2 x))))] (dotimes [i 20000] (assoc! m i (inc i))) (count (persistent! m))))
(8 20000 20000 20000 20000 20000)
(do (defn pad [coll n x] (let [cnt (- n (count coll))] (if (pos? cnt) (concat coll (take cnt (repeat x))) coll))) (pad [1 2 3] 5 nil))
(1 2 3 nil nil)
(let [l [1 2 3 3 2 1] p (partition 2 1 l)] (split-at (inc (count (first (split-with #(not (= (first %) (first (rest %)))) p)))) l))
[(1 2 3) (3 2 1)]
(let [str. (fn [& args] (str (apply str args) ".")) x 5 y 2] (str. "The average of " x " and " y " is " (/ (+ x y) 1)))
"The average of 5 and 2 is 7."(let [b {} a {:s b} items [a b] index-of (reduce (fn [m [x i]] (assoc m i x)) {} (map-indexed vector items))] [(index-of a) (index-of b)])
[0 1]
((fn [n] (let [f (fn [n f] (if (<= 2 n) 1 (+ (f (- n 1)) (f (- n 2))))) f (memoize f)] (f n f))) 6)
1(defn method-handle [method arg-count] (let [obj (with-meta (gensym) {:tag `Date}) sym (symbol method) args (repeatedly arg-count gensym)] (eval `(fn [~obj ~@args] (. ~obj ~sym ~@args)))))
#'user/method-handle
(let [l '([0 12] [1 23] [3 65]) v (vec (repeat (count l) 0)) f (fn [v [k v’]] (assoc v k v’))] (reduce f v l))
[12 23 0 65]
(let [foo (fn [a] (if (= 0 (get a 0)) a (recur (update-in a [0] dec))))] (foo [10 9 8 7 6 5 4 3 2 1 0]))
[0 9 8 7 6 5 4 3 2 1 0]
(let [app (fn [f & rest] (apply f rest)) fst (fn [f & rest] f)] (map app (cycle [fst -]) [10 20 30 40] [1 2 3 4]))
(10 18 30 36)
(defn replacefirst [pred f coll] (let [head (take-while (complement pred) coll) [item & tail] (drop-while (complement pred) coll)] (into (empty coll) (concat head (cons (f item) tail)))))
#'user/replacefirst
(reduce #(let [i (rand-int %2) j (dec %2)] (-> %1 (assoc i (%1 j)) (assoc j (%1 i)))) [1 2 3 4 5] (range 5 1 -1))
[1 5 4 3 2]
(defn reduce-if-not [pred f init s] (loop [a init s s] (if (empty? s) a (let [a (f a (first s))] (when-not (pred a) (recur a (next s)))))))
#'user/reduce-if-not
(let [m {"a" 100, "b" 200, "c" 50} total (apply + (vals m))] (first (shuffle (for [[k v] m itm (repeat (/ (* v 100) total) k)] itm))))
"a"(let [s (str '(1 2 3)) s1 (binding [*print-length* 1] (str '(1 2 3))) s2 (binding [*print-length* 2] (str '(1 2 3)))] [s s1 s2])
["(1 2 3)" "(1 2 3)" "(1 2 3)"]
(reduce-kv (fn [m k v] (let [[_ a b] (re-matches #"([a-z]*)([0-9]*)" k)] (assoc-in m [a b] v))) {} {"abc12" :a, "abc13" :b, "abd42" :c})
{"abc" {"12" :a, "13" :b}, "abd" {"42" :c}}
((fn [a b] (let [s->v (fn [vs] (vec (reverse (drop-while zero? (reverse (map #(Integer. %) (.split vs "[.]")))))))] (compare (s->v a) (s->v b)))) "0.1.3" "0.1.3.0")
0(let [a (into-array [1 2 3 4 5]) s (seq a)] (println (first s) ".") (aset a 0 42) (println (first s) ".") (println (aget a 0) "."))
nil"1 .\n42 .\n42 .\n"(let [a (atom 10) old-value (atom nil) instrumented-inc ((fn [f] (fn [x & args] (reset! old-value x) (apply f x args))) inc)] [(swap! a instrumented-inc) @old-value])
[11 10]
(let [keys [:a :b] m {:a [1 2 3], :b [3 4 5]}] (apply map (fn [& vals] (apply hash-map (interleave keys vals))) (map m keys)))
({:a 1, :b 3} {:a 2, :b 4} {:a 3, :b 5})
(let [cs [:b :w] var-names (map (comp symbol #(str "v" %)) (range)) binding-vector (interleave var-names (repeat (count cs) cs))] `(for [~@binding-vector] [~@(take (count cs) var-names)]))
(clojure.core/for [v0 [:b :w] v1 [:b :w]] [v0 v1])
(defn distinct-by' [f coll] (peek (reduce (fn [[found results] e] (let [index (f e)] (if (contains? found index) [found results] [(conj found index) (conj results e)]))) [#{} []] coll)))
#'user/distinct-by'
(let [star (take 5 (iterate #(str "**" %) "*")) space (take 5 (iterate #(apply str (next %)) " "))] (dorun (map (comp println str) space star)))
nil" *\n ***\n *****\n *******\n *********\n"(defn merge-sort [& seqs] (let [[[s0 & ss :as s] & seqs] (sort-by first (remove empty? seqs))] (lazy-seq (when (seq s) (cons s0 (apply merge-sort (cons ss seqs)))))))
#'user/merge-sort
(let [start {"name" "databases", "columns" ["name" "id"], "values" [["testdb" 1] ["mydb" 2]]}] (mapcat (fn [vcoll] (map (fn [a b] {(keyword a) b}) (start "columns") vcoll)) (start "values")))
({:name "testdb"} {:id 1} {:name "mydb"} {:id 2})
(defn map-keys [m ks f] (reduce (fn [m* k] (let [v (get m* k ::empty)] (if-not (= v ::empty) (assoc m* k (f (k m*))) m*))) m ks))
#'user/map-keys
(let [{{:keys [id], :as session} :session, {:strs [a b], :as params} :params} {:session {:id 0}, :params {"a" 1, "b" 2}}] [id a b session params])
[0 1 2 {:id 0} {"a" 1, "b" 2}]
(defmacro heck-> ([x] x) ([x form] `(-> ~x ~form)) ([x form1 & more] `(let [x1# ~x x2# (-> x1# ~form1)] (heck-> (if (nil? x2#) x1# x2#) ~@more))))
#'user/heck->
(defn small-enough? [limit input] (reduce (fn [total el] (let [result (+ total (count (pr-str el)))] (if (> result limit) (reduced false) result))) 0 (remove coll? (tree-seq coll? seq input))))
#'user/small-enough?
(let [f (fn [s] (->> (iterate #(.indexOf s "<C>" (inc %)) -1) (drop 1) (take-while (complement neg?))))] (f "la la la <C> la la <C> <C> foo <C>"))
(9 19 23 31)
(let [_ #(apply + 3 %&) <> (->> #(%) #(%) #(%) #(%) #(%) #(%) #(%) #(%))] ((((((((<> _) _) _) _) _) _) _) _))
3(let [replace (fn [m & x] (assert (every? (partial contains? m) (keep-indexed #(if (even? %1) %2) x))) (apply assoc (conj (seq x) m)))] (replace {:a 1} :a 2))
{:a 2}
(let [v [1 5 3 3 2 4 5 5 4 5 1 2 1 2 3 5]] (map #(vector (first %) (count %)) (vals (group-by identity v))))
([1 3] [5 5] [3 3] [2 3] [4 2])
(let [v [:a :b :c] n (count v)] (for [x (range n) y (range n) :when (> x y)] #{(get v x) (get v y)}))
(#{:a :b} #{:a :c} #{:b :c})
(take 20 ((fn f [l] (if (nil? l) nil (let [y (first l)] (lazy-seq (cons y (f (filter #(> (mod % y) 0) (next l)))))))) (iterate inc 2)))
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71)

