(let [lift-map (fn [f n col] (((apply comp (repeat n (partial partial map))) f) col))] (lift-map inc 3 [[[1 2] [3] [4 5 6]]]))
(((2 3) (4) (5 6 7)))
(let [a (into {} [[0 12] [1 23] [3 65]]) b (apply max (map first a))] (map #(get a % 0) (range (inc b))))
(12 23 0 65)
(defn ack [m n] (cond (zero? m) (inc n) (zero? n) (recur (dec m) 1) (pos? n) (recur (dec m) (ack m (dec n)))))
#'user/ack
(let [l '([0 12] [1 23] [3 65]) m (into {} l)] (reduce #(conj %1 (m %2)) [] (range (inc (first (last m))))))
[12 23 nil 65]
(defn tree-depth [branch? children root] (let [walk (fn walk [node] (if (branch? node) (inc (reduce max (map walk (children node)))) 0))] (walk root)))
#'user/tree-depth
((fn [s v] (some #(= s %) (map #(subvec v % (+ % (count s))) (range 0 (inc (- (count v) (count s))))))) [] [])
true
((fn [a b & args] (into {} (map (fn [[x y]] [(a x) (b y)]) (partition 2 args)))) identity inc :a 1 :b 2 :c 3)
{:a 2, :b 3, :c 4}
((fn [start end] (map (fn [item] (filter #(and (>= % start) (< % end)) [(dec item) item (inc item)])) (range start end))) 2 8)
((2 3) (2 3 4) (3 4 5) (4 5 6) (5 6 7) (6 7))
((fn a [m n] (cond (zero? m) (inc n) (zero? n) (recur (dec m) 1) :else (recur (dec m) (a m (dec n))))) 3 1)
13
((fn [start end] (map (fn [item] (filter #(and (>= % start) (< % end)) [(dec item) item (inc item)])) (range start end))) 5 15)
((5 6) (5 6 7) (6 7 8) (7 8 9) (8 9 10) (9 10 11) (10 11 12) (11 12 13) (12 13 14) (13 14))
(binding [*print-length* 100] (str (zipmap (reverse [:str :dex :con :int :wis :cha]) (repeatedly 6 (fn [] (apply + (rest (sort (repeatedly 4 #(inc (rand-int 6)))))))))))
"{:cha 17, :wis 10, :int 11, :con 16, :dex 12, :str 12}"
((fn take-seq [pred coll] (map second (filter (partial apply pred) (partition 2 1 coll)))) #(= %2 (inc %1)) [1 2 3 5 6 9])
(2 3 6)
(swap! (atom {:hi "there", :list [1 2 3 4]}) update-in [:list] ((fn [n] (fn [v] (vec (concat (take n v) (drop (inc n) v))))) 2))
{:hi "there", :list [1 2 4]}
(let [limit 6] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((5 4 3 2 1 0) (4 5 3 2 1 0) (4 3 5 2 1 0) (4 3 2 5 1 0) (4 3 2 1 5 0) (4 3 2 1 0 5) (5 3 4 2 1 0) (3 5 4 2 1 0) (3 4 5 2 1 0) (3 4 2 5 1 0) (3 4 2 1 5 0) (3 4 2 1 0 5) (5 3 2 4 1 0) (3 5 2 4 1 0) (3 2 5 4 1 0) (3 2 4 5 1 0) (3 2 4 1 5 0) (3 2 4 1 0 5) (5 3 2 1 4 0) (3 5 2 1 4 0) (3 2 5 1 4 0) (3 2 1 5 4 0) (3 2 1 4 5 0) (3 2 1 4 0 5) (5 3 2 1 0 4) (3 5 2 1 0 4) (3 2 5 1 0 4) (3 2 1 5 0 4) (3 2 1 ...
((fn foo [x y & args] (apply hash-map (map #(%1 %2) (cycle [x y]) args))) identity inc :a 1 :b 2 :c 3)
{:a 2, :b 3, :c 4}
(loop [m {} coll [2 3 3 2 4 2 5]] (if (empty? coll) m (recur (update-in m [(first coll)] (fnil inc 0)) (rest coll))))
{2 3, 3 2, 4 1, 5 1}
(letfn [(myrange [n] (take n (iterate (fn [x] (print x " ") (inc x)) 0))) (mytest [a b & others])] (apply mytest (myrange 10)))
nil
"0 1 2 "
(let [input {:a 0, :b 1, :c 2, :d 3}] (reduce-kv (fn [m k v] (if (even? v) (assoc m k (inc v)) m)) input input))
{:a 1, :b 1, :c 3, :d 3}
(loop [[k v & rest :as all] '(:a 1 :b 2 :c 3) result []] (if-not all result (recur rest (conj result [(inc v) k]))))
[[2 :a] [3 :b] [4 :c]]
(second (reduce (fn [[hash acc] el] (if (contains? hash el) [hash (inc acc)] [(conj hash el) acc])) [#{} 0] [:a :b :c :d :a :a :a :b]))
4
(loop [acc [] [e & r] [2 4 6 7 8 10]] (if (odd? e) (into (conj acc (inc e)) r) (recur (conj acc e) r)))
[2 4 6 8 8 10]
(let [coll '(1 3 2 5 4 7)] (last (keep-indexed (fn [i [a b]] (when (< a b) (inc i))) (partition 2 1 coll))))
5
(reduce (fn [acc-map item] (assoc acc-map item (inc (acc-map item 0)))) {} '(nil nil "a" "b" "b" "a" "b" "c" "c" "c" "c" "b" nil))
{nil 3, "a" 2, "b" 4, "c" 4}
(let [in [1 35 456 2005 3045 3987] by-count (group-by #(quot % 1000) in)] (map #(get by-count % []) (range (inc (apply max (keys by-count))))))
([1 35 456] [] [2005] [3045 3987])
(defn subvec? [s v] (some #(= s %) (map #(subvec v % (+ % (count s))) (range 0 (inc (- (count v) (count s)))))))
#'user/subvec?
(let [limit 5] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((4 3 2 1 0) (3 4 2 1 0) (3 2 4 1 0) (3 2 1 4 0) (3 2 1 0 4) (4 2 3 1 0) (2 4 3 1 0) (2 3 4 1 0) (2 3 1 4 0) (2 3 1 0 4) (4 2 1 3 0) (2 4 1 3 0) (2 1 4 3 0) (2 1 3 4 0) (2 1 3 0 4) (4 2 1 0 3) (2 4 1 0 3) (2 1 4 0 3) (2 1 0 4 3) (2 1 0 3 4) (4 3 1 2 0) (3 4 1 2 0) (3 1 4 2 0) (3 1 2 4 0) (3 1 2 0 4) (4 1 3 2 0) (1 4 3 2 0) (1 3 4 2 0) (1 3 2 4 0) (1 3 2 0 4) (4 1 2 3 0) (1 4 2 3 0) (1 2 4 3 0) (1 ...
((fn foomap [fn lst] (if (empty? lst) lst (let [temp (map fn lst)] (cons (first temp) (foomap fn (rest temp)))))) inc [1 1 1 1 1])
(2 3 4 5 6)
((fn [a b & args] (into (sorted-map) (map (fn [[x y]] [(a x) (b y)]) (partition 2 args)))) identity inc :a 1 :b 2 :c 3)
{:a 2, :b 3, :c 4}
(let [incw (fn [f] #(inc (f %))) firstw (fn [f] #(first (f %))) stack (-> identity firstw incw)] (stack [0 2 4 6]))
1
(let [incw (fn [f] #(f (inc %))) firstw (fn [f] #(f (first %))) stack (-> identity incw firstw)] (stack [0 2 4 6]))
1
(condp some [1 2 3 4] #{0 6 7} :>> inc #{4 5 9} :>> dec #{1 2 3} :>> #(+ % 3))
3
(second (reduce (fn [[hash acc] el] (if (contains? hash el) [hash acc] [(conj hash el) (inc acc)])) [#{} 0] [:a :b :c :d :a :a :a :b]))
4
(defn poss [s] (for [i (range (count s)) :let [n (nth s i)] e (range n)] (concat (take i s) [e] (drop (inc i) s))))
#'user/poss
(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}}
(reduce (fn [acc-map item] (assoc acc-map item (inc (get acc-map item 0)))) {} '(nil nil "a" "b" "b" "a" "b" "c" "c" "c" "c" "b" nil))
{nil 3, "a" 2, "b" 4, "c" 4}
(defn fib [n] (get (reduce (fn [m n] (assoc m n (+ (m (dec n)) (m (- n 2))))) [1N 1N] (range 2 (inc n))) n))
#'user/fib
((fn [s v] (some #(= s %) (map #(subvec v % (+ % (count s))) (range 0 (inc (- (count v) (count s))))))) [1] [])
nil
(let [limit 7] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((6 5 4 3 2 1 0) (5 6 4 3 2 1 0) (5 4 6 3 2 1 0) (5 4 3 6 2 1 0) (5 4 3 2 6 1 0) (5 4 3 2 1 6 0) (5 4 3 2 1 0 6) (6 4 5 3 2 1 0) (4 6 5 3 2 1 0) (4 5 6 3 2 1 0) (4 5 3 6 2 1 0) (4 5 3 2 6 1 0) (4 5 3 2 1 6 0) (4 5 3 2 1 0 6) (6 4 3 5 2 1 0) (4 6 3 5 2 1 0) (4 3 6 5 2 1 0) (4 3 5 6 2 1 0) (4 3 5 2 6 1 0) (4 3 5 2 1 6 0) (4 3 5 2 1 0 6) (6 4 3 2 5 1 0) (4 6 3 2 5 1 0) (4 3 6 2 5 1 0) (4 3 2 6 5 1 0)...
(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]]
((fn [s v] (some #(= s %) (map #(subvec v % (+ % (count s))) (range 0 (inc (- (count v) (count s))))))) [1] [1])
true
(defn fib [n] (get (reduce (fn [memo n] (assoc memo n (+ (memo (dec n)) (memo (- n 2))))) {0 1N, 1 1N} (range 2 (inc n))) n))
#'user/fib
(transduce (map :age) (completing #(update % %2 (fnil inc -1)) (comp (partial apply +) vals)) {} [{:age 1} {:age 1} {:age 1} {:age 2} {:age 2} {:age 3}])
3
(defn fib [n] (get (reduce (fn [m n] (assoc m n (+ (m (dec n)) (m (- n 2))))) {0 1N, 1 1N} (range 2 (inc n))) n))
#'user/fib
((fn [s v] (some #(= s %) (map #(subvec v % (+ % (count s))) (range 0 (inc (- (count v) (count s))))))) [2 3 4] [])
nil
(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]
(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)
(reductions (fn [t [i c]] (+ (* t (/ (dec c) c)) (* i (/ 1.0 c)))) 0 (map list [10 9 8 7 6 5] (map inc (range))))
(0 10.0 9.5 9.0 8.5 8.0 7.499999999999999)
(defn fib [n] (get (reduce (fn [m n] (assoc m n (+ (m (dec n)) (m (- n 2))))) {0 1, 1 1} (range 2 (inc n))) n))
#'user/fib
(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")
(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)