(let [g [1 5 3 3 2 4 5 5 4 5 1 2 1 2 3 5] [v x c] (reduce (fn [[v x c] y] (if (= x y) [v x (inc c)] [(conj v [x c]) y 1])) [[] (first g) 1] (rest g))] (conj v [x c]))
[[1 1] [5 1] [3 2] [2 1] [4 1] [5 2] [4 1] [5 1] [1 1] [2 1] [1 1] [2 1] [3 1] [5 1]]
(let [stack [1 2] pop-peek (juxt pop peek) [stack arg1] (pop-peek stack) [stack arg2] (pop-peek stack)] (conj stack (+ arg1 arg2)))
[3]
((fn mine ([] (mine 0)) ([a] (mine a 0)) ([a b] (mine a b 0)) ([a b c & args] (conj args c b a))))
(0 0 0)
(defn nest-ranges [xs] (if (empty? xs) '(()) (let [sub (nest-ranges (rest xs))] (mapcat (fn [n] (map #(conj % n) sub)) (range (first xs))))))
#'user/nest-ranges
(reduce (fn [m x] (assoc m (first x) (conj ((first x) m) (second x)))) {} [[:a 1] [:b 2] [:a 3] [:a 4] [:b 5]])
{:a (4 3 1), :b (5 2)}
((fn perm [r p] (if (seq r) (mapcat #(perm (disj r %) (conj p %)) r) [p])) '#{a b c d e} [])
([a e c b d] [a e c d b] [a e b c d] [a e b d c] [a e d c b] [a e d b c] [a c e b d] [a c e d b] [a c b e d] [a c b d e] [a c d e b] [a c d b e] [a b e c d] [a b e d c] [a b c e d] [a b c d e] [a b d e c] [a b d c e] [a d e c b] [a d e b c] [a d c e b] [a d c b e] [a d b e c] [a d b c e] [e a c b d] [e a c d b] [e a b c d] [e a b d c] [e a d c b] [e a d b c] [e c a b d] [e c a d b] [e c b a d] [e ...
(reduce (fn [m {:keys [id f]}] (assoc m id (conj (get m id []) f))) {} [{:id 1, :f "a"} {:id 1, :f "b"} {:id 2, :f "c"}])
{1 ["a" "b"], 2 ["c"]}
(reduce (fn [m [k v]] (update-in m [k] #(if (nil? %) [v] (conj % v)))) {} [[:a 50] [:a 10] [:a 30] [:b 20]])
{:a [50 10 30], :b [20]}
(clojure.string/join "" (interpose ", " (reduce-kv #(conj %1 (str (name %2) " = " %3)) [] {:id "SERIAL", :is_exception "boolean", :is_obsolete "boolean", :is_disabled "boolean"})))
"id = SERIAL, is_exception = boolean, is_obsolete = boolean, is_disabled = boolean"
(map (partial apply str) ((fn perms [num-digits] (if (zero? num-digits) '([]) (for [perm (perms (dec num-digits)) n (range 10)] (conj perm n)))) 2))
("00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "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" "47" "48" "49" "50" "51" "52" "53" "54" "55" "56" "57" "58" "59" "60" "61" "62" "63" "64" "65" "66" "67" "68" "69" "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"...
(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]
(reduce (fn [seen n] (if (= (last seen) n) (reduced seen) (conj seen n))) [] '(1 2 3 4 5 5 6 7))
[1 2 3 4 5]
((fn [coll] (boolean (reduce (fn [s e] (if (contains? s e) (reduced false) (conj s e))) #{} coll))) [1 2 3 4 4])
false
(let [s (range)] (loop [i 10 s s rv []] (if (> i 0) (recur (dec i) (rest s) (conj rv (first s))) rv)))
[0 1 2 3 4 5 6 7 8 9]
(reduce (fn [mp line] (reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) mp (partition 2 (clojure.string/split line #":")))) {} ["a:0:b:1:c:hello" "c:2:d:3"])
{"a" ["0"], "b" ["1"], "c" ["hello" "2"], "d" ["3"]}
(loop [list [10 20 30 40 50] result []] (if (< (count list) 2) result (recur (rest list) (conj result (take 2 list)))))
[(10 20) (20 30) (30 40) (40 50)]
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} (concat {:a 1, :b 2} {:a 3, :b 4, :c 5}))
{:a [1 3], :b [2 4], :c [5]}
((fn pset [c] (when-let [[f & r] c] (concat (map #(conj % f) (pset r)) (pset r) [#{f}]))) [1 2 3 4])
(#{1 2 3 4} #{1 2 4} #{1 2 3} #{1 3 4} #{1 4} #{1 3} #{1 2} #{2 3 4} #{2 4} #{2 3} #{3 4} #{4} #{3} #{2} #{1})
(let [g [1 5 3 3 2 4 5 5 4 5 1 2 1 2 3 5] gs (sort g) [v x c] (reduce (fn [[v x c] y] (if (= x y) [v x (inc c)] [(conj v [x c]) y 1])) [[] (first gs) 1] (rest gs))] (conj v [x c]))
[[1 3] [2 3] [3 3] [4 2] [5 5]]
(transduce (map :age) (fn ([s] (count s)) ([s i] (conj s i))) #{} [{:age 1} {:age 1} {:age 1} {:age 2} {:age 2} {:age 3}])
3
(reduce (fn [m [k v]] (update-in m [k] (fnil conj #{}) v)) {15 #{5 10}, 25 #{5}} {1 3, 5 7, 25 42})
{1 #{3}, 5 #{7}, 15 #{5 10}, 25 #{5 42}}
(let [roll (fn [g p] (conj g p)) roll-many (fn [g n p] (nth (iterate #(roll % p) g) n))] (roll-many [] 20 0))
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
(for [f (list first second)] (reduce #(assoc %1 (f %2) (conj (%1 (f %2) []) %2)) {} [[30 9 2010] [30 10 2010] [31 10 2010]]))
({30 [[30 9 2010] [30 10 2010]], 31 [[31 10 2010]]} {9 [[30 9 2010]], 10 [[30 10 2010] [31 10 2010]]})
(defn partitioner [x coll] (loop [remainder coll result []] (cond (empty? remainder) result (>= (count remainder) x) (recur (drop x remainder) (conj result (take x remainder))))))
#'user/partitioner
(reduce (fn [m [k v]] (update-in m [k] #(if (vector? %) (conj % v) [v]))) {} [[:a 50] [:a 10] [:a 30] [:b 20]])
{:a [50 10 30], :b [20]}
(reduce #(let [[d m y] %2] (assoc %1 m (conj (%1 m []) [d m y]))) {} [[30 9 2010] [30 10 2010] [31 10 2010]])
{9 [[30 9 2010]], 10 [[30 10 2010] [31 10 2010]]}
(let [a {1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7, 9 9}] [(class (conj a [8 8])) (class a)])
[clojure.lang.PersistentHashMap clojure.lang.PersistentArrayMap]
(reduce (fn [m p] (let [[k v] (first p)] (assoc m k (conj (m k []) v)))) {} [{:A 1} {:A 2} {:A 3} {:C 1} {:C 2}])
{:A [1 2 3], :C [1 2]}
(filter (complement nil?) (map #(if (= %1 1) %2) (cycle (conj (into [] (repeat (dec 3) 1)) 0)) [1 2 3 4 5 6 7 8]))
(1 2 4 5 7 8)
(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]]
(reduce (fn [x y] (merge-with (fn [a b] (if (vector? a) (conj a b) (vector a b))) x y)) {} [{:one "2332"} {:one "4334"} {:one "3443"}])
{:one ["2332" "4334" "3443"]}
(let [fs {:foo +, :bar -, :zonk reduce} params {:foo [1 2], :bar [10 5], :zonk [conj #{} [1 2 3]]}] (merge-with apply fs params))
{:bar 5, :foo 3, :zonk #{1 2 3}}
(letfn [(pset [[f & r :as c]] (when c (lazy-cat (map #(conj % f) (pset r)) (pset r) [#{f}])))] (pset [1 2 3 4]))
(#{1 2 3 4} #{1 2 4} #{1 2 3} #{1 3 4} #{1 4} #{1 3} #{1 2} #{2 3 4} #{2 4} #{2 3} #{3 4} #{4} #{3} #{2} #{1})
(reduce #(if (= (last %1) %2) %1 (conj %1 %2)) [] [1 1 2 3 3 3 3 3 3 3 3 3 2 2 3])
[1 2 3 2 3]
((fn paths [root m] (if (map? m) (mapcat (fn [[k v]] (paths (conj root k) v)) m) [root])) [] {:a {:b 1, :c {:d 2, :e 3}}})
([:a :b] [:a :c :d] [:a :c :e])
(defn path-to [target m path] (if (some #{target} (keys m)) path (first (for [[k v] m :when (map? v)] (path-to target v (conj path k))))))
#'user/path-to
(merge-with (fn [a b] (if (vector? a) (conj a b) [a b]) {:a (list 1 2 3)} {:a (list 4 5 6)} {:a (list 7 8 9)}))
nil
(let [a {0 0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7, 9 9}] [(class (conj a [8 8])) (class a)])
[clojure.lang.PersistentHashMap clojure.lang.PersistentHashMap]
(loop [i 5 v []] (if (zero? i) v (recur (dec i) (loop [j 5 v v] (if (zero? j) v (recur (dec j) (conj v [i j])))))))
[[5 5] [5 4] [5 3] [5 2] [5 1] [4 5] [4 4] [4 3] [4 2] [4 1] [3 5] [3 4] [3 3] [3 2] [3 1] [2 5] [2 4] [2 3] [2 2] [2 1] [1 5] [1 4] [1 3] [1 2] [1 1]]
(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
(reduce (fn [acc [k vs]] (reduce (fn [acc v] (update-in acc [v] (fnil conj []) k)) acc vs)) {} {1 [:a :b :c], 2 [:a :c], 3 [:a :d]})
{:a [1 2 3], :b [1], :c [1 2], :d [3]}
(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
(->> [{:a {:c 2, :b 1}} {:a {:c 3, :b 4}} {:a {:c 4}}] (apply concat) (reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) {}))
{:a [{:b 1, :c 2} {:b 4, :c 3} {:c 4}]}
(let [v [1 2 3 1 2 4]] (reduce (fn [a b] (if (< (peek a) b) (conj a b) a)) (-> v (get 0) vector) v))
[1 2 3 4]
(reduce-kv (fn [acc k vs] (reduce (fn [acc v] (update-in acc [v] (fnil conj []) k)) acc vs)) {} {1 [:a :b :c], 2 [:a :c], 3 [:a :d]})
{:a [1 2 3], :b [1], :c [1 2], :d [3]}
(defn ba [s [f & r]] (if f (let [y (gensym)] `(-> ~(peek s) ~f (as-> ~y ~(ba (conj s y) r)))) s))
#'user/ba
(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"]}
(apply merge-with (fn [a b] (if (vector? a) (conj a b) (vector a b))) [{:a {:c 2, :b 1}} {:a {:c 3, :b 4}} {:a {:c 4}}])
{:a [{:b 1, :c 2} {:b 4, :c 3} {:c 4}]}
(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 [fs {:foo +, :bar -, :zonk reduce} params {:foo [1 2], :bar [10 5], :zonk [conj #{} [1 2 3]], :nonexistent 5}] (merge-with apply fs params))
{:bar 5, :foo 3, :nonexistent 5, :zonk #{1 2 3}}