(reduce #(if ((set %) %2) % (conj % %2)) [] [1 1 2 1 3 2 3 3])
[1 2 3]
(defn group-with [kf vf coll] (reduce (fn [acc x] (update-in acc [(kf x)] (fnil conj []) (vf x))) {} coll))
#'user/group-with
(let [a (atom [])] (clojure.walk/postwalk #(do (swap! a conj %) %) '(+ a (- b c))) @a)
[+ a - b c (- b c) (+ a (- b c))]
(defn make-connection-thingy [] (let [connections (atom [])] {:onconnect (fn [x] (swap! connections conj x)), :onmessage (fn [x] (println x))}))
#'user/make-connection-thingy
(let [ms [{:a 1, :b 2} {:c 3}] new-last (merge (last ms) {:d 4})] (conj (pop ms) new-last))
[{:a 1, :b 2} {:c 3, :d 4}]
(transduce (map :age) (completing conj count) #{} [{:age 1} {:age 1} {:age 1} {:age 2} {:age 2} {:age 3}])
3
(loop [coll [1] state 1] (if (> (count coll) 10) coll (recur (conj coll state) (+ (last coll) state))))
[1 1 2 3 5 8 13 21 34 55 89]
(let [v '[a b c d e]] (-> (subvec v 0 2) (conj 'X) (into (subvec v 2))))
[a b X c d e]
(ffirst (drop-while (comp not zero? second) (iterate (fn [[i r]] [(conj i (rem r 10)) (quot r 10)]) [() 420])))
(4 2 0)
((fn [a & args] (if (empty? args) a (recur (conj a (first args)) (rest args)))) nil 1 2 3)
(3 2 1)
(loop [xs (reverse [1 2 3]) ys []] (if (seq xs) (recur (rest xs) (conj ys (vec xs))) (reverse ys)))
([1] [2 1] [3 2 1])
(reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) {} [[:a 1] [:a 2] [:b 10] [:b 20]])
{:a [1 2], :b [10 20]}
(reduce #(if (= (last %1) %2) %1 (conj %1 %2)) [] [1 1 2 3 3 2 2 3])
[1 2 3 2 3]
(loop [x (range 20) acc []] (if (< (reduce + acc) 8) (recur (next x) (conj acc (first x))) acc))
[0 1 2 3 4]
(reduce (fn [m [id f]] (assoc m id (conj (get m id []) f))) {} [[1 "a"] [1 "b"] [2 "c"]])
{1 ["a" "b"], 2 ["c"]}
(reduce (fn [s x] (if (contains? s x) (reduced x) (conj s x))) #{} '(1 2 3 3 4))
3
(= [(*) (+ (*) (*)) (+ (*) (*) (*)) (+ (*) (*) (*) (*))] (conj '(2 3 4) 1))
true
(defn mymap [f l] (loop [l l r []] (if (empty? l) r (recur (rest l) (conj r (f (first l)))))))
#'user/mymap
(let [x1 (list "one" "two" "three") x2 (conj x1 "zero")] (every? #(apply identical? %) (map vector x1 (rest x2))))
true
(loop [s "foo bar baz" part "" strs []] (if (empty? s) (conj strs part) (if (= (first s) \space) (recur (subs s 1) "" (if (empty? part) strs (conj strs part))) (recur (subs s 1) (str part (first s)) strs))))
["foo" "bar" "baz"]
(reduce (fn [m [k v]] (assoc m k (conj (get m k #{}) v))) {} [[:bob :beer] [:bob :bread] [:alice :apples]])
{:alice #{:apples}, :bob #{:beer :bread}}
(reduce #(for [x % y %2] (conj x y)) [[]] [#{1 2} #{30 40 50} #{600 700}])
([1 50 600] [1 50 700] [1 40 600] [1 40 700] [1 30 600] [1 30 700] [2 50 600] [2 50 700] [2 40 600] [2 40 700] [2 30 600] [2 30 700])
(reduce (fn [so-far [value & origs]] (conj so-far (* value value))) [] (take-while identity (iterate next [1 2 3 4])))
[1 4 9 16]
(reduce (fn [acc x] (if (zero? x) (reduced acc) (conj acc x))) [] [2 3 4 0 3 2 1])
[2 3 4]
(reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) {} [[:a 50] [:a 10] [:a 30] [:b 20]])
{:a [50 10 30], :b [20]}
(reduce (fn [acc [k v]] (assoc acc v (conj (get acc v []) k))) {} {:a 1, :b 1, :c 1})
{1 [:a :b :c]}
(let [items '(a b c)] (reduce (fn [[i acc] x] [(inc i) (conj acc [x i])]) [0 []] items))
[3 [[a 0] [b 1] [c 2]]]
(letfn [(mapf [f xs] (reduce (fn [mapped x] (conj mapped (f x))) [] xs))] (mapf inc '(1 2 3)))
[2 3 4]
(apply str (first (last (take 32 (iterate (fn [[bits left]] [(conj bits (bit-and left 1)) (bit-shift-right left 1)]) [() -1])))))
"1111111111111111111111111111111"
(def orig (loop [ret (sorted-set) i 0] (if (< i 200000) (recur (conj ret (rand-int 100000)) (+ i 1)) ret)))
#'user/orig
((fn structure [s] (mapcat (fn [x] (if (coll? x) (conj (empty x) (structure x)) ())) s)) [1 2 '(3 4)])
(())
(let [res (atom [])] (clojure.walk/prewalk #(do (when (number? %) (swap! res conj %)) %) {1 [2 #{3 4}]}) @res)
[1 2 4 3]
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} [[:a 1] [:a 2] [:b 1] [:b 7]])
{:a [1 2], :b [1 7]}
(for [[prefix & suffixes] '((a (b c) (c b)) (b (a c) (c a))) suffix suffixes] (conj suffix prefix))
((a b c) (a c b) (b a c) (b c a))
(letfn [(f [s] (reduce (fn [acc _] (conj acc (take (-> acc count inc) s))) [] s))] (f [1 2 3]))
[(1) (1 2) (1 2 3)]
(reverse (ffirst (drop-while (comp not zero? second) (iterate (fn [[i r]] [(conj i (rem r 10)) (quot r 10)]) [[] 420]))))
(4 2 0)
(defn drop-idx [idx coll] (reduce #(if (= (key %2) idx) %1 (conj %1 (val %2))) [] (zipmap (range (count coll)) coll)))
#'user/drop-idx
(letfn [(pile [n cards] (->> (range n) (mapcat #(take-nth n (drop % cards))) (reduce conj nil)))] (pile 3 (range 10)))
(8 5 2 7 4 1 9 6 3 0)
(let [pop-peek (juxt pop peek) stack [1 2] [stack arg1] (pop-peek stack) [stack arg2] (pop-peek stack)] (conj stack (+ arg1 arg2)))
[3]
(defn dedup2 [coll] (if (seq coll) (reduce #(if (= %2 (last %1)) %1 (conj %1 %2)) [(first coll)] (rest coll))))
#'user/dedup2
(defn pivot [p xs] (loop [prior [] [h & r] xs] (if (p h) [prior h r] (recur (conj prior h) r))))
#'user/pivot
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} (concat {:a 1, :b 2} {:a "a", :c "c"}))
{:a [1 "a"], :b [2], :c ["c"]}
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} (concat {:a 10, :b 20} {:a 30, :c 40}))
{:a [10 30], :b [20], :c [40]}
(defn add-peak [peak peaks] (if (>= (reduce + peak) (apply max (map #(reduce + %) peaks))) (conj peaks peak) peaks))
#'user/add-peak
(reduce #(for [x % y %2] (conj x y)) [[]] [#{1 2} #{30 40 50} #{} #{600 700}])
()
(reduce #(+ %1 (mod %2 %1)) (conj '(1 2 3 4) (mod (first '(1 2 3 4)) 5)))
1
(reduce (fn [[a v] k] [(get a k) (conj v k (get a k))]) [{:a {:b {:c 321}}} []] [:a :b :c])
[321 [:a {:b {:c 321}} :b {:c 321} :c 321]]
(let [f (fn [n m] (conj (->> n range (partition (int (/ n m))) (map first) vec) n))] (f 100 3))
[0 33 66 100]
(let [v (with-meta [1 2] {:foo :bar})] (map #(meta %) [v (conj v 3) (map identity v) (subvec v 1)]))
({:foo :bar} {:foo :bar} nil nil)
(reduce (fn [m [k v]] (update m k (fnil conj []) v)) {} (partition 2 '(:k1 a :k1 b :k2 c)))
{:k1 [a b], :k2 [c]}