(defn real-ns [s] (let [sn (symbol (namespace s))] (ns-name (or (find-ns sn) (get (ns-aliases *ns*) sn)))))
#'user/real-ns
(take 20 (let [fib (promise)] (deliver fib (lazy-cat [1 1] (map + @fib (next @fib)))) @fib))
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
(let [x 5] (condp < x 1 "< 1" 5 "< 5" 6 "< 6" "other"))
"< 1"(let [v [1 2 3 4] n 3] (concat (subvec v n) (subvec v 0 n)))
(4 1 2 3)
(let [{x :x, y :y, {z :z} :y} {:x 10, :y {:z 20}}] [x y z])
[10 {:z 20} 20]
(let [li (lazy-cat [1 2 3 (lazy-seq (prn "hi"))])] (first (filter #(= 3 %) li)))
3"\"hi\"\n"(defn fix [f x] (let [y (f x)] (if (= x y) x (recur f y))))
#'user/fix
(let [coll [1 1 3] x (first coll)] (remove (fn [element] (= element x)) coll))
(3)
(let [map-from-fn (fn [f ks] (into {} (map (juxt identity f) ks)))] (map-from-fn inc [7 9 12]))
{7 8, 9 10, 12 13}
(let [o (to-array [1 2 3]) v (vec o)] [(seq v) (do (aset o 0 42) (seq v))])
[(42 2 3) (42 2 3)]
(let [inner {:a 10, :b 20} outer {:a :a, :b :b, :c :a}] (inner (outer :c)))
10(let [x 10 x-times-2 (* 2 x) y 5 y+x (+ y x)] (* 10 y+x))
150(let [{:syms [foo], :keys [baz], :strs [bar]} {'foo 1, "bar" 2, :baz 3}] [foo bar baz])
[1 2 3]
(defn char-seq [reader] (lazy-seq (let [x (.read reader)] (when-not (neg? x) (cons (char x) (char-seq reader))))))
#'user/char-seq
(let [m [{:id 1} {:id 2} {:id 3}]] (map assoc m (repeat :newkey) [4 5 6]))
({:id 1, :newkey 4} {:id 2, :newkey 5} {:id 3, :newkey 6})
(macroexpand-1 '(let [[w1 w2 w3 :as words] (clojure.string/split "hello how are you doing?" #"\s+")] words))
(let [[w1 w2 w3 :as words] (clojure.string/split "hello how are you doing?" #"\s+")] words)
(let [latin1 (apply str (map char (range 32 255)))] (apply str (re-seq #"\p{Lu}" latin1)))
"ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ"(defn foo [& args] (let [{:keys [a b c]} (apply hash-map args)] (list a b c)))
#'user/foo
(let [m {"x" 1, "y" 2} x (get m "x") y (get m "y")] [x y])
[1 2]
(let [my-list '("one" "two" "three" "four")] (map #(nth my-list %) (range 0 4 2)))
("one" "three")
(let [input {:a 1, :b 2, :c 3, :d 4}] (into {} (filter (comp odd? second) input)))
{:a 1, :c 3}
(let [s [:a 1 :a 2 :a 3]] (zipmap (take-nth 2 s) (take-nth 2 (next s))))
{:a 3}
(binding [*print-meta* true] (pr-str (macroexpand '(let [{:keys [^String a ^Integer b]} c ^Float d 1.0]))))
"^{:line 1, :column 52} (let [{:keys [^String a ^Integer b]} c ^Float d 1.0])"(reduce #(let [[k v] %2] (assoc %1 k (* 2 v))) {} {:a 1, :b 2})
{:a 2, :b 4}
(defn toggler-factory [f1 f2] (let [a (atom true)] (fn [] (if @a (f1) (f2)) (swap! a not))))
#'user/toggler-factory
(let [f even? s [1 2 3 4 5]] [(filter f s) (filter (complement f) s)])
[(2 4) (1 3 5)]
(let [state (atom {:plots [[1 2] [1 5]]})] (do (update-in @state [:plots 0 1] inc) @state))
{:plots [[1 2] [1 5]]}
(let [foo {:a 1, :b 1, :c nil}] (into {} (filter #(not (nil? (val %))) foo)))
{:a 1, :b 1}
(let [[odd [e1 & _]] (split-with odd? [1 3 5 6 7 8 10])] [odd e1])
[(1 3 5) 6]
(let [prev [0 1 2 3 4]] ((fn last-two [v] [(peek v) (peek (pop v))]) prev))
[4 3]
(let [args [:foo "/foo" :bar "/bar"]] `(do ~(map (partial apply str) (partition 2 args))))
(do (":foo/foo" ":bar/bar"))
(let [m {:parent '({:child :foo} {:child :bar} {:child :baz})}] (update-in m [:parent] (partial map :child)))
{:parent (:foo :bar :baz)}
(defmacro tm [code] (let [t (gensym)] `(try ~code (catch Throwable ~t (println "doing logging here")))))
#'user/tm
(let [! (fn ! [num] (if-not (pos? num) 1 (* num (! (dec num)))))] (! 10))
3628800(let [matcher (re-matcher #"foo (\S+)" "foo bar foo baz")] (take-while identity (repeatedly #(re-find matcher))))
(["foo bar" "bar"] ["foo baz" "baz"])
(let [x 2 y 3 x (+ x y) y (+ x y)] [x y])
[5 8]
(let [items '[a b c d e f g h]] (map list items (rest items)))
((a b) (b c) (c d) (d e) (e f) (f g) (g h))
(macroexpand-1 '(let [nested-map {:foo {:bar 12}} func (fn [{{bar :bar} :foo}] bar)] (func nested-map)))
(let [nested-map {:foo {:bar 12}} func (fn [{{bar :bar} :foo}] bar)] (func nested-map))
(let [xs (range 52) shuffled-xs (shuffle xs)] (assert (or (not= xs shuffled-xs) (= xs shuffled-xs))))
nil(let [[tag attrs & content] [:a {:href "foo"} "text"]] {:tag tag, :attrs attrs, :content (vec content)})
{:attrs {:href "foo"}, :content ["text"], :tag :a}
(let [m {:a 1, :b 2}] (zipmap (map #(str % "_text") (keys m)) (vals m)))
{":a_text" 1, ":b_text" 2}
(let [x (to-array [42]) xv (vec x)] [(xv 0) (do (aset x 0 33333) (xv 0))])
[42 33333]
(let [lzs (map #(do (println "side effect") (+ % 1)) [1 2 3])] (first lzs))
2"side effect\nside effect\nside effect\n"(let [[a b [c d e]] [1 2 [3 4 5]]] [a b c d e])
[1 2 3 4 5]
(let [coll #{nil 1 false 2}] (= coll (mapcat #(% identity coll) [filter remove])))
false(let [foo (apply interleave [[[10] [20]] [[11] [21]] [[12] [22]]])] (split-at (/ (count foo) 2) foo))
[([10] [11] [12]) ([20] [21] [22])]
(let [v [11 22 33 44] indices [1 3]] (mapv #(get v (dec %)) indices))
[11 33]
(defn shift [n coll] (let [l (count coll)] (take l (drop (mod n l) (cycle coll)))))
#'user/shift
(defmacro fnn [f n] (let [syms (repeatedly n gensym)] `(fn [~@syms & _#] (~f ~@syms))))
#'user/fnn
(let [[x [a & os]] (list 10 (range 3))] (* x (+ a (apply / os))))
5N

