(let [vals->map (fn [f vals] (reduce (fn [m v] (assoc m v (f v))) {} vals))] (vals->map inc [1 2 3]))
{1 2, 2 3, 3 4}
((fn average [coll] (apply / (reduce (fn [[sum count] item] [(+ sum item) (inc count)]) [0 0] coll))) (range 10))
9/2
(into {} (map #(vector %1 [%2 %3]) (iterate inc 1) (concat (range -4 5) (range 4 -5 -1)) (cycle [-1 1])))
{1 [-4 -1], 2 [-3 1], 3 [-2 -1], 4 [-1 1], 5 [0 -1], 6 [1 1], 7 [2 -1], 8 [3 1], 9 [4 -1], 10 [4 1], 11 [3 -1], 12 [2 1], 13 [1 -1], 14 [0 1], 15 [-1 -1], 16 [-2 1], 17 [-3 -1], 18 [-4 1]}
(let [m (transient (hash-map))] (dotimes [n 10000] (assoc! m n (inc n))) (let [m' (persistent! m)] [(count m') (get m' 4567)]))
[8 nil]
(defn get-name [] (->> 'clojure.core ns-publics keys (mapcat (fn [s] (clojure.string/split (name s) #"-"))) shuffle (take (inc (rand-int 3))) (clojure.string/join \-) symbol))
#'user/get-name
(do (defmacro map-> [coll & clauses] (let [g (gensym)] `(for [~g ~coll] (-> ~g ~@clauses)))) (map-> [1 2 3] inc even?))
(true false true)
(letfn [(sieve [[p & nums]] (lazy-seq (cons p (sieve (remove #(zero? (mod % p)) nums)))))] (take 10 (sieve (iterate inc 2))))
(2 3 5 7 11 13 17 19 23 29)
(apply print (interleave [:str :dex :con :int :wis :cha] (repeatedly 6 (fn [] (apply + (rest (sort (repeatedly 4 #(inc (rand-int 6))))))))))
nil
":str 15 :dex 14 :con 11 :int 10 :wis 10 :cha 13"
(map first (filter (fn [[x y]] (= (inc x) y)) (partition 2 1 [1 2 5 6 3 3 4 7 8])))
(1 5 3 7)
(some (fn [[e pos]] (when (= e :progress) pos)) (map (fn [x y] [x y]) '(:a :b :progress) (iterate inc 0)))
2
(map first (filter (fn [[x y]] (= (inc x) y)) (partition 2 1 [1 2 3 4 5 6 7 8])))
(1 2 3 4 5 6 7)
(defn avg ([s] (/ (reduce + s) (count s))) ([x & args] (/ (+ x (reduce + args)) (inc (count args)))))
#'user/avg
(str (zipmap (reverse [:str :dex :con :int :wis :cha]) (repeatedly 6 (fn [] (apply + (rest (sort (repeatedly 4 #(inc (rand-int 6))))))))))
"{:cha 13, :wis 10, :int 13, :con 13, :dex 8, :str 11}"
(let [a (set (range 0 1e5)) b (set (range 0 1e5 3))] (reduce #(if (a %2) (inc %) %) 0 b))
33334
(map second (filter (fn [[x y]] (= (inc x) y)) (partition 2 1 [1 2 5 6 3 3 4 7 8])))
(2 6 4 8)
(let [v [1 2 3 4]] (dotimes [i 3] (let [i (inc i)] (println (assoc v 0 (v i) i (v 0))))))
nil
"[2 1 3 4]\n[3 2 1 4]\n[4 2 3 1]\n"
(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}]}
(apply str (mapcat #(map char %) (concat [(map inc [70 110 110 99 109 104]) (reverse '(33 116 104 103))])))
"Goodnight!"
(defn decfirst ([v] (decfirst v 0)) ([v i] (if (pos? (v i)) (assoc v i (dec (v i))) (recur v (inc i)))))
#'user/decfirst
(#(vals (into (into (sorted-map) (map vector (range (inc (reduce max (keys %)))) (repeat nil))) %)) {0 0, 1 1, 3 3})
(0 1 nil 3)
(into {} (map (fn [[k v]] [k (if (even? v) (inc v) v)]) {:a 0, :b 1, :c 2, :d 3, :e 4}))
{:a 1, :b 1, :c 3, :d 3, :e 5}
((fn [i x coll] (concat (take i coll) x (drop (inc i) coll))) 2 [:a :b] [1 2 3 4 5])
(1 2 :a :b 4 5)
(reduce (fn [moves word] (conj moves (({"next" inc, "prev" dec} word) (peek moves)))) [0] (clojure.string/split "next prev next next" #" "))
[0 1 0 1 2]
(letfn [(f [& args] (map-indexed (fn [i t] (with-meta t {:index i})) args)) (g [x] (inc x))] (f g g g))
(#object [clojure.lang.AFunction$1 0x5570e554 "clojure.lang.AFunction$1@5570e554"] #object [clojure.lang.AFunction$1 0x5bb7018a "clojure.lang.AFunction$1@5bb7018a"] #object [clojure.lang.AFunction$1 0x5f888e24 "clojure.lang.AFunction$1@5f888e24"])
(let [h {:a 1, :b 2} f (fn [f v] (reduce #(conj %1 [(key %2) (f (val %2))]) {} v))] (f inc h))
{:a 2, :b 3}
(defn ackermann [m n] (cond (zero? m) (inc n) (zero? n) (ackermann (dec m) 1) :else (ackermann (dec m) (ackermann m (dec n)))))
#'user/ackermann
(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 looper [x y] (loop [x1 x y1 y] (if (< x1 5) (let [x1 x1] (println x1) (recur (inc x1) y1)))))
#'user/looper
(reduce #(assoc %1 %2 (inc (%1 %2 0))) {} '(nil nil "a" "b" "b" "a" "b" "c" "c" "c" "c" "b" nil))
{nil 3, "a" 2, "b" 4, "c" 4}
(map #(inc (read-string (apply str (drop-last (format "3r%03d" (Integer. (Integer/toString (dec %) 3))))))) [1 2 3 4 5 6 7 8 9])
(1 1 1 2 2 2 3 3 3)
((fn step [[x & xs] state] (lazy-seq (when x (cons (+ x state) (step xs (inc state)))))) [1 2 3 4 5] 0)
(1 3 5 7 9)
(update {:appenders {:appender-id {:fn inc, :arg-map 41}}} :appenders #(reduce-kv (fn [m k {:keys [fn arg-map]}] (assoc m k (fn arg-map))) % %))
{:appenders {:appender-id 42}}
(let [db {:next-id 1, :rows {}} id (:next-id db) new-row "fred"] (-> db (assoc :next-id (inc id)) (update-in [:rows] assoc id new-row)))
{:next-id 2, :rows {1 "fred"}}
(apply str (map (zipmap (map char (range (int \a) (inc (int \z)))) (map char (range (int \z) (dec (int \a)) -1))) "wizard"))
"draziw"
(reduce-kv (fn [m k v] (assoc m k (if (even? v) (inc v) v))) {} {:a 0, :b 1, :c 2, :d 3})
{:a 1, :b 1, :c 3, :d 3}
(let [a (set (range 0 6)) b (set (range 0 10 2))] (reduce #(if (a %2) (inc %) %) 0 b))
3
(let [counter (atom 0)] (nth (map (fn [i] (map #(swap! counter + %) (take 3 (repeat 1)))) (iterate inc 0)) 1000))
(1 2 3)
(let [kv-map (fn [f coll] (into {} (mapv (fn [[& kv]] (mapv f kv)) coll)))] (kv-map inc {1 10, 2 20, 3 31}))
{2 11, 3 21, 4 32}
(for [i [1 2 3 4 5 6 7] :let [j (inc i)] :when (odd? j) :while (< j 5)] [i j])
([2 3])
(def fib (lazy-seq (for [x (iterate inc 0)] (if (< x 2) x (+ (nth fib (- x 2)) (nth fib (- x 1)))))))
#'user/fib
((fn [e s] (let [r (take-while #(not= e %) s)] (concat r (drop (inc (count r)) s)))) 1 [2 1 1 2 3])
(2 1 2 3)
(let [midpoint (fn [idx coll] [(vec (take idx coll)) (nth coll idx) (vec (drop (inc idx) coll))])] (midpoint 3 [1 2 3 4 5]))
[[1 2 3] 4 [5]]
(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 1) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(letfn [(crack [c] (cons c (map (fn [n] (map #(str % n) c)) (iterate inc 1))))] (take 3 (crack '("user" "pass"))))
(("user" "pass") ("user1" "pass1") ("user2" "pass2"))
(loop [x {:foo {:bar []}} y 0] (let [y (inc y)] (if (< y 10) (recur (update-in x [:foo :bar] conj y) y) x)))
{:foo {:bar [1 2 3 4 5 6 7 8 9]}}
(let [a [[1 12] [2 23] [5 65]] m (into (sorted-map) a)] (map m (-> a last first inc range) (repeat 0)))
(0 12 23 0 0 65)
(clojure.string/join " " (take 40 (reductions #(if (= %2 42) (reduced %1) (inc %1)) (iterate #(bit-xor % (bit-shift-left 1 (rand-int 8))) 12))))
"12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 46"
(let [most #(if (pos? (compare % %2)) % %2)] (reduce most (map vector (map #(* 2 %) (range 100)) (iterate inc 0))))
[198 99]
(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 0) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(let [m (transient (apply hash-map (range 20)))] (dotimes [n 10000] (assoc! m n (inc n))) (let [m' (persistent! m)] [(count m') (get m' 4567)]))
[10000 4568]