(into {} (map #(vector %1 [%2 %3]) (iterate inc 1) (concat (range -4 5) (range 4 -5 -1)) (cycle [-1 1])))
{1 [-4 -1], 2 [-3 1], 3 [-2 -1], 4 [-1 1], 5 [0 -1], 6 [1 1], 7 [2 -1], 8 [3 1], 9 [4 -1], 10 [4 1], 11 [3 -1], 12 [2 1], 13 [1 -1], 14 [0 1], 15 [-1 -1], 16 [-2 1], 17 [-3 -1], 18 [-4 1]}
(let [days-of-christmas (range 12 0 -1) total-presents (reduce #(+ % (apply + %2)) 0 (take-while seq (iterate rest days-of-christmas)))] total-presents)
364
((comp (partial apply map vector) (juxt identity (comp reverse (partial take-while first) (partial iterate pop) pop))) [:a :b :c :d])
([:a [:a]] [:b [:a :b]] [:c [:a :b :c]])
(reverse (ffirst (drop-while (comp not zero? second) (iterate (fn [[i r]] [(conj i (rem r 10)) (quot r 10)]) [[] 420]))))
(4 2 0)
(let [list [0 1 2 3]] (map #(take 4 %) (take 4 (iterate #(drop 1 %) (cycle list)))))
((0 1 2 3) (1 2 3 0) (2 3 0 1) (3 0 1 2))
(let [wtf (iterate macroexpand-1 '(->> . :__ (->> @two (->> args))))] (= (nth wtf 5) (nth wtf 9)))
true
(vector (def ^:dynamic *modulus* 10) (defn f [x] (-> x (inc) (mod *modulus*))) (binding [*modulus* 3] (take 3 (iterate f 1))))
[#'user/*modulus* #'user/f (1 2 3)]
(first (remove reduced? (iterate deref (last (reductions (fn [t i] (if (> i 10) (reduced t) (+ t i))) (range 200))))))
55
(reduce + (filter even? (take-while #(< % 4e6) (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))))
4613732
(defn binary [x] (apply str (reverse (map #(if (odd? %) \1 \0) (take-while pos? (iterate #(bit-shift-right % 1) x))))))
#'user/binary
(let [counter (atom 0)] (nth (map (fn [i] (map #(swap! counter + %) (take 3 (repeat 1)))) (iterate inc 0)) 1000))
(1 2 3)
(letfn [(sieve [[p & nums]] (lazy-seq (cons p (sieve (remove #(zero? (mod % p)) nums)))))] (take 10 (sieve (iterate inc 2))))
(2 3 5 7 11 13 17 19 23 29)
(some (fn [[e pos]] (when (= e :progress) pos)) (map (fn [x y] [x y]) '(:a :b :progress) (iterate inc 0)))
2
(clojure.string/join " " (take 40 (reductions #(if (= %2 42) (reduced %1) (inc %1)) (iterate #(bit-xor % (bit-shift-left 1 (rand-int 8))) 12))))
"12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 46"
(letfn [(multi-of [nr maxi] (take-while #(< % maxi) (iterate #(+ nr %) nr)))] (apply + (concat (multi-of 3 10) (multi-of 5 10))))
23
(let [most #(if (pos? (compare % %2)) % %2)] (reduce most (map vector (map #(* 2 %) (range 100)) (iterate inc 0))))
[198 99]
(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 0) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(letfn [(multi-of [nr maxi] (take-while #(< % maxi) (iterate #(+ nr %) nr)))] (apply + (concat (multi-of 3 1000) (multi-of 5 1000))))
266333
(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)
((fn [& args] (->> args (iterate next) (take-while identity) (filter (comp #{:foo} first)) first second)) 1 2 :foo 3 4 5 6)
3
(letfn [(crack [c] (cons c (map (fn [n] (map #(str % n) c)) (iterate inc 1))))] (take 3 (crack '("user" "pass"))))
(("user" "pass") ("user1" "pass1") ("user2" "pass2"))
(def fib (lazy-seq (for [x (iterate inc 0)] (if (< x 2) x (+ (nth fib (- x 2)) (nth fib (- x 1)))))))
#'user/fib
(defn drop-nth [n s] (let [zip (partial map vector)] (for [[i x] (zip (iterate inc 1) s) :when (not= 0 (rem i n))] x)))
#'user/drop-nth
(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]
(apply str (map (comp char #(+ 97 %)) (take 5 (map first (iterate (fn [[i p]] [(bit-and p 127) (bit-shift-right p 7)]) [7 29541764])))))
"hello"
(apply str (map (comp char #(+ 97 %)) (take 5 (map first (iterate (fn [[i p]] [(bit-and p 31) (bit-shift-right p 5)]) [7 470372])))))
"hello"
(letfn [(myrange [n] (take n (iterate (fn [x] (print x " ") (inc x)) 0))) (mytest [a b & others])] (apply mytest (myrange 10)))
nil
"0 1 2 "
(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 [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 [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 ...
(map first (filter #(= 1 (- (apply max %) (apply min %))) (butlast (map (partial take 2) (take-while seq (iterate rest [1 2 10 11 20 21]))))))
(1 10 20)
(let [a (atom []) res (partition-by #(do (swap! a conj %) (zero? (mod % 5))) (iterate inc 0)) forced (doall (take 3 res))] [forced @a])
[((0) (1 2 3 4) (5)) [0 1 1 2 3 4 5 5]]
(let [f (fn [s] (->> (iterate #(.indexOf s "<C>" (inc %)) -1) (drop 1) (take-while (complement neg?))))] (f "la la la <C> la la <C> <C> foo <C>"))
(9 19 23 31)
(take 20 ((fn f [l] (if (nil? l) nil (let [y (first l)] (lazy-seq (cons y (f (filter #(> (mod % y) 0) (next l)))))))) (iterate inc 2)))
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71)
(defn lazy-look-and-generate [] (iterate (fn [x] (mapcat (fn [n] (loop [x1 (first n) y (rest n) count 1] (if (= x1 (first y)) (recur x1 (rest y) (inc count)) (conj nil count x1)))) x)) [1]))
#'user/lazy-look-and-generate
((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"]
(->> (update-in (vec (->> 11 (* (inc 11)) (iterate dec) (take 11) (reverse) (take 1) (first) (range 111) (take (inc 1)) (map char) (reverse) (partition (inc 1) 1) (cycle) (take (inc 1)))) [1] reverse) (flatten) (apply str))
"poop"
((fn [p s] (->> [nil s] (iterate (fn [[_ [_ & xs]]] (split-with (complement p) xs))) (partition 2 1) (map (fn [[[_ [i]] [r]]] (cons i r))) (take-while first))) #{1} [1 2 2 1 3 3 1 4 4])
((1 2 2) (1 3 3) (1 4 4))
(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 :...
((fn [p s] (->> [nil s] (iterate (fn [[_ [_ & xs]]] (split-with (complement p) xs))) (partition 2 1) (map (fn [[[_ [i]] [r]]] (cons i r))) (take-while first))) #{1} [1 2 3 4 1 5 6 7 1 8 1 9 1 1 1 2 1])
((1 2 3 4) (1 5 6 7) (1 8) (1 9) (1) (1) (1 2) (1))