(let [f (atom (fn [] (println "Hello")))] (@f) (reset! f (fn [] (println "Goodbye World"))) (@f))
nil"Hello\nGoodbye World\n"(let [space " "] (apply str (concat "I" space "worship" space "His" space "shadow")))
"I worship His shadow"(let [[a b] (re-matches #"(\d+) (\d+)" "12 34")] (prn a) (prn 2))
nil"\"12 34\"\n2\n"(let [[x y :as z 1 2 3 whatever] (range 5)] [x y z])
[0 1 (0 1 2 3 4)]
(defn tree-depth-check-empty [branch? children empty root] (let [walk (fn walk [node] (if (branch? node) (let [children (children node)] (if (empty? children) empty (inc (reduce max (map walk children))))) 0))] (walk root)))
#'user/tree-depth-check-empty
(let [r 7] (reduce (fn [t [i w]] (let [t (+ t w)] (if (< r t) (reduced i) t))) 0 [[0 1] [1 2] [2 3] [3 4] [5 6]]))
3(defmacro condf [expr & clauses] (let [x (gensym "x")] `(let [~x ~expr] (cond ~@(apply concat (for [[p e] (partition-all 2 clauses)] [(if (keyword? p) p `(~p ~x)) e]))))))
#'user/condf
(defmacro if-pred [form cond yes no] `(let [~'% ~form] (if (~cond ~'%) ~yes ~no)))
#'user/if-pred
(defmacro method-call [object method-name & params] (let [method-symbol (symbol method-name)] `(. ~object ~method-symbol ~@params)))
#'user/method-call
(defmacro if-pred [form condition yes no] `(let [result# ~form] (if (~condition result#) ~yes ~no)))
#'user/if-pred
(let [{:keys [a b c], :or {c 0}, :as args} {:a 1, :b 2}] args)
{:a 1, :b 2}
(let [s "a/b/c/d/e"] (map #(subs s 0 (inc %)) (range 0 (count s) 2)))
("a" "a/b" "a/b/c" "a/b/c/d" "a/b/c/d/e")
(let [s [1 2 3]] (map (comp second vector) s (partition 2 1 (cycle s))))
((1 2) (2 3) (3 1))
(let [x 9 y '(- x)] (println `y) (println ``y) (println ``~y) (println ``~~y))
nil"user/y\n(quote user/y)\nuser/y\n(- x)\n"(let [flip (fn [f] (fn [& xs] (apply f (reverse xs))))] ((flip /) 2 4))
2(defn foo [var] (let [m (meta var)] (symbol (name (:name m)) (name (ns-name (:ns m))))))
#'user/foo
(let [m {:a 1, :b 2}] (into m (for [[k v] m] [k (inc v)])))
{:a 2, :b 3}
(let [{a 0, b 5} [:a :b :c :d :e :f :g :h]] [a b])
[:a :f]
(defmacro solve [vars queries] (let [quoted_vars (map #(quote %) vars)] `(l/run* ~quoted_vars ~@queries)))
#'user/solve
(drop 30 (let [a (object-array 64) v (vec a)] (aset a 0 :hi) v))
(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil)
(defn or-comp [args & fns] (let [results (for [fn fns] (fn args))] (some true? results)))
#'user/or-comp
(let [xs [[1 2] [3 4]]] (into {} (for [[k v] xs] [(keyword (str k)) v])))
{:1 2, :3 4}
(map #(str (let [n (int %)] (if (= n %) n %))) [9.0 4.25])
("9.0" "4.25")
(let [& [1 2 3] [& &] & [& &] & [& &] &] &)
(1 2 3)
(let [a (atom [1 2 3])] (swap! a (partial filterv (fn [x] (not= x 2)))))
[1 3]
(let [f (partial apply hash-map :opt1 42 :opt2 "bam")] (map f [[:a 1] [:b 2]]))
({:a 1, :opt1 42, :opt2 "bam"} {:b 2, :opt1 42, :opt2 "bam"})
(let [r (:repeat {:repeat 0} 0)] (if (< r 2) [r] (range 1 (inc r))))
[0]
(let [_ (print "hey, ") _ (print "party ") _ (print "people!") _ 5] _)
5"hey, party people!"(let [f (fn [s p r] (.replaceAll s p r))] (apply f "foo" ["o" "a"]))
"faa"(defmacro nif [expr pos zero neg] `(let [expr# ~expr] (cond (pos? expr#) ~pos ..)))
#'user/nif
(let [a (into-array [1 2 3]) p (seq a) _ (aset a 0 2)] p)
(2 2 3)
(let [c (atom 0) _ (with-meta (map (fn [_] (swap! c inc)) (range 100)) {})] @c)
32(let [a (atom false) l (lazy-seq (swap! a not) nil)] [(first (cons 3 l)) @a])
[3 false]
(let [coll [nil 1 false 2]] (= coll (mapcat #(% identity coll) [remove filter])))
false(let [a (hash-map (Object.) 1)] [(assoc a (Object.) 2) (assoc a (key (first a)) 2)])
[{#object [java.lang.Object 0x13802062 "java.lang.Object@13802062"] 2, #object [java.lang.Object 0x1d47cdba "java.lang.Object@1d47cdba"] 1} {#object [java.lang.Object 0x1d47cdba "java.lang.Object@1d47cdba"] 2}]
(let [f (fn f [& params] (apply hash-map params))] (:b (f :a 3 :b 32)))
32(take 5 (binding [*print-dup* true] (let [q (repeatedly (fn [] *print-dup*))] (doall (take 3 q)) q)))
(true true true false false)
(let [l '([0 12] [1 23] [3 65]) v (repeat (count l) 0)] v)
(0 0 0)
(let [a '(a list) r (atom a)] (swap! r conj 'something) [a @r r])
[(a list) (something a list) #object [clojure.lang.Atom 0x2aa81bde {:status :ready, :val (something a list)}]]
(defmacro t [& body] `(let [b# (System/currentTimeMillis) ret# (do ~@body)] [ret# (- (System/currentTimeMillis) b#)]))
#'user/t
(let [p (promise) s (lazy-seq (cons @p nil))] (deliver p s) (identical? s (first s)))
true(let [keys (concat (range 0 7) (range 16 23))] (into {} (for [key keys] [key 0])))
{0 0, 1 0, 2 0, 3 0, 4 0, 5 0, 6 0, 16 0, 17 0, 18 0, 19 0, 20 0, 21 0, 22 0}
(let [{:keys [a b], :as all} {:a 12, :b 34, :c 56}] [a b all])
[12 34 {:a 12, :b 34, :c 56}]
(let [plus (fn [x] (fn [y] (+ x y)))] (map (plus 3) [4 5 6]))
(7 8 9)
(let [x (atom 0)] (defn counter [] x) (defn tick [] (swap! x #(+ % 1))))
#'user/tick
(let [coll [nil false 0 ""]] (reduce #(when (= %2 :hooray) (reduced %2)) [] coll))
nil(defmacro mm [arg] (let [[p q] arg] `(and (isa? 'x ~p) (isa? y ~q))))
#'user/mm
(let [maxrange 100] (reduce + (distinct (concat (range 3 maxrange 3) (range 5 maxrange 5)))))
2318((fn [& opts] (let [{:keys [a b], :or {a 1, b 2}} opts] [a b])))
[1 2]
(let [m {:foo1 "bar1"} cond (= 1 1)] (cond-> m cond (assoc :foo2 "bar2")))
{:foo1 "bar1", :foo2 "bar2"}

