(->> [1 2 3] (mapcat #(list x %)) (apply +) with-out-str (defn totes []) (let [x 15]))
#'user/totes
(let [x '(a b c)] (vector `(1 2 ~x 3) `(1 2 ~@x 3)))
[(1 2 (a b c) 3) (1 2 a b c 3)]
(let [make-seq (fn make-seq [start-value] (lazy-seq (cons start-value (make-seq (inc start-value))))) a (make-seq 1)] (take 4 a))
(1 2 3 4)
(let [capacity 8] (reduce #(if (< (reduce + %1) capacity) (conj %1 %2) %1) [] (range 20)))
[0 1 2 3 4]
(let [a 23] (doseq [b (range 24)] (print (mod (min (- a b) (- b a)) 24))))
nil"12345678910111213141516171819202122230"(let [x (lazy-cat (do (println 1) [1]) (do (println 2) [2]) (do (println 3) [3]))] (first x))
1"1\n"(let [{a :a, b :b} {:a 5, :b 6} [x y] (range 2)] [a b x y])
[5 6 0 1]
(let [m {:foo "foo", :bar "bar"}] (merge m (apply hash-map (mapcat (fn [[a b]] [b a]) m))))
{:bar "bar", :foo "foo", "bar" :bar, "foo" :foo}
(let (zipmap2 #(map %1 (vector %2 %3))) (zipmap2 identity [1 2 3 4] [:dog :cat :bat]))
([1 2 3 4] [:dog :cat :bat])
(let [[a b c *xs] '(1 2 3 4 5 6 7)] (+ a b c))
6(let [fibos (atom nil)] (reset! fibos (lazy-cat [0 1] (map + @fibos (rest @fibos)))) (take 10 @fibos))
(0 1 1 2 3 5 8 13 21 34)
(let [vals [1 2 3 4 5]] (every? true? (map #(< %1 %2) vals (rest vals))))
true(reduce (fn [sum x] (let [sum (+ x sum)] (if (pos? sum) (reduced sum) sum))) -123 (range))
13(defn magic [elt coll] (let [[xs ys] (split-with #(not= elt %) coll)] (concat xs (rest ys))))
#'user/magic
(let [x [1 2 3 4 5 7]] (clojure.set/difference (set (range 1 (inc (last x)))) (set x)))
#{6}
(let [a "foo" b (apply str (seq "foo"))] [(identical? a b) (identical? a (get #{a} b))])
[false true]
(defn make-query [arg1 arg2] (let [query (atom {:value ""})] (swap! query update-in [:value] str arg1 "::" arg2)))
#'user/make-query
(remove false? (map #(let [x (* % %)] (if (even? x) x false)) (range 1 10)))
(4 16 36 64)
(let [apply-if #(if (%1 %3) (%2 %3) %3)] (map #(apply-if odd? inc %) (range 10)))
(0 2 2 4 4 6 6 8 8 10)
(let [x 1] (-> x inc (as-> y (-> y (+ 10) (as-> z [x y z])))))
[1 2 12]
(let [m {:foo 1, :bar 2}] (map (fn [a b] '(a b)) (keys m) (vals m)))
((a b) (a b))
(let [zipmap2 #(map %1 (vector %2 %3))] (zipmap2 identity [1 2 3 4] [:dog :cat :bat]))
([1 2 3 4] [:dog :cat :bat])
(let [{[f & r] :items} {:hp 100, :mana 10, :items '[sword bread apple hammer]}] [f r])
[sword (bread apple hammer)]
(let [m {:hoo 38, :boy 12, :yeah 384, :okay 49}] (zipmap (shuffle (range (count m))) (vals m)))
{0 12, 1 384, 2 38, 3 49}
(defn byte-seq [input-stream] (lazy-seq (let [value (.read input-stream)] (if (= value -1) nil (cons val (byte-seq input-stream))))))
#'user/byte-seq
(let [x 0] (cond (< x 0) (print "neg") (> x 0) (print "pos") true (print "zero")))
nil"zero"(defmacro defalias [dst src] `(let [v# (var ~src)] (doto (def ~dst @v#) (alter-meta! (constantly (meta v#))))))
#'user/defalias
(let [m {} m {:a 5, :b (:a m)} m {:a 5, :b (:a m)}] m)
{:a 5, :b 5}
(let [in '[a b a b a b a b]] (apply map list (partition 2 in)))
((a a a a) (b b b b))
(let [start "22" pad-char \: desired-length 10] (str (apply str (repeat (- desired-length (count start)) pad-char)) start))
"::::::::22"(let [state (atom {:plots [[1 2] [1 5]]})] (do (swap! state update-in [:plots 0 1] inc) @state))
{:plots [[1 3] [1 5]]}
(defmacro fn-with-bindings [args & body] `(let [bindings# (clojure.lang.Var/getThreadBindings)] (fn ~args (try (clojure.lang.Var/pushThreadBindings bindings#) ~@body (finally (clojure.lang.Var/popThreadBindings))))))
#'user/fn-with-bindings
(let [freqs (frequencies [:a :a :b :b :b :b :c :d :e :f])] (apply max-key val freqs))
[:b 4]
(clojure.string/join " " (let [coll [1 2 3 [10 20] 4]] (mapcat #(vector % %) coll)))
"1 1 2 2 3 3 [10 20] [10 20] 4 4"(let [f str] (reduce-kv (fn [m k v] (assoc m k (f v))) {} {:a 1, :b 2}))
{:a "1", :b "2"}
(let [funcs {:a inc, :b dec} values {:b 5, :a 10}] (merge-with #(%1 %2) funcs values))
{:a 11, :b 4}
(defn apply-regex-rules [rulesMap file] (let [apply-rule? (fn [rule] (re-seq rule file))] (map #(apply-rule? %) (keys rulesMap))))
#'user/apply-regex-rules
(let [s "Leonidas " f (fn [[key re]] (when (re-find re s) key))] (some f {"found!" #"Leonidas\S*"}))
"found!"(let [xs [[:a 1 2 3] [:b 4 5 6]]] (#(zipmap (map first %) %) xs))
{:a [:a 1 2 3], :b [:b 4 5 6]}
(let [[a b c] (re-matcher #".*test.*" "this is a test. testing. only a test.")] [a b c])
[nil nil nil]
(binding [*print-length* 64] (let [a (object-array 64) v (vec a)] (aset a 0 :hi) (prn 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 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]\n"(let [v [3 1 6 4] i (apply max-key v (range (count v)))] [i (v i)])
[2 6]
((fn [arg] (let [[last last-1 last-2] (reverse arg)] (println "last 3: " last-2 last-1 last))) (range 10))
nil"last 3: 7 8 9\n"(let [x (lazy-seq (cons (do (println "ein") 1) (lazy-seq (cons (do (println "twei") 2) nil))))] (first x))
1"ein\n"(let [rep {"name" "World", "greeting" "Hello"}] (clojure.string/replace "<greeting> <name>" #"<(.*?)>" #(get rep (%1 1))))
"Hello World"(let [i 2] (reduce (fn [c p] (if (p i) (inc c) c)) 0 [odd? even? pos?]))
2(let [v [:a :b :c] i 1] (into (subvec v 0 i) (subvec v (inc i))))
[:a :c]
(let [backing (into-array (cons 5 (repeat 20 nil))) v (vec backing)] (aset backing 0 7) v)
[7 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil]
(let [kvs (repeatedly 20 #(rand-int 100))] (println "kvs =" kvs "\nsorted-map =" (seq (apply sorted-map kvs))))
nil"kvs = (0 69 75 59 91 6 28 5 94 24 87 99 91 21 96 51 32 55 49 87) \nsorted-map = ([0 69] [28 5] [32 55] [49 87] [75 59] [87 99] [91 21] [94 24] [96 51])\n"(let [m {:x 1, :y 2}] (reduce (fn [res [k v]] (assoc res k (inc v))) {} m))
{:x 2, :y 3}

