(let [f (memoize (fn foo [x] (prn x) (if (pos? x) (foo (dec x)) x)))] (f 5) (f 5))
0"5\n4\n3\n2\n1\n0\n"(let [{:keys [a b c], :or {a 1, b 2, c 3}} {:a 5, :b 6}] [a b c])
[5 6 3]
(let [v [:a :b :c :d :e]] (take (count v) (partition 3 1 (drop (dec (count v)) (cycle v)))))
((:e :a :b) (:a :b :c) (:b :c :d) (:c :d :e) (:d :e :a))
(let [{:keys [a b c], :or {:a 1, :b 2, :c 3}} {:a 5, :b 6}] [a b c])
[5 6 nil]
(defn base-rotater [f] (fn [piece] (let [s (dec (count piece))] (map (fn [[x y]] (f s x y)) piece))))
#'user/base-rotater
(let [not-found (Object.)] (if (identical? not-found (get {:a nil, :b false, :c 1} :d not-found)) "not found" "found it"))
"not found"(let [a (atom nil)] [(first (filter #(< 1000 %) (map (fn [x] (reset! a x) x) (range)))) @a])
[1001 1001]
(defmacro def-macro-alias [nm1 nm2] (let [gs (gensym)] (list 'defmacro nm1 (vector '& gs) (list concat (list 'quote nm2) gs))))
#'user/def-macro-alias
(let [both (fn [p q & args] [(apply p args) (apply q args)])] (both + - 1 2 3))
[6 -4]
(let [s "abcdefg" i 3 left (subs s 0 i) right (subs s i (count s))] [left right])
["abc" "defg"]
(let [{{b :b, c :c} :a, d :d} {:a {:b 1, :c 2}, :d 3}] (vector b c d))
[1 2 3]
(defn reductions [f init coll] (lazy-seq (let [res (f init (first coll))] (cons res (reductions f res (rest coll))))))
#'user/reductions
(let [m {"a" {:value "b"}}] (reduce-kv (fn [s k v] (str k "=" (apply str (vals v)))) "" m))
"a=b"(take 3 (iterate (fn [m] (let [i (rand-int (count m))] (dissoc m i))) {0 :a, 1 :b, 2 :c}))
({0 :a, 1 :b, 2 :c} {0 :a, 1 :b} {0 :a})
(let [foo []] (second (reduce (fn [[a x] b] [a (and x (= a b))]) [(first foo) true] (rest foo))))
true(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 [a {:a 1, :b 2, :c 3} b {:a 1, :c 3}] (= (select-keys a (keys b)) b))
true(let [[a b & {:keys [c d]}] [1 2 :e :e :d :d :c :c]] [a b c d])
[1 2 :c :d]
(let [m1 {:a :b} m2 (assoc m1 :a :b) m3 (assoc m1 :a :c)] (do (println m1 m2 m3)))
nil"{:a :b} {:a :b} {:a :c}\n"(let [f (fn [n m] (conj (->> n range (take-nth (int (/ n m))) vec) n))] (f 100 3))
[0 33 66 99 100]
(let [m {:foo '[bar baz], :quz '[etc]}] (doseq [[k v] m] (apply println "!!" k v)))
nil"!! :foo bar baz\n!! :quz etc\n"(let [delim "^"] (clojure.string/split "hello^world" (re-pattern (str "[" (->> delim first int (format "%04x") (str "\\u")) "]"))))
["hello" "world"]
(let [v [0 1 2] i 1] (reduce conj (subvec v 0 i) (subvec v (inc i) (count v))))
[0 2]
(let [m {:a 1, :b 2, :c 3}] (into (empty m) (for [[k v] m] [k (* 10 v)])))
{:a 10, :b 20, :c 30}
(let [seqs [[1 5] [2 2 4]] val 1] (map #(drop-while (fn [x] (> x val)) %) seqs))
((1 5) ())
(let [r (partition 2 (range 6))] (println r) (some #(= '(4 5) %) (partition 2 (range 6))))
true"((0 1) (2 3) (4 5))\n"(let [n 13 x 7 p 5] (for [i (range n)] (mod (apply * x (repeat i p)) n)))
(7 9 6 4 7 9 6 4 7 9 6 4 7)
(defmacro time2 [expr] `(let [start# (. System (nanoTime)) ret# ~expr] (/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
#'user/time2
(let [apply-if (fn [pred func val] (if (pred val) (func val) val))] (map (partial apply-if odd? inc) (range 10)))
(0 2 2 4 4 6 6 8 8 10)
(let [n 13 x 5 p 7] (for [i (range n)] (mod (apply * x (repeat i p)) n)))
(5 9 11 12 6 3 8 4 2 1 7 10 5)
(macroexpand '(let [m {:a 1, :b 2, :d 4} {:keys [a b c]} m] [a b c]))
(let [m {:a 1, :b 2, :d 4} {:keys [a b c]} m] [a b c])
(let [x (atom 1) y x] (print "x:" x "y:" y) (swap! y inc) (print "x:" x "y:" y))
nil"x: #object[clojure.lang.Atom 0x6d06f034 {:status :ready, :val 1}] y: #object[clojure.lang.Atom 0x6d06f034 {:status :ready, :val 1}]x: #object[clojure.lang.Atom 0x6d06f034 {:status :ready, :val 2}] y: #object[clojure.lang.Atom 0x6d06f034 {:status :ready, :val 2}]"(let [t1 {"A" {"B" {"C" 10}}} t2 {"A" {"B" {"C" 2}}}] (merge-with (partial merge-with (partial merge-with +)) t1 t2))
{"A" {"B" {"C" 12}}}
(defmacro bla [x fname] (let [f `(fn [args#] (println args#) ~x)] `(defn ~fname [& ~'args] (~f ~'args))))
#'user/bla
(let [f (fn f [] #(+ 2 2)) g (f) h (f)] [g h (= g h)])
[#object [sci.impl.fns$fun$arity_0__26683 0x6039588e "sci.impl.fns$fun$arity_0__26683@6039588e"] #object [sci.impl.fns$fun$arity_0__26683 0x6efeaaf9 "sci.impl.fns$fun$arity_0__26683@6efeaaf9"] false]
(let [rstr #(apply str (flatten %&))] (rstr "begin(" (for [x (range 10)] (str x ",")) ");"))
"begin(0,1,2,3,4,5,6,7,8,9,);"(let [i (atom 0)] {:filter (first (filter #(do (swap! i inc) %) (apply hash-map (range 100)))), :i i})
{:filter [0 1], :i #object [clojure.lang.Atom 0x437ff565 {:status :ready, :val 1}]}
(let [hour-a 23 hour-b 20] (rem (- (+ (if (< hour-a (+ 12 hour-b)) 0 24) hour-b) hour-a) 24))
-3(defn update-first [pred f coll] (let [[x [t & r]] (split-with (complement pred) coll)] (concat x [(f t)] r)))
#'user/update-first
(let [elts [:b :a :d :c :f] s (apply sorted-set-by (fn [x y] 1) elts)] (map s elts))
(nil nil nil nil nil)
(let [apply-if (fn [pred func] (fn [val] (if (pred val) (func val) val)))] (map (apply-if odd? inc) (range 10)))
(0 2 2 4 4 6 6 8 8 10)
(let [a (atom nil)] [(first (filter #(< 31 %) (map (fn [x] (reset! a x) x) (range)))) @a])
[32 32]
(let [num 20 div -6 [a b] ((juxt quot mod) num div)] (= (+ (* a div) b) num))
false(let [coll [:a :b :c :d :e]] (->> coll (iterate pop) (take (count coll)) (reverse) (cons []) (mapv vector coll)))
[[:a []] [:b [:a]] [:c [:a :b]] [:d [:a :b :c]] [:e [:a :b :c :d]]]
(let [{a :a, b :b, :as all} {:a 1, :b 2, :c 3}] {:a a, :b b, :all all})
{:a 1, :all {:a 1, :b 2, :c 3}, :b 2}
(defn case [v & pairs] (let [pairs (partition 2 pairs)] (second (first (filter #(= v (first %)) pairs)))))
#'user/case
(let [s [0 1 2 3 4 5 6 7 8 9]] (concat (take 3 s) (drop 4 s)))
(0 1 2 4 5 6 7 8 9)
(let [a (seq [1 2 3]) b '(1 2 3)] (list (= a b) (seq? a) (seq? b)))
(true true true)
(let [m {1 2, 3 4}] {:assoc (assoc m 10 20), :merge (merge m {5 4, 1 10})})
{:assoc {1 2, 3 4, 10 20}, :merge {1 10, 3 4, 5 4}}
(let [f (fn [coll] (conj! coll 3))] (println (persistent! (f (transient [1 2])))) (println (persistent! (f (transient [1 2])))))
nil"[1 2 3]\n[1 2 3]\n"

