(let [v '(1 2 3 1 4)] (first (reduce (fn [[xs l] x] (if (< x l) [xs l] [(conj xs x) x])) [[] (first v)] v)))
[1 2 3 4]
((fn perm [r m p] (if (> m 0) (mapcat #(perm (disj r %) (dec m) (conj p %)) r) [p])) '#{a b c d e} 5 [])
([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 ...
(let [l [1 1 1 2 3 3 1 1 4 4 4 4]] (reduce #(if (= (last %1) %2) %1 (conj %1 %2)) [(first l)] (rest l)))
[1 2 3 1 4]
(defmacro when-and-let [bnds & body] (loop [a (partition 2 bnds) b (cons 'do body)] (if (empty? a) b (recur (butlast a) (cons 'when-let (conj (list b) (vec (last a))))))))
#'user/when-and-let
((fn [& ms] (let [base (zipmap (mapcat keys ms) (repeat []))] (apply merge-with conj base ms))) {:a 1, :b 2, :c 3} {:a 3, :b 4, :d 4} {:a 5})
{:a [1 3 5], :b [2 4], :c [3], :d [4]}
(loop [primes [] [f & r] ((comp rest rest) (range))] (let [sieved (remove #(zero? (mod % f)) r)] (if (= (count primes) 10) primes (recur (conj primes f) sieved))))
[2 3 5 7 11 13 17 19 23 29]
(reduce (fn [[acc s] n] [(conj acc (subs s 0 n)) (subs s n)]) [[] "this is the input string, is it long enough?"] [8 4 4 4 12])
[["this is " "the " "inpu" "t st" "ring, is it "] "long enough?"]
((fn [& ms] (let [base (zipmap (set (mapcat keys ms)) (repeat []))] (apply merge-with conj base ms))) {:a 1, :b 2, :c 3} {:a 3, :b 4, :d 4} {:a 5})
{:a [1 3 5], :b [2 4], :c [3], :d [4]}
(reduce (fn [m [k v]] (update-in m [k] (fnil conj #{}) v)) {15 #{5 10}, 25 #{5}} (map (juxt #(+ 5 %) identity) #{10 20 30}))
{15 #{5 10}, 25 #{5 20}, 35 #{30}}
(let [replace (fn [m & x] (assert (every? (partial contains? m) (keep-indexed #(if (even? %1) %2) x))) (apply assoc (conj (seq x) m)))] (replace {:a 1} :a 2))
{:a 2}
((defn fib [n] (letfn [(f [x a] (if (= x 2) a #(f (dec x) (conj a (apply +' (take-last 2 a))))))] (trampoline f n [0 1]))) 10)
[0 1 1 2 3 5 8 13 21 34]
(try (reduce (fn [s x] (if (contains? s x) (throw (ex-info "" {:dup x})) (conj s x))) #{} '(1 2 3 3 4)) nil (catch clojure.lang.ExceptionInfo e (:dup (ex-data e))))
3
(let [m {:a 0, :b 1, :c 2}] (reduce (fn [m' [k :as e]] (if (= (find m k) e) m' (conj m' e))) {} {:a 0, :b 1, :c 3}))
{:c 3}
(let [replace (fn [m & x] (assert (every? (partial #(contains? %1 %2) m) (keep-indexed #(if (even? %1) %2) x))) (apply assoc (conj (seq x) m)))] (replace {:a 1} :a 2))
{:a 2}
(map (fn [[k v]] (list* (name k) v)) (apply merge-with (fn [a b] (conj (if (list? a) a (list a)) b)) [{:text "asdad", :value "123"} {:text "asdad", :value "123"} {:text "asdad", :value "123"}]))
(("text" "asdad" "asdad" "asdad") ("value" "123" "123" "123"))
((fn [& ms] (let [base (zipmap (set (mapcat keys ms)) (repeat []))] (reduce #(merge-with conj % %2) base ms))) {:a 1, :b 2, :c 3} {:a 3, :b 4, :d 4} {:a 5})
{:a [1 3 5], :b [2 4], :c [3], :d [4]}
(reduce #(if (and (seq %1) (= (zero? %2) (zero? (last %1)))) (update %1 (dec (count %1)) (partial + %2)) (conj %1 %2)) [] [0 1 1 1 0 0 0 1 0 1 1])
[0 3 0 1 0 2]
(let [replace (fn [m & x] (assert (every? (partial #(contains? %1 %2) m) (keep-indexed #(if (even? %1) %2) x))) (apply assoc (conj (seq x) m)))] (replace {:a 1} :b 2))
{:a 1, :b 2}
((fn [n] (letfn [(f [n a] (if (= n 2) a #(f (dec n) (conj a (+' (last a) (nth a (- (count a) 2)))))))] (trampoline f n [0 1]))) 10)
[0 1 1 2 3 5 8 13 21 34]
((fn flatify [coll] (loop [remainder coll result []] (if (empty? remainder) result (if (coll? (first remainder)) (recur (rest remainder) (concat result (flatify (first remainder)))) (recur (rest remainder) (conj result (first remainder))))))) [1 [2 3] 4])
(4 1 2 3)
(apply merge-with #(flatten (conj [%1] %2)) (mapv (fn [m] {(:date m) (:time m)}) [{:date "2013/12/28", :time "19:30"} {:date "2013/12/28", :time "19:31"} {:date "2013/12/28", :time "21:30"} {:date "2014/1/1", :time "19:30"} {:date "2014/1/1", :time "21:30"}]))
{"2013/12/28" ("19:30" "19:31" "21:30"), "2014/1/1" ("19:30" "21:30")}
(reduce (fn [r0 [from repliers]] (concat r0 (reduce (fn [r1 [to dates]] (conj r1 [[from to] dates])) [] repliers))) [] {:a {:b [1 2 3], :c [5 6]}, :b {:c [7], :d [9 10]}})
([[:a :b] [1 2 3]] [[:a :c] [5 6]] [[:b :c] [7]] [[:b :d] [9 10]])
(map (fn [[k v]] (apply vector (name k) v)) (apply merge-with (fn [a b] (conj (if (vector? a) a [a]) b)) [{:text "asdad", :value "123"} {:text "asdad", :value "123"} {:text "asdad", :value "123"}]))
(["text" "asdad" "asdad" "asdad"] ["value" "123" "123" "123"])
(class (conj (array-map :a 1 :c 2 :d :e :f :g :h :i :j :k :l :m :n :o :p :q :r :s :t :u :v :w :x :y :z :z) [:b 2]))
clojure.lang.PersistentHashMap
(map (fn [[id fs]] {:id id, :f (vec fs)}) (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"}]))
({:f ["a" "b"], :id 1} {:f ["c"], :id 2})
(defn partition-when ([p? [c & cs]] (partition-when p? cs [c])) ([p? [c & cs] acc] (cond (empty? cs) [acc] (p? c) (lazy-seq (cons acc (partition-when p? cs [c]))) :else (partition-when p? cs (conj acc c)))))
#'user/partition-when
(defn lazy-look-and-generate [] (iterate (fn [x] (mapcat (fn [n] (loop [x1 (first n) y (rest n) count 1] (if (= x1 (first y)) (recur x1 (rest y) (inc count)) (conj nil count x1)))) x)) [1]))
#'user/lazy-look-and-generate
(defn fib [n] (if (= n 1) [1] (if (= n 2) [1 1] (conj (fib (- n 1)) (+ (nth (fib (- n 1)) (- n 2)) (nth (fib (- n 1)) (- n 3)))))))
#'user/fib
(defn type-splitter [coll] (loop [remainder coll result {}] (cond (empty? remainder) result (contains? result (type (first remainder))) (recur (rest remainder) result) :else (recur (rest remainder) (conj result [(type (first remainder)) (filter (partial = (type (first remainder))) remainder)])))))
#'user/type-splitter
(first (reduce (fn [[acc pa] [a b c]] (let [a (if (empty? a) pa a)] [(conj acc [a b c]) a])) [] [["fresh" :hello :world] ["" :foo :bar] ["" :bar :baz] ["cool" :qux :qex] ["" :etc :etc]]))
(["cool" :etc :etc] ["cool" :qux :qex] ["fresh" :bar :baz] ["fresh" :foo :bar] ["fresh" :hello :world])
((fn [c] (last (take-while #(not (nil? (peek %))) (iterate (fn [w] (let [s (peek w) n (rand-nth (get c s))] (conj w n))) [(first (rand-nth (seq c)))])))) {"baby" ["hello"], "hello" ["world" "my"], "my" ["baby" "darling"]})
["baby" "hello" "my" "baby" "hello" "my" "darling"]
(defn type-splitter [coll] (loop [remainder coll result {}] (cond (empty? remainder) result (contains? result (type (first remainder))) (recur (rest remainder) result) :else (recur (rest remainder) (conj result [(type (first remainder)) (filter #((= (type %1) (type %2))) remainder)])))))
#'user/type-splitter
((fn [xs] (first (reduce (fn [[is x] [j y]] (cond (< y x) [[j] y] (= y x) [(conj is j) x] :else [is x])) [[] Double/POSITIVE_INFINITY] (map-indexed vector xs)))) [1 2 1 0 8 0])
[3 5]
(defn partition-when ([p? [c & cs]] (partition-when p? cs [c])) ([p? [c & cs :as coll] acc] (cond (empty? coll) [acc] (p? c) (lazy-seq (cons acc (partition-when p? cs [c]))) :else (partition-when p? cs (conj acc c)))))
#'user/partition-when
((fn [vector-of-maps] (reduce (fn [acc m] (reduce (fn [m [k v]] (if (contains? m k) (update-in m [k] conj v) (assoc m k [v]))) acc m)) {} vector-of-maps)) [{:a 1, :b 2} {:a 3, :b 4}])
{:a [1 3], :b [2 4]}
((fn step ([coll] (step #{} coll)) ([seen? coll] (when-let [[x & coll] (seq coll)] (if (seen? x) (cons x (step seen? coll)) (step (conj seen? x) coll))))) [0 1 2 1 2 1 3 4])
(1 2 1)
(defn keep-taking-while ([pred coll] (keep-taking-while pred [] coll)) ([pred old coll] (if (not (empty? coll)) (let [new (first coll) rest-of-new (rest coll) old-and-new (conj old new) result (pred old-and-new)] (if result (recur pred old-and-new rest-of-new) old)) [])))
#'user/keep-taking-while
(defn loop-with-keys ([f e] (loop-with-keys f e [])) ([f e keychain] (f e keychain) (let [tr (cond (map? e) e (coll? e) (map list (range) e) :else nil)] (doseq [[k el] tr] (loop-with-keys f el (conj keychain k))))))
#'user/loop-with-keys
((fn [i] (mapcat list (loop [o [] i i [f & fs] (reverse (reductions * [1 60 24 7]))] (if f (recur (conj o (int (/ i f))) (rem i f) fs) o)) [:weeks :days :hours :minutes])) 8707)
(0 :weeks 6 :days 1 :hours 7 :minutes)
(defn type-splitter [coll] (loop [remainder coll result {}] (cond (empty? remainder) result (contains? result (type (first remainder))) (recur (rest remainder) result) :else (recur (rest remainder) (conj result [(type (first remainder)) (filter #(= (type (first remainder)) (type %)) remainder)])))))
#'user/type-splitter
(defn take-while-coll ([pred coll] (take-while-coll pred coll [])) ([pred coll so-far] (let [e (first coll) with (conj so-far e)] (when (seq coll) (if (pred e) (lazy-seq (cons with (take-while-coll pred (rest coll) with))) (take-while-coll pred (rest coll) []))))))
#'user/take-while-coll
(defn type-splitter [coll] (loop [remainder coll result {}] (cond (empty? remainder) result (contains? (type (first remainder)) result) (recur (rest remainder) result) :else (recur (rest remainder) (conj result [(type (first remainder)) (filter (partial = (type (first remainder))) remainder)])))))
#'user/type-splitter
(let [listInput [] listInput (conj listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}])] listInput)
[[{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]]
(let [input ">>><<v^"] (count (distinct (reduce (fn [[latest & others :as houses] dir] (conj houses (condp = dir \< (update-in latest [0] dec) \> (update-in latest [0] inc) \^ (update-in latest [1] inc) \v (update-in latest [1] dec)))) '([0 0]) input))))
5
(mapv (fn [[k v]] {:date k, :time (vec v)}) (apply merge-with #(flatten (conj [%1] %2)) (mapv (fn [m] {(:date m) (:time m)}) [{:date "2013/12/28", :time "19:30"} {:date "2013/12/28", :time "19:31"} {:date "2013/12/28", :time "21:30"} {:date "2014/1/1", :time "19:30"} {:date "2014/1/1", :time "21:30"}])))
[{:date "2013/12/28", :time ["19:30" "19:31" "21:30"]} {:date "2014/1/1", :time ["19:30" "21:30"]}]
(let [k-filter (fn [el coll] (filter #(not= el %) coll)) permute (fn perms [n coll] (if (= n 1) (map #(vector %) coll) (for [el coll nlis (perms (- n 1) (k-filter el coll))] (conj nlis el))))] (permute 4 [1 2 3 4]))
([4 3 2 1] [3 4 2 1] [4 2 3 1] [2 4 3 1] [3 2 4 1] [2 3 4 1] [4 3 1 2] [3 4 1 2] [4 1 3 2] [1 4 3 2] [3 1 4 2] [1 3 4 2] [4 2 1 3] [2 4 1 3] [4 1 2 3] [1 4 2 3] [2 1 4 3] [1 2 4 3] [3 2 1 4] [2 3 1 4] [3 1 2 4] [1 3 2 4] [2 1 3 4] [1 2 3 4])
(defn sub-seq [coll] (loop [remainder coll sub [] longest-sub []] (cond (empty? remainder) longest-sub (empty? sub) (recur (rest remainder) [(first remainder)] longest-sub) (> (first remainder) (last sub)) (recur (rest remainder) (conj sub (first remainder)) (if (> (count sub) (count longest-sub)) sub longest-sub)) :else (recur (rest remainder) [(first remainder)] (if (> (count sub) (count longest-sub)) sub longest-sub)))))
#'user/sub-seq