(loop [v [] s (seq "string")] (if-not s v (recur (conj v (first s)) (next s))))
[\s \t \r \i \n \g]
(defn stair-step [seq] (reduce (fn [val item] (conj val (+ item (last val)))) [(first seq)] (rest seq)))
#'user/stair-step
((fn [f coll] (reduce (fn [acc x] (conj acc (f x))) [] coll)) inc [1 2 3 4])
[2 3 4 5]
(reduce #(update-in %1 [(ffirst %2)] (fnil conj []) (val (first %2))) {} [{:a "ab"} {:a "ac"} {:a "ad"}])
{:a ["ab" "ac" "ad"]}
(reduce (fn [ranges [start end :as el]] (let [[s e] (peek ranges)] (if (> start e) (conj ranges el) (conj (pop ranges) [s end])))) [[1 1]] [[1 3] [3 3] [3 5] [7 10] [9 20] [420 666]])
[[1 5] [7 20] [420 666]]
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} [[:a :b] [:a :c] [:d :e]])
{:a [:b :c], :d [:e]}
(next (reduce (fn [v x] (conj v (+ x (v (dec (count v)))))) [0] [3 3 4 3]))
(3 6 10 13)
(let [items '(a b c)] (reduce (fn [[i acc] x] [(inc i) (conj acc x)]) [0 []] items))
[3 [a b c]]
(-> {:foo [1 2]} (update-in [:foo] conj 3) (update-in [:foo 1] + 10) (update-in [:foo] (partial mapv inc)))
{:foo [2 13 4]}
(defn distinct-coll [coll] (reduce (fn [s x] (if (contains? s x) (reduced false) (conj s x))) #{} coll))
#'user/distinct-coll
(merge-with #(if (string? %1) [%1 %2] (conj %1 %2)) {:words "hello"} {:words "world"} {:nothing "nothing"} {:words "foobar"})
{:nothing "nothing", :words ["hello" "world" "foobar"]}
(defn dedup2 [coll] (if (seq coll) (reduce #(if (= %2 (last %1)) %1 (conj %1 %2)) coll)))
#'user/dedup2
(let [a (atom [])] (clojure.walk/prewalk #(do (swap! a conj %) %) '(+ a (- b c))) @a)
[(+ a (- b c)) + a (- b c) - b c]
(reduce (fn [acc item] (if (some (partial = item) acc) acc (conj acc item))) [] ["foo" "bar" "zonk" "foo"])
["foo" "bar" "zonk"]
(reduce #(conj % (get [:a :b :c :d :e] %2)) [] [1 2 3 2 1 0])
[:b :c :d :c :b :a]
(let [capacity 8] (reduce #(if (< (reduce + %1) capacity) (conj %1 %2) %1) [] (range 20)))
[0 1 2 3 4]
(merge-with conj {:a (), :b (), :c ()} {:a 1, :b 2} {:a 3, :c 1} {:a 5, :b 3})
{:a (5 3 1), :b (3 2), :c (1)}
(reduce (fn [[a v] k] [(get a k) (conj v k (get a k))]) [{} []] [:a :b :c])
[nil [:a nil :b nil :c nil]]
(reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) {} [[:a :b] [:a :c] [:d :e]])
{:a [:b :c], :d [:e]}
(reduce (fn [[i coll] el] (vector (inc i) (conj coll [el (inc i)]))) [0 []] [:a :b :c])
[3 [[:a 1] [:b 2] [:c 3]]]
(loop [m 9001 acc ()] (if (zero? m) acc (recur (quot m 10) (conj acc (rem m 10)))))
(9 0 0 1)
(reduce (fn [m [k v]] (update-in m [k] (fnil conj []) v)) {} [[:a 1] [:b 2] [:a 3]])
{:a [1 3], :b [2]}
(conj (vec (drop-last [1 2 3 4])) (let [l (last [1 2 3 4])] (* l l l)))
[1 2 3 64]
(rest (reductions (fn [acc x] (if (pos? x) (conj acc x) (reduced acc))) [] '(1 2 3 -4)))
([1] [1 2] [1 2 3] [1 2 3])
(let [v [0 1 2 3] i 2] (apply conj (subvec v 0 i) (nthnext v (inc i))))
[0 1 3]
(let [x {:foo 1, :bar 2}] (conj x {:bar (or (:bar x) 3), :baz (or (:baz x) 4)}))
{:bar 2, :baz 4, :foo 1}
(loop [acc () [f & r] [1 2 3 4 5]] (if f (recur (conj acc f) r) acc))
(5 4 3 2 1)
(reduce (fn [acc [k v]] (assoc acc v (conj (acc v) k))) {} {:a 1, :b 1, :c 2})
{1 (:b :a), 2 (:c)}
(defn fold-links [urls] (reduce (fn [acc [src dst]] (assoc acc dst (conj (get acc dst #{}) src))) {} urls))
#'user/fold-links
(merge-with conj {:hello {:x 1, :y 3, :other [4 5 6]}} {:hello {:x 3, :y 1, :other [3]}})
{:hello {:other [3], :x 3, :y 1}}
(let [ms [{:a 1, :b 2} {:c 3}]] (-> ms pop (conj (-> ms peek (merge {:d 4})))))
[{:a 1, :b 2} {:c 3, :d 4}]
(reduce (fn [m [k v]] (update-in m [k] (fnil conj #{}) v)) {} [[:bob :beer] [:bob :bread] [:alice :apples]])
{:alice #{:apples}, :bob #{:beer :bread}}
(reduce (fn [[state result] item] [(inc state) (conj result (+ state item))]) [0 []] [1 2 3 4 5])
[5 [1 3 5 7 9]]
(loop [l [] val 0] (if (= (count l) 5) (println l) (recur (conj l val) (inc val))))
nil
"[0 1 2 3 4]\n"
((fn f [accum & args] (if (seq args) (recur (conj accum args) (rest args)) accum)) [] 'a 'b 'c)
[(a b c) (b c) (c)]
((fn [s] (let [[h & t] s] (do (print (type t)) (conj t h)))) '(1 2 3))
(1 2 3)
"clojure.lang.PersistentList"
(apply conj {} (map (fn [pair] [(:old_id pair) (:_id pair)]) '({:_id "blah", :old_id 375} {:_id "hello", :old_id 376})))
{375 "blah", 376 "hello"}
(reduce (fn [m [prev next]] (update-in m [prev] (fnil conj []) next)) {} (partition-all 2 1 ["monty" "python" "monty" "hall"]))
{"hall" [nil], "monty" ["python" "hall"], "python" ["monty"]}
(merge-with (fn [vr v] (conj (if (seq? vr) vr [vr]) v)) {:a 1, :b 2} {:a 0.5, :c 4})
{:a [1 0.5], :b 2, :c 4}
(apply merge-with #((if (seq? %) conj list) % %2) '({:foo [1], :bar [2]} {:foo [2]} {:foo [3]}))
{:bar [2], :foo ([3] [1] [2])}
(reduce #(if (not= (last %1) %2) (conj %1 %2) %1) [] [1 1 2 3 3 2 2 3])
[1 2 3 2 3]
(do (def people (atom [])) (defn add-person [name age] (swap! people conj {:age age, :name name})) (add-person "guy" 30) @people)
[{:age 30, :name "guy"}]
(reduce (fn [s x] (if (contains? s x) (reduced x) (conj s x))) #{} '(1 2 3 4))
#{1 2 3 4}
(let [m (into {} (map vec (partition 2 (range 16))))] [(class m) (class (conj m [18 19] [20 21]))])
[clojure.lang.PersistentArrayMap clojure.lang.PersistentHashMap]
(let [f (fn [n m] (conj (->> n range (take-nth (int (/ n m))) vec) n))] (f 100 3))
[0 33 66 99 100]
(reduce (fn [a b] (if (number? b) (reduced a) (conj a b))) [] [:a :b :c :d 0 :e :f])
[:a :b :c :d]
(let [v [0 1 2] i 1] (reduce conj (subvec v 0 i) (subvec v (inc i) (count v))))
[0 2]
((partial (comp seq reduce) (comp (partial apply conj) (juxt first (comp (partial + 1) second)) list) []) (range 10))
(1 2 3 4 5 6 7 8 9 10)
(let [the-map {:a "alpha", :b "beta"}] (update the-map 1 #(if (seq %) (conj % :new-thing) #{:new-thing})))
{1 #{:new-thing}, :a "alpha", :b "beta"}
(reduce (fn [m [k v]] (assoc m k (conj (m k []) v))) {} [["x" 4] ["x" 5] ["y" 6]])
{"x" [4 5], "y" [6]}