(defmacro some-let [bindings body] (let [[x y & rest] bindings] `(if-let [~x ~y] ~(if (empty? rest) body `(some-let ~(vec rest) ~body)))))
#'user/some-let
(let [coll (range 5) pred #(= 2 %) [t d] (split-with (complement pred) coll) other (concat t (rest d)) match (first d)] [match other])
[2 (0 1 3 4)]
(let [as [0 1 2 3] bs [9 8 7 6]] (doall (map #(println (format "a %d and b %d" %1 %2)) as bs)))
(nil nil nil nil)
"a 0 and b 9\na 1 and b 8\na 2 and b 7\na 3 and b 6\n"(let [fs {:foo +, :bar -, :zonk reduce} params {:foo [1 2], :bar [10 5], :zonk [conj #{} [1 2 3]]}] (merge-with apply fs params))
{:bar 5, :foo 3, :zonk #{1 2 3}}
(let [p 5 in (shuffle (range 10))] (reduce (fn [[l g] i] (if (< i p) [(conj l i) g] [l (conj g i)])) [[] []] in))
[[2 0 4 1 3] [9 7 6 8 5]]
(let [testar (fn [x y] (if (= (reduce + (filter odd? (range 0 x))) y) (str y " is an square perfect")))] (testar 10 25))
"25 is an square perfect"(let [[foo bar _ _ & baz :as orig] [1 2 3 4 5 6 7 8 9 10]] {:new [foo bar baz], :orig orig})
{:new [1 2 (5 6 7 8 9 10)], :orig [1 2 3 4 5 6 7 8 9 10]}
(let [v [1 5 3 3 2 4 5 5 4 5 1 2 1 2 3 5]] (map (juxt first count) (partition-by identity v)))
([1 1] [5 1] [3 2] [2 1] [4 1] [5 2] [4 1] [5 1] [1 1] [2 1] [1 1] [2 1] [3 1] [5 1])
(let [m1 {:foo 1, :bar 2, :baz 3} m2 {1 4, 2 5}] (reduce-kv (fn [acc k v] (assoc acc k (m2 v))) {} m1))
{:bar 5, :baz nil, :foo 4}
(loop [n 1034 result '()] (let [q (int (/ n 10)) r (rem n 10)] (if (= r 0) result (recur q (cons r result)))))
(3 4)
(let [m {:a 565, :b 878, :c 565, :d 997}] (reduce (fn [m [k v]] (if (not= v 565) m (dissoc m k))) m m))
{:b 878, :d 997}
(defn partition-with [p coll] (let [seps (concat (map p coll (rest coll)) [true])] (->> (map list seps coll) (partition-by second) (map #(map second %)))))
#'user/partition-with
(let [roll (fn [g p] (conj g p)) roll-many (fn [g n p] (nth (iterate #(roll % p) g) n))] (roll-many [] 20 0))
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
(let [m {:a {:x 3, :y 4}, :b {:x 2, :y 8}}] (apply merge-with into (for [[outer inner] m [k v] inner] {k {outer v}})))
{:x {:a 3, :b 2}, :y {:a 4, :b 8}}
(loop [n 1234 result '()] (let [q (int (/ n 10)) r (rem n 10)] (if (= r 0) result (recur q (cons r result)))))
(1 2 3 4)
(reduce (fn [[score mx] e] (let [s2 (count e)] (if (> s2 score) [s2 e] [score mx]))) [0 nil] ["hello" "this" "is" "sort by count"])
[13 "sort by count"]
(let [sorted-m (sorted-map :0 10.11 :1 9.10 :2 8.9 :3 7.8 :4 6.7 :5 5.6 :6 4.5 :7 3.3 :8 2.1 :9 1.0)] (vals sorted-m))
(10.11 9.1 8.9 7.8 6.7 5.6 4.5 3.3 2.1 1.0)
(let [src (to-array '(3 3 4 3))] (seq (amap src i dest (+ (aget src i) (if (pos? i) (aget dest (dec i)) 0)))))
(3 6 10 13)
(let [ls [[1 2 3 4] [0 0 1 2] [-1 -1 -1]]] (and (apply = (map count ls)) (every? true? (apply map > ls))))
false(let [amap (sorted-map 1 \b 2 \c 3 \d 4 \e 5 \f 6 \g 7 \h 8 \i 9 \j)] (subseq amap > 5))
([6 \g] [7 \h] [8 \i] [9 \j])
(defmacro unrolled-doseq [[name elems] & body] (let [names (repeatedly (count elems) gensym)] (list* 'let [(vec names) elems] (for [name' names] (list* 'let [name name'] body)))))
#'user/unrolled-doseq
(let [a (make-array Double/TYPE 5 5) b (aclone a)] (aset b 2 0 -9.3) (println (aget b 2 0)) (println (aget a 2 0)))
nil"-9.3\n-9.3\n"(let [args ["alice" "bob"] func (fn [args] (doall (map (fn [x] (prn "func1" x)) args)) (doall (map (fn [x] (prn "func2" x)) args)))] (func args))
(nil nil)
"\"func1\" \"alice\"\n\"func1\" \"bob\"\n\"func2\" \"alice\"\n\"func2\" \"bob\"\n"((fn [coll n x] (let [cnt (- n (count coll))] (if (pos? cnt) (concat coll (take cnt (repeat x))) coll))) [1 2 3] 5 nil)
(1 2 3 nil nil)
(let [l [{:key "a", :val "a"} {:key "b", :val "b"} {:key "c", :val "c"}]] (for [a l b l :when (not= a b)] [a b]))
([{:key "a", :val "a"} {:key "b", :val "b"}] [{:key "a", :val "a"} {:key "c", :val "c"}] [{:key "b", :val "b"} {:key "a", :val "a"}] [{:key "b", :val "b"} {:key "c", :val "c"}] [{:key "c", :val "c"} {:key "a", :val "a"}] [{:key "c", :val "c"} {:key "b", :val "b"}])
(let [table {:plane (fn [action target] (prn "the plane" action target))} [fname & args] (.split "plane flies-to paris" " +")] (apply (table (keyword fname)) args))
nil"\"the plane\" \"flies-to\" \"paris\"\n"(loop [remaining {:a 1, :b 2, :c 3} res {}] (let [key (first (keys remaining))] (if key (recur (dissoc remaining key) (assoc res key "whatever")) res)))
{:a "whatever", :b "whatever", :c "whatever"}
(let [v [{:name "Chris", :age 10, :id 0} {:name "Chris", :age 28, :id 1} {:name "Adam", :age 25, :id 2}] m (group-by :name v)] m)
{"Adam" [{:age 25, :id 2, :name "Adam"}], "Chris" [{:age 10, :id 0, :name "Chris"} {:age 28, :id 1, :name "Chris"}]}
(let [testar (fn [x y] (cond (= (reduce + (filter odd? (range 0 x))) y) (str y " is an square perfect")))] (testar 11 25))
"25 is an square perfect"(let [coll [1 2 3] pred #(= (mod % 2) 0) xs (filter pred coll)] (if (and (seq xs) (not (next xs))) (first xs)))
2(let [m {:a 2, :b "test"} f #(str % "_2")] (into m (map (fn [[k v]] [(keyword (str (name k) "_x")) (f v)]) m)))
{:a 2, :a_x "2_2", :b "test", :b_x "test_2"}
((fn expand-key [m k] (let [v (m k)] (into (dissoc m k) (map vector k v)))) {[:a :b :c] [1 2 3]} [:a :b :c])
{:a 1, :b 2, :c 3}
(let [coll [1 2 3] pred #(= (mod % 2) 1) xs (filter pred coll)] (if (and (seq xs) (not (next xs))) (first xs)))
nil(let [start {"name" "databases", "columns" ["name"], "values" [["testdb"] ["mydb"]]}] (mapcat (fn [vcoll] (map (fn [a b] {(keyword a) b}) (start "columns") vcoll)) (start "values")))
({:name "testdb"} {:name "mydb"})
(let [seconds 72010 hours (mod seconds 3600) minutes (mod (- seconds (* hours 3600)) 60)] (- seconds (+ (* hours 3600) (* minutes 60))))
35410(let [m {"a" {:value "b"}, "foo" {:value "bar"}}] (reduce-kv (fn [s k v] (str s ", " k "=" (apply str (vals v)))) "" m))
", a=b, foo=bar"(let [sec (/ 134000 1000) min (int (/ sec 60)) rem (- sec (* min 60))] (str min " minutes and " rem " seconds"))
"2 minutes and 14 seconds"(reduce #(let [[d m y] %2] (assoc %1 m (conj (%1 m []) [d m y]))) {} [[30 9 2010] [30 10 2010] [31 10 2010]])
{9 [[30 9 2010]], 10 [[30 10 2010] [31 10 2010]]}
(let [as [0 1 2 3] bs [9 8 7 6]] (doseq [a as b bs] (println (format "a %d and b %d" a b))))
nil"a 0 and b 9\na 0 and b 8\na 0 and b 7\na 0 and b 6\na 1 and b 9\na 1 and b 8\na 1 and b 7\na 1 and b 6\na 2 and b 9\na 2 and b 8\na 2 and b 7\na 2 and b 6\na 3 and b 9\na 3 and b 8\na 3 and b 7\na 3 and b 6\n"(let [a {1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7, 9 9}] [(class (conj a [8 8])) (class a)])
[clojure.lang.PersistentHashMap clojure.lang.PersistentArrayMap]
(let [f (fn ([a] {:pre [(integer? a)]} (+ a 10)) ([a b] {:pre [(integer? a) (integer? b)]} (+ a b)))] [(f 1 2) (f 1)])
[3 11]
(let [a {:n (- (+ Integer/MAX_VALUE 1) 1)} b {:n Integer/MAX_VALUE}] (when (= a b) (when (= (hash a) (hash b)) (= {a :z} {b :z}))))
true(defn ba [s [f & r]] (if f (let [y (gensym)] `(-> ~(peek s) ~f (as-> ~y ~(ba (conj s y) r)))) s))
#'user/ba
(let [grid [[1 2 3] [4 5 6] [7 8 9]]] (doseq [x (range (count (first grid))) y (range (count grid))] (print (get-in grid [y x]))))
nil"147258369"(let [grid [[1 2 3] [4 5 6] [7 8 9]]] (doseq [y (range (count grid)) x (range (count (first grid)))] (print (get-in grid [y x]))))
nil"123456789"(let [limit 7] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((6 5 4 3 2 1 0) (5 6 4 3 2 1 0) (5 4 6 3 2 1 0) (5 4 3 6 2 1 0) (5 4 3 2 6 1 0) (5 4 3 2 1 6 0) (5 4 3 2 1 0 6) (6 4 5 3 2 1 0) (4 6 5 3 2 1 0) (4 5 6 3 2 1 0) (4 5 3 6 2 1 0) (4 5 3 2 6 1 0) (4 5 3 2 1 6 0) (4 5 3 2 1 0 6) (6 4 3 5 2 1 0) (4 6 3 5 2 1 0) (4 3 6 5 2 1 0) (4 3 5 6 2 1 0) (4 3 5 2 6 1 0) (4 3 5 2 1 6 0) (4 3 5 2 1 0 6) (6 4 3 2 5 1 0) (4 6 3 2 5 1 0) (4 3 6 2 5 1 0) (4 3 2 6 5 1 0)...
(let [m {:a {:b 2, :c 3}, :b {:d 4, :e 5}, :c {:f 6}}] (reduce (fn [m k] (update-in m [k] keys)) m (keys m)))
{:a (:b :c), :b (:d :e), :c (:f)}
(let [v [1 5 3 3 2 4 5 5 4 5 1 2 1 2 3 5]] (map (juxt first count) (vals (group-by identity v))))
([1 3] [5 5] [3 3] [2 3] [4 2])
(let [limit 6] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((5 4 3 2 1 0) (4 5 3 2 1 0) (4 3 5 2 1 0) (4 3 2 5 1 0) (4 3 2 1 5 0) (4 3 2 1 0 5) (5 3 4 2 1 0) (3 5 4 2 1 0) (3 4 5 2 1 0) (3 4 2 5 1 0) (3 4 2 1 5 0) (3 4 2 1 0 5) (5 3 2 4 1 0) (3 5 2 4 1 0) (3 2 5 4 1 0) (3 2 4 5 1 0) (3 2 4 1 5 0) (3 2 4 1 0 5) (5 3 2 1 4 0) (3 5 2 1 4 0) (3 2 5 1 4 0) (3 2 1 5 4 0) (3 2 1 4 5 0) (3 2 1 4 0 5) (5 3 2 1 0 4) (3 5 2 1 0 4) (3 2 5 1 0 4) (3 2 1 5 0 4) (3 2 1 ...
(let [S [1 2 3] _ #(% (+)) __ #(% (*)) ___ #(% (+ (*) (*)))] #{(_ S) (__ S) (___ S)})
#{1 2 3}

