(defmacro is-required [m ks] `(let [m# ~m] (doseq [k# ~ks] (assert (contains? m# k#) (str (pr-str m#) " should contain " (pr-str k#))))))
#'user/is-required
(defn foo [a & rst] (let [[b c & d] rst args [b c]] {:a a, :b b, :c c, :args args, :rst rst}))
#'user/foo
(let [na Double/NaN nana (* 0.0 Double/NaN) nanana (Double. Double/NaN) watman (Double. (* 0.0 Double/NaN))] (map #(= % %) [na nana nanana watman]))
(true true true true)
(defn split-seq [pred coll] (lazy-seq (when (seq coll) (let [coll (drop-while pred coll) [leading remaining] (split-with (complement pred) coll)] (cons leading (split-seq pred remaining))))))
#'user/split-seq
(let [m {:a 1, :b 2, :c {"foo" "bar"}}] (into (empty m) (apply concat (for [[k v] m] (if (map? v) v [[k v]])))))
{:a 1, :b 2, "foo" "bar"}
(let [return (promise)] (if false (deliver return 0)) (if nil (deliver return 1)) (if true (deliver return 2)) (if :redundant (deliver return 3)) @return)
2(let [ms '[{"hello" "world"} {"good" "year"} {"hello" "foo"} {"good" "nothing"} {"else" "there"}]] (apply merge-with into (for [m ms [k v] m] {k [v]})))
{"else" ["there"], "good" ["year" "nothing"], "hello" ["world" "foo"]}
(let [fib (fn [f n] (if (<= n 1) n (+ (f f (dec n)) (f f (- n 2)))))] (fib (memoize fib) 10))
55(loop [a 0 s (range 10)] (if (empty? s) a (let [a (+ a (first s))] (when-not (= a 5) (recur a (next s))))))
45(let [src (to-array [3 3 4 3])] (seq (amap src i dest (+ (aget src i) (if (pos? i) (aget dest (dec i)) 0)))))
(3 6 10 13)
(let [[[a _ _] [_ b _] [_ _ c]] '((1 2 3) (1 2 3) (1 2 3))] (println a b c))
nil"1 2 3\n"(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
(re-find #"\uffef" ",(defn whatami [n] (let [o (rand-nth [* /])] (o (reduce o (repeat (+ n (Math/floor (o (o (o 1/2))))) 2)))))")
nil((fn midpoint [idx v] (let [h (subvec v 0 idx) t (subvec v idx)] [h (first t) (subvec t 1)])) 5 (vec (range 15)))
[[0 1 2 3 4] 5 [6 7 8 9 10 11 12 13 14]]
((fn [e s] (let [r (take-while #(not= e %) s)] (concat r (drop (inc (count r)) s)))) 1 [2 1 1 2 3])
(2 1 2 3)
(defn -:> [v & pairs] (let [pairs (partition 2 pairs) keys (map first pairs) fns (map second pairs)] (zipmap keys ((apply juxt fns) v))))
#'user/-:>
(binding [*print-length* 20] (let [xs [3 2 1]] (mapcat (fn [[x idx]] (for [y (range x)] (assoc xs idx y))) (map vector xs (range)))))
([0 2 1] [1 2 1] [2 2 1] [3 0 1] [3 1 1] [3 2 0])
(let [listmap {:a [4 7 8 9], :b [1 3 8]}] (for [[k v] listmap [src dst] (partition 2 1 v)] [k src dst]))
([:a 4 7] [:a 7 8] [:a 8 9] [:b 1 3] [:b 3 8])
(let [midpoint (fn [idx coll] [(vec (take idx coll)) (nth coll idx) (vec (drop (inc idx) coll))])] (midpoint 3 [1 2 3 4 5]))
[[1 2 3] 4 [5]]
(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 1) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(do (use 'clojure.set) (let [a (ancestors (type 'wheeee)) b (ancestors (type :wheeee))] {:a-b (clojure.set/difference a b), :b-a (clojure.set/difference b a), :a*b (clojure.set/intersection a b)}))
{:a*b #{clojure.lang.IFn clojure.lang.IHashEq clojure.lang.Named java.io.Serializable java.lang.Comparable java.lang.Object java.lang.Runnable java.util.concurrent.Callable}, :a-b #{clojure.lang.AFn clojure.lang.IMeta clojure.lang.IObj}, :b-a #{}}
(defn move-up [v i] (let [pos (.indexOf v i)] (if (pos? pos) (-> v (assoc pos (v (dec pos))) (assoc (dec pos) i)) v)))
#'user/move-up
(let [stack [1 2] pop-peek (juxt pop peek) [stack arg1] (pop-peek stack) [stack arg2] (pop-peek stack)] (conj stack (+ arg1 arg2)))
[3]
(let [m {:a 0, :b 1, :c 2}] (reduce (fn [m' [k k']] (assoc m' k' (k m))) {} [[:a :x] [:b :y] [:c :z]]))
{:x 0, :y 1, :z 2}
(let [v (vec (range 100)) n (count v) {last-2 (- n 3), last-1 (- n 2), last (- n 1)} v] [last-2 last-1 last])
[97 98 99]
(let [most #(if (pos? (compare % %2)) % %2)] (reduce most (map vector (map #(* 2 %) (range 100)) (iterate inc 0))))
[198 99]
(let [l [1 2 3 4 5 5 4 3 2 1]] (every? #(= (first %) (last %)) (map vector l (reverse l))))
true(defn safe-comp [& fs] (fn [& args] (some-> (reduce #(let [r (apply %2 %1)] (if (nil? r) (reduced nil) [r])) args fs) first)))
#'user/safe-comp
(re-find #"\ufeff" ",(defn whatami [n] (let [o (rand-nth [* /])] (o (reduce o (repeat (+ n (Math/floor (o (o (o 1/2))))) 2)))))")
nil(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 0) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(let [my-non-neg-even? (fn [n] (cond (zero? n) true (pos? n) (recur (- n 2)) (neg? n) false))] (map my-non-neg-even? [1 4 5 10 80]))
(false true false true true)
(let [coll [1 2 3] pred #(= (mod % 2) 0) xs (filter pred coll)] (if (and (seq xs) (not (next xs))) xs))
(2)
(let [sec (/ 134000 1000) min (int (/ sec 60)) rem (- sec (* min 60))] (str min " minutes and " rem "seconds"))
"2 minutes and 14seconds"(let [well-behaved? (fn [ob] (= ob (read-string (binding [*print-dup* true] (pr-str ob)))))] (map well-behaved? ['tommy nil false 388 :what (symbol "388") (symbol "foo bar")]))
(true true true true true false false)
(defn make-rangemapper [a b a' b'] (let [scale (/ (- b' a') (- b a))] (fn [x] (+ a' (* (- x a) scale)))))
#'user/make-rangemapper
(let [{:keys #{a b c d e f g h i}} {:a 1, :b 2, :c 3, :d 4}] [a b c d])
[1 2 3 4]
(let [lift-map (fn [f n col] (((apply comp (repeat n (partial partial map))) f) col))] (lift-map inc 3 [[[1 2] [3] [4 5 6]]]))
(((2 3) (4) (5 6 7)))
((fn [[k [low high]]] (let [?k (symbol (str "?" (name k)))] (list ['?foo k ?k] [('> ?k low)] [('< ?k high)]))) [:bar [10 20]])
([?foo :bar ?bar] [10] [20])
(let [a (into {} [[0 12] [1 23] [3 65]]) b (apply max (map first a))] (map #(get a % 0) (range (inc b))))
(12 23 0 65)
(defn -:> [v & pairs] (let [pairs (partition 2 pairs) keys (map first pairs) fns (map second pairs)] (zipmap ((apply juxt fns) v) keys)))
#'user/-:>
(let [[x & xs] [1 2 3] [r _] (reduce (fn [[r x] y] [(and r (< x y)) y]) [true x] xs)] r)
true(let [sequence '((1 2) (3 4) (5 6) (7 8)) keys (map first sequence) vals (map second sequence)] (zipmap keys vals))
{1 2, 3 4, 5 6, 7 8}
(reduce (fn [m [k v]] (let [ks (map keyword (.split k "_"))] (assoc-in m ks v))) {} (partition 2 ["a_b_c" 1 "d" 2 "a_f" 3]))
{:a {:b {:c 1}, :f 3}, :d 2}
(binding [*print-length* 20] (prn (let [xs [3 2 1]] (mapcat (fn [[x idx]] (for [y (range x)] (assoc xs idx y))) (map vector xs (range))))))
nil"([0 2 1] [1 2 1] [2 2 1] [3 0 1] [3 1 1] [3 2 0])\n"(let [nums (partition 3 (range 10))] (concat nums (apply map list nums) (list (map nth nums (range))) (list (map nth nums (range 2 -1 -1)))))
((0 1 2) (3 4 5) (6 7 8) (0 3 6) (1 4 7) (2 5 8) (0 4 8) (2 4 6))
(loop [n 1034 result '()] (let [q (int (/ n 10)) r (rem n 10)] (if (= n 0) result (recur q (cons r result)))))
(1 0 3 4)
(let [evl (fn [x] ({'* *, '+ +, '- -, '/ /} x x))] (map evl '(+ - * / + 1 2 3)))
(#object [clojure.core$_PLUS_ 0x35b52c2b "clojure.core$_PLUS_@35b52c2b"] #object [clojure.core$_ 0x173365be "clojure.core$_@173365be"] #object [clojure.core$_STAR_ 0x3579ae31 "clojure.core$_STAR_@3579ae31"] #object [clojure.core$_SLASH_ 0x6ffaf3c6 "clojure.core$_SLASH_@6ffaf3c6"] #object [clojure.core$_PLUS_ 0x35b52c2b "clojure.core$_PLUS_@35b52c2b"] 1 2 3)
(let [a 23] (doseq [b (range 24)] (print (min (rem (+ 24 (- a b)) 24) (rem (+ 24 (- b a)) 24)) " ")))
nil"1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 0 "(let [coll '(1 3 2 5 4 7)] (last (keep-indexed (fn [i [a b]] (when (< a b) (inc i))) (partition 2 1 coll))))
5(let [start {"name" "databases", "columns" ["name"], "values" [["testdb"] ["mydb"]]}] (map (fn [vcoll] (map (fn [a b] {(keyword a) b}) (start "columns") vcoll)) (start "values")))
(({:name "testdb"}) ({:name "mydb"}))

