(let [line "foo!bar@baz test" [nick more] (.split line "!" 2) [user more] (.split more "@" 2) [host] (.split more " ")] [nick user host])
["foo" "bar" "baz"]
(let [word "hamstar" dict #{"ham" "star"}] (filter (partial every? dict) (for [i (range (count word))] (mapv (partial apply str) [(take i word) (drop i word)]))))
(["ham" "star"])
(let [m1 {:a 2, :b 3} m2 {:b 3, :d 4} intersected-keys (clojure.set/intersection (set (keys m1)) (set (keys m2)))] (vector (select-keys m1 intersected-keys) (select-keys m2 intersected-keys)))
[{:b 3} {:b 3}]
(reduce (fn [m p] (let [[k v] (first p)] (assoc m k (conj (m k []) v)))) {} [{:A 1} {:A 2} {:A 3} {:C 1} {:C 2}])
{:A [1 2 3], :C [1 2]}
(let [ones (fn ones [] (lazy-seq (print "got one; ") (cons 1 (ones)))) s (ones)] (doseq [i (take 3 s)] (print "a " i)) (take 3 s))
(1 1 1)
"got one; a 1got one; a 1got one; a 1"(let [c [{:step 1, :pos :s1} {:step 2, :pos :s2} {:step 5, :pos :s1}]] (->> (map #(hash-map (:pos %) [(:step %)]) c) (apply merge-with into)))
{:s1 [1 5], :s2 [2]}
(let [a {:a 0, :b 1, :c 2} b {:b 3, :c :4, :d 5, :e 6}] (merge (select-keys a (keys b)) (select-keys b (keys a))))
{:b 3, :c :4}
(defn swap!* [a f & args] (let [ret (atom nil)] (apply swap! a (fn [x & args] (reset! ret x) (apply f x args)) args) @ret))
#'user/swap!*
(let [a1 {:action 'create, 'a1 {:val1 111, :val2 222}} a2 {:action 'create, 'a2 {:val1 111, :val2 222}} a3 {:action 'link, 'a3 {:nodeA a1, :nodeB a2}}] a3)
{:action link, a3 {:nodeA {:action create, a1 {:val1 111, :val2 222}}, :nodeB {:action create, a2 {:val1 111, :val2 222}}}}
(let [m {:a 2, :b "test"} f #(str % "_2")] (merge m (into {} (map (fn [[k v]] [(keyword (str (name k) "_x")) (f v)]) m))))
{:a 2, :a_x "2_2", :b "test", :b_x "test_2"}
(let [empty (Object.)] ((fn [as bs] (map (fn [a b] (if (= b empty) a b)) as (concat bs (repeat empty)))) ["" "A" "B"] [1 2]))
(1 2 "B")
(defn my-subs [st from end] (let [f (if (neg? from) (max (+ (count st) from) 0) from)] (if (<= f end) (subs st f end) "")))
#'user/my-subs
(defn cartes4 [a b & more] (let [so-far (for [x a y b] [x y])] (if more (recur so-far (first more) (next more)) (map flatten so-far))))
#'user/cartes4
(let [divisors [[15 "fizzbuzz"] [3 "fizz"] [5 "buzz"]]] (defn fizzbuzz [n] (-> (first (for [[r s] divisors :when (zero? (mod n r))] s)) (or (str n)))))
#'user/fizzbuzz
(let [f #(println "f o" %) g #(str 'g %) u #(str 'u %) s #(str 's)] ((comp f g u s)))
nil"f o gus\n"(let [[x y z & {:keys [foo bar]}] '(1 2 3 :foo 100 :bar 200)] {:x x, :y y, :z z, :foo foo, :bar bar})
{:bar 200, :foo 100, :x 1, :y 2, :z 3}
(defn factor [n] (let [[p] (->> (range) (drop 2) (filter #(zero? (rem n %))))] (if (= n p) [n] (cons p (factor (/ n p))))))
#'user/factor
(let [decide (fn [xs a b] (if (seq xs) (a xs) b))] [(decide (range 1 6) (partial apply *) 42) (decide '() (partial apply *) 42)])
[120 42]
((fn [[k [low high]]] (let [?k (symbol (str "?" (name k)))] (list ['?foo k ?k] [(list '> ?k low)] [(list '< ?k high)]))) [:bar [10 20]])
([?foo :bar ?bar] [(> ?bar 10)] [(< ?bar 20)])
(let [input {:a 0, :b 1, :c 2, :d 3}] (reduce-kv (fn [m k v] (if (even? v) (assoc m k (inc v)) m)) input input))
{:a 1, :b 1, :c 3, :d 3}
(let [keys [:a :b] m {:a [1 2 3], :b [3 4 5]}] (apply map (fn [& vals] (zipmap keys vals)) (map m keys)))
({:a 1, :b 3} {:a 2, :b 4} {:a 3, :b 5})
(let [vals [{:foo "cool"} {:foo nil} {:foo "blah"}] fill (first (filter :foo vals)) final (map (fn [cur] (update-in cur [:foo] #(or % fill))) vals)] final)
({:foo "cool"} {:foo {:foo "cool"}} {:foo "blah"})
(let [in [1 35 456 2005 3045 3987] by-count (group-by #(quot % 1000) in)] (map #(get by-count % []) (range (inc (apply max (keys by-count))))))
([1 35 456] [] [2005] [3045 3987])
(let [f #(if (= % 7) [:hi 6 6] :hi) g inc h inc] (= ((comp f g h) 5) ((juxt f g h) 5)))
true(defn get-or-die [in key] (let [sentinel (Object.) result (get in key sentinel)] (if (not= result sentinel) result (throw (ex-info "Key not found!" {:in in, :key key})))))
#'user/get-or-die
(let [limit 5] (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit)))
((4 3 2 1 0) (3 4 2 1 0) (3 2 4 1 0) (3 2 1 4 0) (3 2 1 0 4) (4 2 3 1 0) (2 4 3 1 0) (2 3 4 1 0) (2 3 1 4 0) (2 3 1 0 4) (4 2 1 3 0) (2 4 1 3 0) (2 1 4 3 0) (2 1 3 4 0) (2 1 3 0 4) (4 2 1 0 3) (2 4 1 0 3) (2 1 4 0 3) (2 1 0 4 3) (2 1 0 3 4) (4 3 1 2 0) (3 4 1 2 0) (3 1 4 2 0) (3 1 2 4 0) (3 1 2 0 4) (4 1 3 2 0) (1 4 3 2 0) (1 3 4 2 0) (1 3 2 4 0) (1 3 2 0 4) (4 1 2 3 0) (1 4 2 3 0) (1 2 4 3 0) (1 ...
((fn [m] (let [c? (comp coll? second)] (into {} (remove c? (tree-seq c? #(mapcat seq (second %)) [:x [m]]))))) {:one 1, :two [{:four 4, :three 3}]})
{:four 4, :one 1, :three 3}
(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 [x y]))))
nil"123456789"(let [a [+ 3 4] b '(+ 3 4)] (str "vector: " (apply (first a) (rest a)) " list: " (apply (first b) (rest b))))
"vector: 7 list: 4"((fn foomap [fn lst] (if (empty? lst) lst (let [temp (map fn lst)] (cons (first temp) (foomap fn (rest temp)))))) inc [1 1 1 1 1])
(2 3 4 5 6)
(let [{a :a, b :b, c :c, :as m, :or {a [2 3], b [3 4]}} {:a [5 6], :c [6 7]}] [a b c m])
[[5 6] [3 4] [6 7] {:a [5 6], :c [6 7]}]
(let [fs {:foo +, :bar -, :zonk reduce} params {:foo [1 2], :bar [10 5], :zonk [conj #{} [1 2 3]], :nonexistent 5}] (merge-with apply fs params))
{:bar 5, :foo 3, :nonexistent 5, :zonk #{1 2 3}}
(defn replacefirst [pred f coll] (let [head (take-while (complement pred) coll) [item & tail] (drop-while pred coll)] (into (empty coll) (concat head (cons (f item) tail)))))
#'user/replacefirst
(let [characters (->> (range 48 58) (concat (range 65 89)) (concat (range 97 123)))] (defn generate-id [] (->> #(rand-nth characters) repeatedly (take 4) (map char) (apply str))))
#'user/generate-id
(let [v '(1 2 3 1 4)] (first (reduce (fn [[xs l] x] (if (< x l) [xs l] [(conj xs x) x])) [[] (first v)] v)))
[1 2 3 4]
(take 5 (map (fn [i] (let [d (map #(char (+ (int \0) (- (int %) (int \0)))) (str i))] (apply str (concat d (reverse d))))) (range)))
("00" "11" "22" "33" "44")
(let [do2 (fn [f] (fn [[a b & c]] (list* a (f b) c)))] (map (do2 #(str "prefix-" %)) '((123 "foo" 456) (123 "bar" 456))))
((123 "prefix-foo" 456) (123 "prefix-bar" 456))
(let [grid [[1 2 3] [4 5 6] [7 8 9]]] (for [x (range (count (first grid))) y (range (count grid))] (print (nth (nth grid x) y))))
(nil nil nil nil nil nil nil nil nil)
"123456789"(let [data [{:a 1} {:b 2} {:c [3 4 5]} {:a 6}]] (mapcat #(map hash-map (cycle %1) %2) (map keys data) (map (comp flatten vals) data)))
({:a 1} {:b 2} {:c 3} {:c 4} {:c 5} {:a 6})
(let [m {365 [{:a 1, :b 2} {:a 4, :b 6}], 366 [{:a 11, :b 22} {:a 44, :b 66}]}] (for [[id [mps] :as orig] m] orig))
([365 [{:a 1, :b 2} {:a 4, :b 6}]] [366 [{:a 11, :b 22} {:a 44, :b 66}]])
(let [v [1 2 3 1 2 4]] (reduce (fn [a b] (if (< (peek a) b) (conj a b) a)) (-> v (get 0) vector) v))
[1 2 3 4]
(let [vals [{:foo "cool"} {:foo nil} {:foo "blah"}] fill (:foo (first (filter :foo vals))) final (map (fn [cur] (update-in cur [:foo] #(or % fill))) vals)] final)
({:foo "cool"} {:foo "cool"} {:foo "blah"})
(let [grid [[1 2 3] [4 5 6] [7 8 9]]] (doseq [x (range (count (first grid))) y (range (count grid))] (print (nth (nth grid x) y))))
nil"123456789"(let [m {"a" {:value "b"}, "foo" {:value "bar"}}] (reduce-kv (fn [s k v] (str s (when s ", ") k "=" (apply str (vals v)))) nil m))
"a=b, foo=bar"(map (fn [text] (let [match (re-find #"^[^_]*_([^_]+)$" text)] (list text (if match (nth match 1))))) (list "" "hello" "hello_" "hello_world" "hello__world" "hello_world_test"))
(("" nil) ("hello" nil) ("hello_" nil) ("hello_world" "world") ("hello__world" nil) ("hello_world_test" nil))
(let [incw (fn [f] #(inc (f %))) firstw (fn [f] #(first (f %))) stack (-> identity firstw incw)] (stack [0 2 4 6]))
1(let [a {0 0, 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.PersistentHashMap]
(let [data [1 3 3 3 5 5 5 5 5] freqs (frequencies data) total (count data) pcts (for [[v cnt] freqs] (double (/ v total)))] pcts)
(0.1111111111111111 0.3333333333333333 0.5555555555555556)
(defn nils [c] (let [[c1 & cs] c] (if cs (concat (map #(cons c1 %) (nils cs)) (map #(cons nil %) (nils cs))) [[c1] [nil]])))
#'user/nils
(let [incw (fn [f] #(f (inc %))) firstw (fn [f] #(f (first %))) stack (-> identity incw firstw)] (stack [0 2 4 6]))
1

