(let [session {:task "first task"}] (assoc session :task (if (vector? (:task session)) (conj (:task session) "second task") (vector (:task session) "second task"))))
{:task ["first task" "second task"]}
(let [m {:a 0, :b 1, :c 2}] (cond-> m (contains? m :a) (assoc :a :foo) (contains? m :b) (assoc :b :bar)))
{:a :foo, :b :bar, :c 2}
(defn looper [x y] (loop [x1 x y1 y] (if (< x1 5) (let [x1 x1] (println x1) (recur (inc x1) y1)))))
#'user/looper
(let [a (atom false) b "foo" d (delay (if @a (/ 0 1) b))] (try @d (finally (swap! a not) d)))
"foo"(defn $wap! [a f & args] (let [a' (atom nil)] (swap! a #(do (reset! a' %) (apply f % args))) @a'))
#'user/$wap!
(let [l '([0 12] [1 23] [3 65]) m (into {} l)] (reduce #(conj %1 (m %2)) [] (range (inc (first (last m))))))
[12 23 nil 65]
(letfn [(pile [n cards] (let [cards (reverse cards)] (->> (range n) (mapcat #(take-nth n (drop % cards))))))] (pile 3 (range 1 11)))
(10 7 4 1 9 6 3 8 5 2)
(defn tree-depth [branch? children root] (let [walk (fn walk [node] (if (branch? node) (inc (reduce max (map walk (children node)))) 0))] (walk root)))
#'user/tree-depth
(let [s (range)] (loop [i 10 s s rv []] (if (> i 0) (recur (dec i) (rest s) (conj rv (first s))) rv)))
[0 1 2 3 4 5 6 7 8 9]
(defmacro makeVect [x y z] `(let [v# (double-array 3)] (aset-double v# 0 ~x) (aset-double v# 1 ~y) (aset-double v# 2 ~z) v#))
#'user/makeVect
(let [[date d m y] (doto (re-matcher #"(\d{1,2})\/(\d{1,2})\/(\d{4})" "12/02/1975") re-find)] [date d m y])
["12/02/1975" "12" "02" "1975"]
(let [m {:apple {:type :foo}, :orange {:type :bar}} [apple-type orange-type] (map #(get-in m %) [[:apple :type] [:orange :type]])] [apple-type orange-type])
[:foo :bar]
(let [data [:a :b :c] indexed-data (map vector data (range))] (for [[e i] indexed-data] [e (map first (remove (comp #{i} second) indexed-data))]))
([:a (:b :c)] [:b (:a :c)] [:c (:a :b)])
(let [div (fn [x y] (if (zero? y) :zero-denom (/ x y)))] (map #(apply div %) [[5 10] [3 2] [4 0]]))
(1/2 3/2 :zero-denom)
(let [nana Double/NaN] [(.equals nana nana) (= nana nana) (== nana nana) (.equals Double/NaN (* -1 Double/NaN)) (= Double/NaN Double/NaN) (== Double/NaN Double/NaN)])
[true true false true false false]
(let [que (repeatedly promise)] (dorun (map (fn [x q] (deliver (first q) x)) (range 10) (iterate next que))) (map deref (take 5 que)))
(0 1 2 3 4)
(let [ops {'+ +, '- -}] (reduce (fn [acc [op x]] ((ops op) acc x)) 0 '((+ 1) (+ 2) (- 3))))
0(apply str (let [v [:a :b :c :d :e]] (reduce into (subvec v 0 2) [[1 2 3 4] (subvec v 3 5)])))
":a:b1234:d:e"(let [testar (fn [x y] (cond (= (reduce + (filter odd? (range 0 x))) y) (str y " is a")))] (testar 10 25))
"25 is a"(let [r (filter #(not (= 0 %)) (range -4 5))] (concat (for [x r] [x -1]) (for [x (reverse r)] [x 1])))
([-4 -1] [-3 -1] [-2 -1] [-1 -1] [1 -1] [2 -1] [3 -1] [4 -1] [4 1] [3 1] [2 1] [1 1] [-1 1] [-2 1] [-3 1] [-4 1])
(let [[date d m y] ((comp re-find re-matcher) #"(\d{1,2})\/(\d{1,2})\/(\d{4})" "12/02/1975")] [date d m y])
["12/02/1975" "12" "02" "1975"]
(let [h {:a 1, :b 2} f (fn [f v] (reduce #(conj %1 [(key %2) (f (val %2))]) {} v))] (f inc h))
{:a 2, :b 3}
(let [m {:one {:a 1, :b 2, :c 3}, :two {:a 10, :b 20, :c 30}}] (for [[k v] m] {k (:c v)}))
({:one 3} {:two 30})
(apply merge (for [[k v] (partition 2 ["a_b_c" 1 "d" 2 "a" :boooo])] (let [ks (map keyword (.split k "_"))] (assoc-in {} ks v))))
{:a :boooo, :d 2}
(let [data '[{:type dog, :attrs a, :val 3} {:type dog, :attrs b, :val 5}]] (:val (first (filter (comp #{'b} :attrs) data))))
5(let [outer identity inner (comp vec reverse) coll {1 2, 8 9, 482 3, 72 4}] (outer (into (empty coll) (map inner coll))))
{2 1, 3 482, 4 72, 9 8}
(let [coll [[:a [1 2]] [:b [3 4]] [:c [5 6]]] ks (mapv first coll) vs (mapcat second coll)] {:keys ks, :values vs})
{:keys [:a :b :c], :values (1 2 3 4 5 6)}
(defn update-stats [args] (let [m (apply hash-map args)] (fn [state] (reduce (fn [s [k vs]] (apply update-in s [k] conj vs)) state m))))
#'user/update-stats
(let [testar (fn [x y] (if (= (reduce + (filter odd? (range 0 x))) y) (str y " is an")))] (testar 10 25))
"25 is an"(defn make-connection-thingy [] (let [connections (atom [])] {:onconnect (fn [x] (swap! connections conj x)), :onmessage (fn [x] (println x)), :current-connections (fn [] (deref connections))}))
#'user/make-connection-thingy
(let [input [[:a 0] [:b 1] [:c 2] [:d 4]] places (take 2 (distinct (repeatedly #(rand-int (count input)))))] (map (vec input) places))
([:b 1] [:d 4])
((fn [& all] (let [xall (reverse (rest (reverse all))) last (first (reverse all))] (map #(eval %) xall) last)) (println 1) 2 3)
3"1\n"(let [db {:next-id 1, :rows {}} id (:next-id db) new-row "fred"] (-> db (assoc :next-id (inc id)) (update-in [:rows] assoc id new-row)))
{:next-id 2, :rows {1 "fred"}}
(let [a {1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7}] [(class (conj a [8 8])) (class a)])
[clojure.lang.PersistentArrayMap clojure.lang.PersistentArrayMap]
(let [nest {:a {:b :makes, :c :takes}, :b {:c :takes}, :c {:a :makes}}] (reduce #(conj %1 [(key %2) (keys (val %2))]) {} nest))
{:a (:b :c), :b (:c), :c (:a)}
(let [args ["alice" "bob"] func (fn [args] (map (fn [x] (prn "func1" x)) args) (map (fn [x] (prn "func2" x)) args))] (func args))
(nil nil)
"\"func2\" \"alice\"\n\"func2\" \"bob\"\n"(let [coll [[:a [1 2]] [:b [3 4]] [:c [5 6]]] ks (map first coll) vs (mapcat second coll)] {:keys ks, :values vs})
{:keys (:a :b :c), :values (1 2 3 4 5 6)}
(let [data '[{:type dog, :attrs a, :val 3} {:type dog, :attrs b, :val 5}]] (:val (first (keep (comp #{'b} :attrs) data))))
nil(loop [[f & r] (range 2 1000)] (let [sieved (remove #(zero? (mod % f)) r)] (if (= sieved r) sieved (recur r))))
(501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600...
(let [matrix (vec (for [y (range 1 4)] (vec (for [x (range 1 4)] (+ (* y 10) x)))))] (get-in matrix [1 1]))
22(defmacro m [a b] (let [da (when a `(def my-a ~a)) db (when b `(def my-b ~b))] `(do ~da ~db)))
#'user/m
(let [book {:title "some book", :authors [{:name "some author"} {:name "other author"}]} new-author {:name "added author"}] (assoc book :authors (conj (:authors book) new-author)))
{:authors [{:name "some author"} {:name "other author"} {:name "added author"}], :title "some book"}
(map #(let [t (mod % 3) z {0 "Fizz"}] ({0 (str (z t) 'Buzz)} (mod % 5) (z t %))) (range 1 101))
(1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz" 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz" 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz" 76 77 "Fizz" 79 "Bu...
(let [ex (atom {:a 10, :b 5, :c 0}) to-remove (map first (take 2 (sort @ex)))] (swap! ex dissoc to-remove) (count (keys @ex)))
3(let [testar (fn [x y] (cond (= (reduce + (filter odd? (range 0 x))) y) (str y " is a")))] (testar 11 25))
"25 is a"(loop [x {:foo {:bar []}} y 0] (let [y (inc y)] (if (< y 10) (recur (update-in x [:foo :bar] conj y) y) x)))
{:foo {:bar [1 2 3 4 5 6 7 8 9]}}
(let [data {:a [34 56 7], :b [21 87 999], :c [3], :d [2 4], :s [4]}] (apply max (map count (vals data))))
3(let [a [[1 12] [2 23] [5 65]] m (into (sorted-map) a)] (map m (-> a last first inc range) (repeat 0)))
(0 12 23 0 0 65)
(let [coll [:N :NE :E :SE :S :SW :W :NW]] (map #(nth coll (mod (- 2 %) (count coll))) (range 6 11)))
(:S :SE :E :NE :N)
(let [m '{:Foo #{:Bar :Baz}, :Flib #{:Bar :Box}}] (apply merge-with into {} (for [[k vs] m v vs] {v #{k}})))
{:Bar #{:Flib :Foo}, :Baz #{:Foo}, :Box #{:Flib}}

