(let [thing [[0 1] [1 2] [2 3] [3 4] [4 5] [5 6] [6 7] [7 8] [8 9] [9 10]]] (for [i thing idx [0 1]] (nth i idx)))
(0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10)
(let [cunescape (fn [s] (apply str (map #(-> %1 (clojure.string/replace "\\r" "\r") (clojure.string/replace "\\n" "\n") (clojure.string/replace "\\t" "\t") (clojure.string/replace #"\\(.)" "$1")) (re-seq #"\\.|[^\\]*" s))))] (cunescape "test\\r\\n\\t\\'"))
"test\r\n\t'"
(let [n 5/7] (->> #(random-sample n (range 10)) (repeatedly) (map-indexed list) (filter (fn [[index item]] (and (> (count item) 1) (not (zero? (last item))) (= n (apply / item))))) (ffirst)))
5319
(let [{:keys [a b c]} {:a 1, :b 2, :c 3}] (str "My a is " a " and my b is " b " and my c is " c))
"My a is 1 and my b is 2 and my c is 3"
(let [n 1/7] (->> #(random-sample n (range 10)) (repeatedly) (map-indexed list) (filter (fn [[index item]] (and (> (count item) 1) (not (zero? (last item))) (= n (apply / item))))) (ffirst)))
198
(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]])
[[1 5]]
(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}
(let [m {:a {:b [1 2 3 4]}, :b {:c [5 6 7]}}] (into {} (map (fn [[k m2]] [k (zipmap (keys m2) (map #(apply + %) (vals m2)))]) m)))
{:a {:b 10}, :b {:c 18}}
(let [s (str '(1 2 3)) s1 (binding [*print-length* 1] (str '(1 2 3))) s2 (binding [*print-length* 2] (str '(1 2 3)))] [(count s) (count s1) (count s2)])
[7 7 7]
(let [m [{:k "a"} {:k "b"} {:k "c"} {:k "m"} {:k "c"} {:k "b"}] s "abc" l (count s)] (filter #(= s (apply str (map :k %))) (partition l 1 m)))
(({:k "a"} {:k "b"} {:k "c"}))
(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}
(let [coll [1 2 3 3 4 5 4 6 7 8 9 6 6] singles (->> coll frequencies (filter #(= (last %) 1)) (map first))] (remove (set singles) coll))
(3 3 4 4 6 6 6)
(first (let [m [[1 2] [3 4]]] (for [x (range (count m)) y (range (count (first m))) :let [elt (get-in m [x y])] :when (= elt 3)] [x y])))
[1 0]
(reduce (fn [ranges [start end :as el]] (let [[s e] (peek ranges)] (if (> start (inc e)) (conj ranges el) (conj (pop ranges) [s end])))) [[1 1]] [[1 2] [3 3] [4 5]])
[[1 5]]
(letfn [(parse-fraction [xs] (let [[m n] (reduce (fn [[a b] x] [(+ x (* a 10)) (* b 10)]) [0 0.1] xs)] (* 0.1 (/ m n))))] (parse-fraction [1 2 3 4]))
0.12340000000000001
(letfn [(nils [c] (let [[c1 & cs] c] (if cs (concat (map #(cons c1 %) (nils cs)) (map #(cons nil %) (nils cs))) [[c1] [nil]])))] (nils '(1 2 3)))
((1 2 3) (1 2 nil) (1 nil 3) (1 nil nil) (nil 2 3) (nil 2 nil) (nil nil 3) (nil nil nil))
(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 2] [3 3] [4 5]])
[[1 2] [3 3] [4 5]]
(let [m [{:k "aa"} {:k "bb"} {:k "cc"} {:k "mm"} {:k "cc"} {:k "bb"}] s "aabbcc" l (count s)] (filter #(= s (apply str (map :k %))) (partition l 1 m)))
()
(letfn [(rl [xs] (lazy-seq (when (seq xs) (let [[h t] (split-with (partial = (first xs)) (rest xs))] (cons (cons (first xs) h) (rl t))))))] (rl [1 1 1 2 2 3 3 3]))
((1 1 1) (2 2) (3 3 3))
(let [[i j] [4 5] size 5] (for [x [-1 1] y [-1 1] :let [new-x (+ i x) new-y (+ j y)] :when (every? #(< % size) [new-x new-y])] [new-x new-y]))
([3 4])
(let [a (promise) b (promise)] (deliver a (fn [x] (if (< x 10) x (@b (/ x 2))))) (deliver b (fn [x] (if (> x 100) x (@a (* x 13/10))))) (@a 42))
599781/80000
(let [a (promise) b (promise)] (deliver a (fn [x] (if (< x 0) x (@b (/ x 2))))) (deliver b (fn [x] (if (> x 100) x (@a (- x 10))))) (@a 20))
-10
(let [charset (map char (range 97 123)) rot (concat (drop 13 charset) (take 13 charset)) rmap (zipmap charset rot) rotate (fn [c] (or (rmap c) c))] (apply str (map rotate "guvf vf ebg13")))
"this is rot13"
(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] [4 5] [7 9] [6 6]])
[[1 3] [4 5] [7 6]]
(defn fn-from-code [code] (let [sp (clojure.string/split code #"\s+")] (fn [& args] (when (= (count args) (dec (count sp))) (eval (read-string (str "(" (first sp) " " (clojure.string/join " " args) ")")))))))
#'user/fn-from-code
(let [v [[0 12] [1 23] [3 65]] m (apply max (map first v)) o (vec (repeat (inc m) 0))] (reduce (fn [o [k v]] (assoc o k v)) o v))
[12 23 0 65]
((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]}
((fn f [xs] (lazy-seq (when (seq xs) (let [[zeros [num & more]] (split-with zero? xs)] (list* zeros num (f more)))))) '(2 0 0 2 5 0 9 0 0 0 10 2))
(() 2 (0 0) 2 () 5 (0) 9 (0 0 0) 10 () 2)
(letfn [(endpoints [n m] (let [chunk (int (/ n m))] (loop [r [] prev 0 curr chunk] (if (> curr n) (conj r n) (recur (conj r prev) curr (+ curr chunk))))))] (endpoints 100 3))
[0 33 66 100]
(let [m "merry had a little lamb whose fleece was white as snow, and every where that mary went the lamb was sure to go"] [(hash m) (sort [(hash m) (hash "a")]) (sort [m "a"])])
[-565463861 (-565463861 1455541201) ("a" "merry had a little lamb whose fleece was white as snow, and every where that mary went the lamb was sure to go")]
(reduce (fn [x y] (let [a (get x 1) b (get y 1)] (if (neg? (compare a b)) x y))) [["Coke" "16" ""] ["Coke" "35" "3"] ["Coke" "2" "3"] ["Pepsi" "" ""] ["Coke" "" "36"]])
["Coke" "" "36"]
(defn overwrite [initial-coll over-coll] (let [init (vec initial-coll) over (vec over-coll) i-len (count init) o-len (count over) longer (max i-len o-len)] (mapv #(if (< % o-len) (get over %) (get init %)) (range longer))))
#'user/overwrite
(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 unique-permutations [xs] (let [m (frequencies xs)] ((fn self [m] (if (empty? m) [[]] (mapcat (fn [[x n]] (map #(cons x %) (self (if (= 1 n) (dissoc m x) (update m x dec))))) m))))))
#'user/unique-permutations
(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] [4 5] [7 9] [8 8]])
[[1 3] [4 5] [7 8]]
(let [l [1 2 3 4 5 5 4 3 2 1]] (every? #(= (first %) (last %)) (map vector (take (/ (count l) 2) l) (reverse (drop (/ (count l) 2) l)))))
true
(reduce (fn [x y] (let [a (get x 1) b (get y 1)] (if (pos? (compare a b)) x y))) [["Coke" "16" ""] ["Coke" "35" "3"] ["Coke" "2" "3"] ["Pepsi" "" ""] ["Coke" "" "36"]])
["Coke" "35" "3"]
(first (let [m [[1 2] [3 4]]] (for [x '(0 1) y '(0 1) :let [elt (get-in m (doto [x y] (->> (println "trying"))))] :when (= elt 3)] [x y])))
[1 0]
"trying [0 0]\ntrying [0 1]\ntrying [1 0]\n"
(let [dict {'dup (fn [[x & s] _] (conj s x x)), '* (fn [[x y & s] _] (conj s (* x y)))}] (reductions #((dict %2 conj) %1 %2) () '(5 dup *)))
(() (5) (5 5) (25))
(let [cunescape (fn [s] (apply str (map #(reduce (fn [l [m r]] (clojure.string/replace l m r)) %1 [["\\r" "\r"] ["\\n" "\n"] ["\\t" "\t"] [#"\\(.)" "$1"]]) (re-seq #"\\.|[^\\]*" s))))] (cunescape "test\\r\\n\\t\\'"))
"test\r\n\t'"
(let [data [{:a 1, :b true} {:b 2, :c false} {:c [3 4 5], :b true} {:a 6, :b false}]] (mapcat #(map hash-map (cycle %1) %2) (map keys data) (map (comp flatten vals) data)))
({:a 1} {:b true} {:b 2} {:c false} {:c 3} {:b 4} {:c 5} {:b true} {:a 6} {:b false})
(let [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]]}]
(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 overwrite [initial-coll over-coll] (let [init (seq initial-coll) over (seq over-coll) i-len (count init) o-len (count over) longer (max i-len o-len)] (mapv #(if (< % o-len) (get over %) (get init %)) (range longer))))
#'user/overwrite
(let [ord #(quot % 1000)] (apply mapcat (fn [prev sub] (concat (repeat (dec (- (ord (first sub)) (ord (first prev)))) []) [sub])) ((juxt identity rest) (cons [0] (partition-by ord [1 35 456 2005 3045 3987])))))
((1 35 456) [] (2005) (3045 3987))
(let [dict {'dup (fn [[x & s] _] (conj s x x)), '* (fn [[x y & s] _] (conj s (* x y)))}] (reduce #((dict %2 conj) %1 %2) () '(5 dup *)))
(25)
(let [coll (range 1 25) target 12 cnt (count coll)] (->> (for [a (range 0 cnt) b (range 1 cnt)] [(nth coll a) (nth coll b)]) (filter (fn [[x y]] (= target (+ x y))))))
([1 11] [2 10] [3 9] [4 8] [5 7] [6 6] [7 5] [8 4] [9 3] [10 2])
(defn unique-permutations [xs] (let [m (frequencies xs)] ((fn self [m] (if (empty? m) [[]] (mapcat (fn [[x n]] (map #(cons x %) (self (if (= 1 n) (dissoc m x) (update m x dec))))) m))) m)))
#'user/unique-permutations
(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])
(defn square_root_three [] (loop [lower 1 upper 2 depth 50] (let [n (/ (+ upper lower) 2)] (if (== depth 0) n (if (< (* n n) 3) (recur n upper (dec depth)) (recur lower n (dec depth)))))))
#'user/square_root_three