(defn loop-with-keys ([f e] (loop-with-keys f e [])) ([f e keychain] (f e keychain) (let [tr (cond (map? e) e (coll? e) (map list (range) e) :else nil)] (doseq [[k el] tr] (loop-with-keys f el (conj keychain k))))))
#'user/loop-with-keys
((fn [c] (last (take-while #(not (nil? (peek %))) (iterate (fn [w] (let [s (peek w) n (rand-nth (get c s))] (conj w n))) [(first (rand-nth (seq c)))])))) {"baby" ["hello"], "hello" ["world" "my"], "my" ["baby" "darling"]})
["baby" "hello" "my" "baby" "hello" "my" "darling"]
(let [Y (fn [f] ((fn [x] (f #((x x) %))) (fn [x] (f #((x x) %))))) fact* (fn [f] (fn [n] (if (zero? n) 1 (* n (f (dec n))))))] ((Y fact*) 5))
120
(let [a [[1 12] [2 23] [4 65]] ks (map first a) b (map - ks (cons -1 ks))] (apply concat (interleave (map #(repeat (dec %) 0) b) (map #(list (second %)) a))))
(0 12 23 0 65)
(defn move [[start-x start-y] x y] (let [new-x (+ start-x x) new-y (+ start-y y)] [(cond (> new-x 4) 4 (< new-x 0) 0 :else new-x) (cond (> new-y 4) 4 (< new-y 0) 0 :else new-y)]))
#'user/move
(let [a #(apply str (flatten %)) p partition R rand-nth n #(a (R (mapcat p [1 2 3] %&))) v #(n "aioeu" "iaaiaa")] (a [(n "STKNYPKLGVZ" "GlThShNyFtCh" "ZvrCthShrMycTch") (for [_ " "] [(v) (n "lpstnkgxzv" (concat [(R ["h" "gl" "gr" "nd"]) (v)] "rnthggghtsltrkkhshng") "ghnlok")])]))
"Tchaaghop"
(let [x$ 1 x% 2 x* 3 x# 4 x' 5 x! 6 x& 7 x+ 8 x- 9 x= 10 x| 11 x? 12] [x$ x% x* x# x' x! x& x+ x- x- x| x?])
[1 2 3 4 5 6 7 8 9 9 11 12]
(defn count-num-chars [^String s] (let [len (.length s) space (int 32)] (loop [i (int 0) c (int 0)] (if (< i len) (recur (inc i) (if (== (.codePointAt s i) space) c (inc c))) c))))
#'user/count-num-chars
(let [update-by-key (fn [fs m] (merge-with #(%1 %2) fs m)) fns {:a #(* 2 %), :b #(* 0.5 %), :c #(* 3 %)}] (update-by-key fns {:a 1, :b 2, :c 3}))
{:a 2, :b 1.0, :c 9}
(defn doeach "Visually like map, only for side effects, returns the last item for convenience" [& args] (let [s (apply map args)] (loop [current (first s) remaining (rest s)] (if-not (seq remaining) current (recur (first remaining) (rest remaining))))))
#'user/doeach
(let [ts #(= %& %&) fs #(= `%& %&) nils #({} %&) boolean #((`{~(= + -) ~fs, ~({} +) ~nils} % ts))] (map boolean [nil false true 1 [1 2 3]]))
(nil false true true true)
(let [thing [[0 1] [1 2 3 4 5] [2 3 4] [3 4] [4 5] [5 6] [6 7 8] [7 8] [8 9] [9 10]]] (for [i thing idx (range (count i))] (nth i idx)))
(0 1 1 2 3 4 5 2 3 4 3 4 4 5 5 6 6 7 8 7 8 8 9 9 10)
(let [tst "< one two three" sp (clojure.string/split tst #"\s+")] ((fn [& args] (when (= (count args) (dec (count sp))) (eval (read-string (str "(" (first sp) " " (clojure.string/join " " args) ")"))))) 1 2 3))
true
(reduce (fn [ranges [start end :as el]] (let [[s e] (peek ranges)] (if (> start e) (conj ranges el) (conj (pop ranges) [s end])))) [[1 1]] [[1 3] [3 3] [3 5] [7 10] [9 20] [420 666]])
[[1 5] [7 20] [420 666]]
(let [a #(apply str (flatten %)) r repeatedly p partition N rand-nth n #(a (N (concat (repeat % "") (mapcat p [1 2 3] %&)))) v #(n 0 "aioeu" "iaai") w (fn [] (let [b (n 6 "!." "" "...") s [(n 0 "STKNYPKLG" "GlThShNyFt" "ZvrCth") (r (N [1 2]) #(do [(v) (n 9 (map str "'-" (r 2 v))) (n 0 (concat "lpstnkgx" [(N ["h" "gl" "gr" "nd"]) (v)]) "rlthggghtsltrkkhshng")]))]] [b (if (seq b) [" " s] [(n 3 ",") " " (. (a s) toLowerCase)])]))] (re-find #"[A-Z].+" (a [(r 10 w) "."])))
"Ftagg cthia'eaarl, gialtark. Kel kap."
(let [a #(apply str (flatten %)) r repeatedly p partition N rand-nth n #(a (N (concat (repeat % "") (mapcat p [1 2 3] %&)))) v #(n 0 "aioeu" "iaai") w (fn [] (let [b (n 6 "!." "" "...") s [(n 0 "STKNYPKLG" "GlThShNyFt" "ZvrCth") (r (N [1 2]) #(do [(v) (n 9 (map str "'-" (r 2 v))) (n 0 (concat "lpstnkgx" [(N ["h" "gl" "gr" "nd"]) (v)]) "rlthggghtsltrkkhshng")]))]] [b (if (seq b) [" " s] [(n 3 ",") " " (. (a s) toLowerCase)])]))] (re-find #"[A-Z].+" (a [(r 500 w) "."])))
"Zvro-iathiag, nyia'agg yai'agh! Kikhea git. Thark piaserk pohia'aig... Zvraip, tash kun. Nyi'oo zvrapail! Thiangiath ting, nyiaia ge'ung! Ctholtukh thal. Tep koxak! Tus... Purlaith! Sai'up ship, gandai'aip surliang, pairl... Tokaith ko-aigh cthalt nyo-ilt! Tits. Pakhut. Nai'iash zvreggegg thets, zvrek gleshin. Cthoggigh. Kiakol nyailtarl gairl, cthongia-og, naip shutairl nax, giligg cthelegl! Nyi...
(let [coll [:N :NE :E :SE :S :SW :W :NW] reversed (zipmap coll (range)) shift #(nth coll (mod (% (reversed %2)) (count coll))) left (partial shift dec) right (partial shift inc)] (-> :N left left left right))
:W
(defn choose-k [coll k] (let [n (count coll)] (if (<= k 1) (map vector coll) (reduce into (sorted-set) (for [i (range 1 (inc n))] (map #(into [(nth coll (dec i))] %) (choose-k (drop i coll) (dec k))))))))
#'user/choose-k
(let [base [1 2 3 5 6 8]] (reduce (fn [stack [a b]] (if (= (- a b) -1) (update stack (dec (count stack)) conj a) (conj (update stack (dec (count stack)) conj a) []))) [[]] (partition 2 1 base)))
[[1 2 3] [5 6] []]
(let [listInput [] listInput (conj listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}])] listInput)
[[{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]]
(defn stateful-random [[^long s _]] (let [R bit-shift-right X bit-xor a (unchecked-long (+ s 0x9E3779B97F4A7C15)) b (* (X a (R a 30)) 0xBF58476D1CE4E5B9) b (* (X b (R b 27)) 0x94D049BB133111EB) o (X b (R b 31))] (print o) [a o]))
#'user/stateful-random
(let [split-up (group-by #(.contains % "-") (map (comp str first) (ns-publics 'clojure.core))) [with-hyphens without-hyphens] (map #(get split-up %) [true false])] (for [x with-hyphens :let [fs (clojure.string/split x #"-")] f fs :when (some #{f} without-hyphens)] [x f]))
(["sort-by" "sort"] ["chunk-first" "chunk"] ["chunk-first" "first"] ["tree-seq" "seq"] ["unchecked-remainder-int" "int"] ["when-first" "when"] ["when-first" "first"] ["find-ns" "find"] ["get-thread-bindings" "get"] ["keep-indexed" "keep"] ["cond->>" "cond"] ["take-last" "take"] ["take-last" "last"] ["bit-set" "set"] ["qualified-keyword?" "keyword?"] ["line-seq" "seq"] ["unchecked-subtract-int" "in...
(let [a #(apply str (flatten %)) r repeatedly p partition N rand-nth n #(a (N (concat (repeat % "") (mapcat p [1 2 3] %&)))) v #(n 0 "aioeu" "iaai") w (fn [] (let [b (n 7 "!." "" "...") s [(n 0 "STKNYPKLG" "GlThShNyFt" "ZvrCth") (r (N [1 2]) #(do [(v) (n 9 (map str "'-" (r 2 v))) (n 0 (concat "lpstnkgx" [[(N ["h" "gl" "gr" "nd"]) (v)]]) "rlthggghtsltrkkhshng")]))]] [b (if (seq b) [" " s] [(n 3 ",") " " (. (a s) toLowerCase)])]))] (re-find #"[A-Z].+" (a [(r 500 w) "."])))
"Ke'uts, ftiashais paits tiathogle nots cthenits, guts yogh... Nyiap noth lex! Gerk. Yilt. Tiag... Kindo gi'ork kia'uth, sha'ipirk yaingogre zvraigh... Nyiang kia-eth shaigail, kaipo'ork, tep tu'ath nuk! Ket... Nyax su-iaparl kan, fturk, kai-ikh! Nia-ail! Gogh. Nyaishik nyong... Glurk. Zvrig. Yes... Nyandiaolt, shirlaig zvreno'iax! Kel liaggiax shi-egg shaixats... Le-airlang. Ftu'oghirk thax, cthe...
(let [r (range 100) matching (fn [n s] (map #(if (zero? (mod % n)) s "") r)) fizz (matching 3 "fizz") buzz (matching 5 "buzz") fizzbuzz (map str fizz buzz)] (map #(if (empty? %1) %2 %1) fizzbuzz r))
("fizzbuzz" 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 "f...
(let [base [1 2 3 5 6 8]] (reduce (fn [stack [a b]] (if (= (- a b) -1) (update stack (dec (count stack)) conj a) (conj (update stack (dec (count stack)) conj a) [b]))) [[]] (partition 2 1 base)))
[[1 2 3] [5 5 6] [8]]
(let [limit 5 original [:a :b :c :d :e]] (map #(map (partial nth original) %) (nth (iterate #(for [p % n (range (inc (count p)))] (concat (take n p) [(count p)] (drop n p))) [[0]]) (dec limit))))
((:e :d :c :b :a) (:d :e :c :b :a) (:d :c :e :b :a) (:d :c :b :e :a) (:d :c :b :a :e) (:e :c :d :b :a) (:c :e :d :b :a) (:c :d :e :b :a) (:c :d :b :e :a) (:c :d :b :a :e) (:e :c :b :d :a) (:c :e :b :d :a) (:c :b :e :d :a) (:c :b :d :e :a) (:c :b :d :a :e) (:e :c :b :a :d) (:c :e :b :a :d) (:c :b :e :a :d) (:c :b :a :e :d) (:c :b :a :d :e) (:e :d :b :c :a) (:d :e :b :c :a) (:d :b :e :c :a) (:d :b :...
(let [a #(apply str (flatten %)) p partition R rand-nth n #(a (R (mapcat p [1 2 3] %&))) v #(n "aioeu" "iaaiaa")] (a [(n "STKNYPKLGVZ" "GlThShNyFtCh" "ZvrCthShrMycTch") (for [_ " "] [(v) (n "lpstnkgxzv" (concat [(R '[h gl gr nd]) (v)] "rnthggghtsltrkkhshng") "ghnlok")])]))
"Cthaatsig"
(do (def state (long-array [9000])) (defn splitmix ^long [] (let [z (aset-long ^longs state 0 (unchecked-add (aget ^longs state 0) -7046029254386353132)) z (unchecked-multiply (bit-xor z (unsigned-bit-shift-right z 30)) -4658895280553007687) z (unchecked-multiply (bit-xor z (unsigned-bit-shift-right z 27)) -7723592293110705685)] (bit-xor z (unsigned-bit-shift-right z 31)))))
#'user/splitmix
(let [data {:a [34 56 7], :b [21 87 999], :c [3], :d [2 4], :s [4]}] (take-while seq (map (fn [i] (remove #(nil? (second %)) (map vector (keys data) (map #(nth % i nil) (vals data))))) (range))))
(([:a 34] [:b 21] [:c 3] [:d 2] [:s 4]) ([:a 56] [:b 87] [:d 4]) ([:a 7] [:b 999]))
(let [listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]] (nth (:paths (nth listInput 1)) 0))
[:node 2 :cost 1]
(defn cross [vec-a vec-b] (let [a (cycle vec-a) b (cycle vec-b)] (map (fn [ind] (- (* (nth a (+ ind 1)) (nth b (+ ind 2))) (* (nth a (+ ind 2)) (nth b (+ ind 1))))) '(0 1 2))))
#'user/cross
(let [listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]] (print (nth (:paths (nth listInput 1)) 0)))
nil
"[:node 2 :cost 1]"
((fn select-by [f & seqs] (when-let [non-empty-seqs (seq (remove empty? seqs))] (let [[[x & more] & others] (sort-by (comp f first) non-empty-seqs)] (lazy-seq (cons x (apply select-by f more others)))))) identity [1 3 5] [6 7 9] [2 4 8])
(1 2 3 4 5 6 7 8 9)
(defmacro heck-> ([x] x) ([x form] `(-> ~x ~form)) ([x form1 form2] `(let [x1# ~x x2# (-> x1# ~form1)] (-> (if (nil? x2#) x1# x2#) ~form2))) ([x form1 form2 & more] `(heck-> (heck-> ~x ~form1 ~form2) ~@more)))
#'user/heck->
(let [{x :x, :keys [a b], :strs [c d], :syms [e f], :or {x 0}, :as m} {:a 1, :b 2, "c" 3, "d" 4, 'e 5, 'f 6}] [x a b c d e f])
[0 1 2 3 4 5 6]
(defn stateful-random [[^long s _]] (let [R bit-shift-right X bit-xor a (unchecked-long (+ s 0x9E3779B97F4A7C15)) b (unchecked-long (* (X a (R a 30)) 0xBF58476D1CE4E5B9)) b (unchecked-long (* (X b (R b 27)) 0x94D049BB133111EB)) o (X b (R b 31))] (print o) [a o]))
#'user/stateful-random
(let [listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]] (print (count (:paths (nth listInput 1)))))
nil
"2"
(let [a #(apply str (flatten %)) p partition R rand-nth n #(a (R (mapcat p [1 2 3] %&))) v #(n "aioeu" "iaaiaa")] (a [(n "STKNYPKLGVZ" "GlThShNyFtCh" "ZvrCthShrMycTch") (for [_ " "] [(v) (n "lpstnkgxzv" (concat [(R '[h gl gr nd]) 1 (v)] "rnthggghtsltrkkhshng") "ghnlok")])]))
"Mycahgasl"
(let [r (range 1 11)] (for [c r b r :while (< b c) a r :while (< a b) :when (and (= (+ (* a a) (* b b)) (* c c)) (= (+ a b c) 24))] [a b c]))
([6 8 10])
(let [input ">>><<v^"] (count (distinct (reduce (fn [[latest & others :as houses] dir] (conj houses (condp = dir \< (update-in latest [0] dec) \> (update-in latest [0] inc) \^ (update-in latest [1] inc) \v (update-in latest [1] dec)))) '([0 0]) input))))
5
(let [listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]] (print (:node (nth (:paths (nth listInput 1)) 0))))
nil
"nil"
(let [structure {1 {:parent :root, :slug "lovely"}, 2 {:parent 1, :slug "lovelier"}, 3 {:parent 1, :slug "loveliest"}, 4 {:parent :root, :slug "cool"}}] (into {} (map (fn [[k {:keys [parent slug]}]] [k (str (get-in structure [parent :slug] "") "/" slug)]) structure)))
{1 "/lovely", 2 "lovely/lovelier", 3 "lovely/loveliest", 4 "/cool"}
(let [listInput [{:paths [{:node 1, :cost 1} {:node 2, :cost 2}]} {:paths [{:node 2, :cost 1} {:node 3, :cost 2}]} {:paths [{:node 0, :cost 1} {:node 1, :cost 2}]}]] (print (:node (nth (:paths (nth listInput 1)) 0))))
nil
"2"
(let [listInput [] listInput (concat listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}])] (print (count (:paths (nth listInput 1)))))
nil
"2"
(def <3 (fn [love & loves] (loop [l (str "I love " love) loves loves] (let [[love & loves] loves] (if (nil? love) (str l ".") (if (empty? loves) (str l " and " love ".") (recur (str l ", " love) loves)))))))
#'user/<3
(let [ppl [["{x:I{" 4289] [".^t#c" 9616] ["072!^" 16186] ["P`VQx" 2569] ["t3x.g" 6014]] fits (map second ppl) tot-fit (reduce + fits) cum-fits (rest (reductions + 0 fits)) roulette (for [k (range (count ppl))] [(first (nth ppl k)) (float (/ (nth cum-fits k) tot-fit))])] roulette)
(["{x:I{" 0.11090138] [".^t#c" 0.3595439] ["072!^" 0.77806795] ["P`VQx" 0.844495] ["t3x.g" 1.0])
(let [k-filter (fn [el coll] (filter #(not= el %) coll)) permute (fn perms [n coll] (if (= n 1) (map #(vector %) coll) (for [el coll nlis (perms (- n 1) (k-filter el coll))] (conj nlis el))))] (permute 4 [1 2 3 4]))
([4 3 2 1] [3 4 2 1] [4 2 3 1] [2 4 3 1] [3 2 4 1] [2 3 4 1] [4 3 1 2] [3 4 1 2] [4 1 3 2] [1 4 3 2] [3 1 4 2] [1 3 4 2] [4 2 1 3] [2 4 1 3] [4 1 2 3] [1 4 2 3] [2 1 4 3] [1 2 4 3] [3 2 1 4] [2 3 1 4] [3 1 2 4] [1 3 2 4] [2 1 3 4] [1 2 3 4])
(let [g {"a" {:b [1 2 3], :c [4 5]}, "d" {:b [2 3], :e [5 6 7]}}] (reduce (fn [m [k2 k1 v]] (assoc m k2 (assoc (m k2 {}) k1 v))) {} (for [[k1 m] g [k2 v] m] [k2 k1 v])))
{:b {"a" [1 2 3], "d" [2 3]}, :c {"a" [4 5]}, :e {"d" [5 6 7]}}
(let [listInput [{:paths [[:node 1 :cost 1] [:node 2 :cost 2]]} {:paths [[:node 2 :cost 1] [:node 3 :cost 2]]} {:paths [[:node 0 :cost 1] [:node 1 :cost 2]]}]] (print (:node (apply hash-map (nth (:paths (nth listInput 1)) 0)))))
nil
"2"
(let [sym (gensym) h #(with-meta % {sym true}) g #(if (-> % meta sym) (constantly %) %) f #(if (fn? %) [(%) (%) (%)] %) x 'foo] (= ((comp f g h) x) ((juxt f g h) x) [x x x]))
true