(defmacro bar [map & body] `(let [{:keys ~'[north east south west north-east north-west south-east south-west]} ~map] ~@body))
#'user/bar
(defmacro method-call [^String method] (let [m (with-meta (gensym "_m") {:tag 'java.util.Date})] `(fn [~m] (.. ~m ~(symbol method)))))
#'user/method-call
(let [v [:a :b :c :d :e]] (partition 3 1 (take (count v) (drop (dec (count v)) (cycle v)))))
((:e :a :b) (:a :b :c) (:b :c :d))
(let [m [{:k1 [{:k2 42}]} {:k1 [{:k2 77}]}]] (map #(update-in % [:k1 0 :k2] (fn [_] :meow)) m))
({:k1 [{:k2 :meow}]} {:k1 [{:k2 :meow}]})
(let [x 10] [(< 0 x 10) (< 0 x 20) (< 10 x 20) (<= 0 x 10)])
[false true false true]
(defmacro to-gl-const [k] (let [gl# ("GL_" (.. (name k) (replace \- \_) (toUpperCase)))] `(. GL ~(symbol gl#))))
#'user/to-gl-const
(let [coll [{:id 1, :name "foo", :title "bar"} {:id 2, :name "test", :title "bleh"}]] (zipmap (map :id coll) coll))
{1 {:id 1, :name "foo", :title "bar"}, 2 {:id 2, :name "test", :title "bleh"}}
(defmacro defn-with-expr [expr args & body] (let [val (symbol (eval expr))] (assert (symbol? val)) `(defn ~val ~args ~@body)))
#'user/defn-with-expr
(let [ms [{:a 1, :b 2} {:c 3}] new-last (merge (last ms) {:d 4})] (conj (pop ms) new-last))
[{:a 1, :b 2} {:c 3, :d 4}]
(let [Length 20 Pad \- Name "TEttingerLivesInCaliforniaSoItIsGettingLate"] (apply str (map #(nth Name %1 %2) (range) (repeat Length Pad))))
"TEttingerLivesInCali"(let [well-behaved? (fn [ob] (= ob (read-string (binding [*print-dup* true] (pr-str ob)))))] (map well-behaved? ['tommy nil false 388 :what]))
(true true true true true)
(let [xs (range 10)] (map (fn [a b] a) xs (take-while #(< % 10) (reductions + 0 xs))))
(0 1 2 3 4)
(defmacro let+ [bindings & body] `(let [~(vec (take-nth 2 bindings)) ~(vec (take-nth 2 (next bindings)))] ~@body))
#'user/let+
(let [m {"a" {:value "b"}}] (reduce (fn [s [k v]] (str k "=" (apply str (vals v)))) "" m))
"a=b"(let [fields (atom [[0 1 2 {:player true}]])] (swap! fields (partial map #(assoc-in % [3 :bandit] false))) @fields)
([0 1 2 {:bandit false, :player true}])
(let [{:keys #{a b c d}} {:a 1, :b 2, :c 3, :d 4}] [a b c d])
[1 2 3 4]
(defn join-ends [coll] (let [fst (first coll) mid (butlast (next coll)) end (last coll)] (cons (+ fst end) mid)))
#'user/join-ends
(defn swap!' [a f] (let [old @a new (f old)] (if (compare-and-set! a old new) new (recur a f))))
#'user/swap!'
(let [{:keys [a b c], :as m} {:a 1, :b 2, :c 3, :d 4}] [a b c m])
[1 2 3 {:a 1, :b 2, :c 3, :d 4}]
(let [a (seq [1 2 3]) b '(1 2 3)] (list (= a b) (seqable? a) (seqable? b)))
(true true true)
(let [{a :a, b :b, c :c} {:a 0, :b 1, :c 3}] [c b a b c a])
[3 1 0 1 3 0]
(defn make-toggler [f1 f2 & args] (let [a (atom false)] (fn [] (apply (if (swap! a not) f1 f2) args))))
#'user/make-toggler
(let [col (for [x (range 50) y (range 1 50)] [x y])] (reduce #(map + %1 %2) col))
(60025 61250)
(let [v '[a b c d e]] (-> (subvec v 0 2) (conj 'X) (into (subvec v 2))))
[a b X c d e]
(let [f (fn [x] {:pre [(sequential? x)]} (if (associative? x) vector list))] (map f [[1 2 3] (range 10)]))
(#object [clojure.core$vector 0x643f0d58 "clojure.core$vector@643f0d58"] #object [clojure.lang.PersistentList$Primordial 0x496348fc "clojure.lang.PersistentList$Primordial@496348fc"])
(let [f (fn [a b & {:keys [c d]}] [:my-args a b c d])] (f 1 2 :d 5))
[:my-args 1 2 nil 5]
(let [[board first-row second-row third-row] (iterate (partial drop 3) [1 2 3 4 5 6 7 8 9])] third-row)
()(let [a (atom 0) action (delay (swap! a inc)) f #(force a)] [(f) (f) (f)])
[#object [clojure.lang.Atom 0x319f4626 {:status :ready, :val 0}] #object [clojure.lang.Atom 0x319f4626 {:status :ready, :val 0}] #object [clojure.lang.Atom 0x319f4626 {:status :ready, :val 0}]]
(let [[a b] (split-with #(not= 3 %) [1 2 3 3 45 3 6])] (concat a (rest b)))
(1 2 3 45 3 6)
(let [t {:tag :TestElement, :attrs {:name "pat", :value "ok"}, :content nil} {:keys [tag attrs]} t] {tag attrs})
{:TestElement {:name "pat", :value "ok"}}
(defn- entity-equals [a b] (let [a (merge (zipmap (keys b) (repeat nil)) (select-keys a (keys b)))] (= a b)))
#'user/entity-equals
(let [a (make-array Integer 2 2) b (aclone a)] (aset a 0 0 (int 2)) (aget b 0 0))
2(defn ab [] (fn [x] (let [a (atom 0)] (if (= x :inc) (swap! a inc) (swap! a dec)) @a)))
#'user/ab
(let [a (atom nil)] [(first (filter #(< 30 %) (map (fn [x] (reset! a x) x) (range)))) @a])
[31 31]
(let [m {:a nil, :b :foo}] (reduce #(dissoc % %2) m (filter #(nil? (m %)) (keys m))))
{:b :foo}
(let [m (sorted-map-by (fn [r s] (compare (str r) (str s))))] (-> m (assoc #"foo" 1) (assoc #"foo" 2)))
{#"foo" 2}
(defn my-cat [x y] (lazy-seq (let [s (seq x)] (if-not s y (cons (first s) (concat (rest s) y))))))
#'user/my-cat
(let [f0 "C:\\long\\fn.ext" f1 (apply str (take (.lastIndexOf f0 ".") f0))] (apply str (drop (inc (.lastIndexOf f1 "\\")) f1)))
"fn"(let [m {:a 1, :b 2}] (apply array-map (map (fn [k v] [(name k) v]) (keys m) (vals m))))
{["a" 1] ["b" 2]}
(let [a {:a 1, :b 0, :c 1}] (filterv #(= (a %) (apply max (vals a))) (keys a)))
[:a :c]
(defn update [m ks f] (let [v (select-keys m ks)] (merge m (zipmap (keys v) (map f (vals v))))))
#'user/update
(let [my-nums [10 20 30 40] a (atom (cons nil my-nums)) read-num #(first (swap! a rest))] [(read-num) (read-num)])
[10 20]
(let [state (atom {}) counter (fn [s] (swap! state update-in [s] (fnil inc 0)))] (mapv counter ["hi" "hi" "bye"]) @state)
{"bye" 1, "hi" 2}
(let [a (seq [1 2 3]) b '(1 2 3)] (list (= a b) (list? a) (list? b)))
(true false true)
(let [bindings "should have an even number of forms" bindings "BETTER HAVE AN EVEN NUMBER OF FORMS" bindings] bindings)
"should have an even number of forms"(let [list [:foo :bar "baz" :quux :yay] [a [b & c]] (split-with (complement string?) list)] [a b c])
[(:foo :bar) "baz" (:quux :yay)]
(let [my-list (fn [name] (printf "Making %s list\n" name) (list 1 name))] (take 1 (lazy-cat (my-list "red") (my-list "blue"))))
(1)
"Making red list\n"(let [c [:foo :baz :bar :foo] comp {:baz 0, :bar 1, :foo 2}] (sort-by #(% comp) c))
(:baz :bar :foo :foo)
(let [=? (fn [& args] (fn [x] ((zipmap args (repeat true)) x)))] (some (=? 1 2 3) (range 10)))
true(let [hour-a 23 hour-b 20] (mod (- (+ (if (< hour-a (+ 12 hour-b)) 12 24) hour-b) hour-a) 24))
9

