(take 10 (let [p (promise)] @(deliver p (lazy-cat [1 1] (map + @p (rest @p))))))
(1 1 2 3 5 8 13 21 34 55)
(let [[a & [b c] :as all :or {b 2, c 3}] [1]] [a b c])
[1 nil nil]
(defmacro m [sets] (let [syms (repeatedly (count sets) gensym)] `(for [~@(interleave syms sets)] [~@syms])))
#'user/m
(let [x (atom 1) _ (do (swap! x inc) (swap! x * 2) 123)] @x)
4(let [name [1 1 3]] (if (= (first name) (first (rest name))) (rest name) "something here"))
(1 3)
(loop [x 3] (let [y (* x x)] (if (> y 20) y (recur (inc x)))))
25(defn pivot-with [pred seq] (let [seq (drop-while pred seq)] (take-nth 2 (partition-by (comp boolean pred) seq))))
#'user/pivot-with
(defmacro mac [external f exp] `(let [~f (fn [n#] (max 1 (- n# ~external)))] ~exp))
#'user/mac
(let [a (atom [])] (clojure.walk/postwalk #(swap! a conj %) '(+ a (- b c))) @a)
[+ a - b c ([+ a -] [+ a - b] [+ a - b c]) ([+] [+ a] [+ a - b c ([+ a -] [+ a - b] [+ a - b c])])]
(let [distinct?* (comp (partial (fnil apply :_ [:_]) distinct?) seq)] (map distinct?* [[] [1 2] [2 2]]))
(true true false)
(let [op (first '(+ 1 2)) rst (rest '(+ 1 2))] (apply op rst))
2(defmacro do-in-ns [next-ns & stuff] `(let [old-ns *ns*] (do (in-ns next-ns) (eval ~stuff) (in-ns old-ns))))
#'user/do-in-ns
(let [config {:use-https true, :https-port 443}] (cond-> #{[:a :b :c]} (:use-https config) (conj (:https-port config))))
#{443 [:a :b :c]}
(let [v [1 2 3 4]] (apply conj (subvec v 0 1) 8 (subvec v 1)))
[1 8 2 3 4]
((fn [a b & c] (let [opts (apply hash-map c)] (:key opts))) :foo :bar :key "val")
"val"(let [a [1 2] b [3 4] c [5 6]] ((juxt concat interleave) a b c))
[(1 2 3 4 5 6) (1 3 5 2 4 6)]
(let [x 10 x (/ x 5) x (replicate x x) x (map inc x)] x)
(3 3)
(let [s (apply str (repeatedly 5000 #(char (rand-int 65000))))] [(count s) (count (.getBytes s "LATIN1"))])
[5000 4999]
(let [idx (partition 3 (range 9))] idx (map list idx) [[0 4 8] [2 4 6]])
[[0 4 8] [2 4 6]]
(let [vals [1 2 3 3 5 4]] (map #(< %1 %2) vals (rest vals)))
(true true false true false)
(let [v [:a :b :c]] (for [x v y v :when (not= x y)] [x y]))
([:a :b] [:a :c] [:b :a] [:b :c] [:c :a] [:c :b])
(defmacro with-file [f & body] `(let [x# …] (try ~@body (finally (doto x# .close .delete)))))
#'user/with-file
(let [lst (range 10) f #(* % -1)] (apply hash-map (interleave lst (map f lst))))
{0 0, 1 -1, 2 -2, 3 -3, 4 -4, 5 -5, 6 -6, 7 -7, 8 -8, 9 -9}
(let [s "foo and bar"] (str (subs s 0 4) "X" (subs s 5 (count s))))
"foo Xnd bar"(let [foo (fn [first & rest] [first rest])] (apply foo :name '([:a :b] [:c :d])))
[:name ([:a :b] [:c :d])]
(let [o (to-array [1 2 3]) v (vec o)] [(v 1) (do (aset o 1 42) (v 1))])
[2 42]
(let [v [:a :b :c :d]] (interleave (map name (take-nth 2 v)) (take-nth 2 (next v))))
("a" :b "c" :d)
(let [v [:a :b :c :d :e]] (into (subvec v 0 2) (subvec v 3 5)))
[:a :b :d :e]
(let [m {:a {:aa 1, :ab 1}, :b 2}] (assoc (:a m) :c {:b (:b m)}))
{:aa 1, :ab 1, :c {:b 2}}
(let [x true y [4 5 6]] `(1 2 3 ~@(if x y []) 5))
(1 2 3 4 5 6 5)
(let [adam (atom [])] (do (print adam) (print @adam) (swap! adam conj :eve) (print adam) (print @adam)))
nil"#object[clojure.lang.Atom 0x44179b1f {:status :ready, :val []}][]#object[clojure.lang.Atom 0x44179b1f {:status :ready, :val [:eve]}][:eve]"(let [[head & rest] [:ul "A" "B" "C"]] (into [head] (map #(vector :li %) rest)))
[:ul [:li "A"] [:li "B"] [:li "C"]]
(let [h (apply hash-map [:a 1 :b 2 :c 3 :d 4])] [(keys h) (vals h)])
[(:c :b :d :a) (3 2 4 1)]
(take 5 (let [v [5 0 2 4 1 3]] (iterate #(mapv % %) v)))
([5 0 2 4 1 3] [3 5 2 1 0 4] [1 4 2 5 3 0] [4 3 2 0 5 1] [5 0 2 4 1 3])
(let [f #(repeat 3 %)] (reduce #(assoc %1 %2 (f %2)) {} (range 1 6)))
{1 (1 1 1), 2 (2 2 2), 3 (3 3 3), 4 (4 4 4), 5 (5 5 5)}
(for [val '(1 2 3 4 5)] (let [big-enough? #(> %1 val)] (big-enough? 3)))
(true true false false false)
(let [thing {:tag :TestElement, :attrs {:name "pat", :value "ok"}, :content nil}] {(:tag thing) (:attrs thing)})
{:TestElement {:name "pat", :value "ok"}}
(let [f '[+ - *]] (for [y f] {(:name (meta (resolve y))) (y 1 2)}))
({+ 2} {- 2} {* 2})
(let [fns [inc #(mod % 100) #(* % %)]] ((apply comp (reverse fns)) 104))
25(let [m {:a 1, :b 2}] (into (empty m) (remove (fn [[_ v]] (odd? v)) m)))
{:b 2}
(let [a (make-array Double/TYPE 5 5)] (aset a 2 4 0.9) (println (aget a 2 4)) (let [b (aclone a)] (aset b 2 0 -9.3) (println (aget b 2 0)) (println (aget a 2 0))))
nil"0.9\n-9.3\n-9.3\n"(defn split [e col] (let [[x y] (split-with #(not= e %) col)] (concat x (rest y))))
#'user/split
(let [v '[a b c d e f]] (into (subvec v 0 2) (subvec v 3)))
[a b d e f]
(let [xs [1 2 3 4 5]] (map vector (range) xs (iterate pop xs) (iterate rest xs)))
([0 1 [1 2 3 4 5] [1 2 3 4 5]] [1 2 [1 2 3 4] (2 3 4 5)] [2 3 [1 2 3] (3 4 5)] [3 4 [1 2] (4 5)] [4 5 [1] (5)])
(let [iterative (fn [x] (loop [y x] (if (= y 10000) :done (recur (inc y)))))] (iterative 0))
:done(let [inputs '[[a b c] [d e f]]] (for [xs inputs x xs] (keyword x)))
(:a :b :c :d :e :f)
(let [m {:a 1, :b 2, :c 44} [ks vs] ((juxt keys vals) m)] [vs ks])
[(1 2 44) (:a :b :c)]
(reduce (fn [sum x] (let [sum (+ x sum)] (if (pos? sum) (reduced sum) sum))) -123 (range))
13(let [f (fn [coll] (conj! coll 3))] (println (f (transient [1 2]))) (println (f (transient [1 2]))))
nil"#object[clojure.lang.PersistentVector$TransientVector 0x3c28f776 clojure.lang.PersistentVector$TransientVector@3c28f776]\n#object[clojure.lang.PersistentVector$TransientVector 0x32ef1e83 clojure.lang.PersistentVector$TransientVector@32ef1e83]\n"(let [c [:foo :baz :bar :foo] comp {:baz 0, :bar 1, :foo 2}] (sort-by comp c))
(:baz :bar :foo :foo)

