(nth (iterate #(assoc % (rand-int (count %)) 'foo) [1 2 3 4 5 6]) 3)
[1 2 foo 4 5 foo]
(->> (map (juxt first rest next) [() [] #{} {} nil]) (apply =) (iterate true?) (take 3) (every? true?))
true
(map #(- %1 (* %2 250)) '(0 250 500 750 1000) (iterate inc -1))
(250 250 250 250 250)
(->> (map (juxt first rest next) [() [] #{} {} nil]) (apply =) (iterate true?) (take 3) (apply =))
true
(take 5 (let [v [5 0 2 4 1 3]] (iterate #(mapv % %) v)))
([5 0 2 4 1 3] [3 5 2 1 0 4] [1 4 2 5 3 0] [4 3 2 0 5 1] [5 0 2 4 1 3])
(defn return-truthy [it ob] (let [[f & r] (iterate it ob)] (take-while (partial not= f) r)))
#'user/return-truthy
((fn [n] (first (nth (iterate (fn [[x y]] [y (+ x y)]) [1 1]) n))) 6)
13
(let [all ["a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]] (nth (iterate #(for [base % more all] (str base more)) all) 1))
("aa" "ab" "ac" "ad" "ae" "af" "ag" "ah" "ai" "aj" "ak" "al" "am" "an" "ao" "ap" "aq" "ar" "as" "at" "au" "av" "aw" "ax" "ay" "az" "a0" "a1" "a2" "a3" "a4" "a5" "a6" "a7" "a8" "a9" "ba" "bb" "bc" "bd" "be" "bf" "bg" "bh" "bi" "bj" "bk" "bl" "bm" "bn" "bo" "bp" "bq" "br" "bs" "bt" "bu" "bv" "bw" "bx" "by" "bz" "b0" "b1" "b2" "b3" "b4" "b5" "b6" "b7" "b8" "b9" "ca" "cb" "cc" "cd" "ce" "cf" "cg" "ch"...
(filter identity (map #(when-not % %2) [nil nil 1 nil 1 nil] (iterate inc 0)))
(0 1 3 5)
(take 5 (iterate (comp (partial apply interleave) (partial partition-all 3)) [1 2 3 4 5 6]))
([1 2 3 4 5 6] (1 4 2 5 3 6) (1 5 4 3 2 6) (1 3 5 2 4 6) (1 2 3 4 5 6))
(take 5 (map #(apply str (interleave % (repeat "/"))) (iterate butlast (seq (.split "abc/def/ghi/jkl/mno" "/")))))
("abc/def/ghi/jkl/mno/" "abc/def/ghi/jkl/" "abc/def/ghi/" "abc/def/" "abc/")
(if-let [[x & xs] (seq [1 2 3])] (cons x xs) (iterate #(+ % 1) 1))
(1 2 3)
(defn fibber [n] (take n (map first (iterate (fn [[a b]] [b (+ a b)]) [1 1]))))
#'user/fibber
(map second (take 3 (drop 1 (iterate (fn [[n d]] ((juxt quot rem) n 5)) [85 0]))))
(0 2 3)
(let [[first-row second-row third-row] (iterate (partial drop 3) [1 2 3 4 5 6 7 8 9])] third-row)
(7 8 9)
(take 5 (filter #(= (count ((comp set str) %)) (count (str %))) (iterate (partial + 2013) 2013)))
(2013 4026 6039 8052 12078)
(defn repeat-until-stable [f x] (reduce (fn [x1 x2] (if (= x1 x2) (reduced x1) x2)) (iterate f x)))
#'user/repeat-until-stable
(defn rev-digits [n] (->> n (iterate #(quot % 10)) (take-while (complement zero?)) (map #(rem % 10))))
#'user/rev-digits
(take 200 (let [all ["a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]] (nth (iterate #(for [base % more all] (str base more)) all) 7)))
("aaaaaaaa" "aaaaaaab" "aaaaaaac" "aaaaaaad" "aaaaaaae" "aaaaaaaf" "aaaaaaag" "aaaaaaah" "aaaaaaai" "aaaaaaaj" "aaaaaaak" "aaaaaaal" "aaaaaaam" "aaaaaaan" "aaaaaaao" "aaaaaaap" "aaaaaaaq" "aaaaaaar" "aaaaaaas" "aaaaaaat" "aaaaaaau" "aaaaaaav" "aaaaaaaw" "aaaaaaax" "aaaaaaay" "aaaaaaaz" "aaaaaaa0" "aaaaaaa1" "aaaaaaa2" "aaaaaaa3" "aaaaaaa4" "aaaaaaa5" "aaaaaaa6" "aaaaaaa7" "aaaaaaa8" "aaaaaaa9" "aa...
(defn maplist [f & colls] (apply map f (map #(take (count %) (iterate rest %)) colls)))
#'user/maplist
(first (filter #(= [5 6] (take 2 %)) (iterate rest [3 4 5 6 7 8])))
(5 6 7 8)
(map first (take 3 (drop 1 (iterate (fn [[n d]] ((juxt quot rem) n 5)) [85 0]))))
(17 3 0)
(defn fibber [n] (take n (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1]))))
#'user/fibber
(take 1 (iterate (comp apply interleave (partial partition 3)) [1 2 3 4 5 6 7 8 9]))
([1 2 3 4 5 6 7 8 9])
(take-while some? (iterate #(let [li (.lastIndexOf % "/")] (when (<= 0 li) (.substring % 0 li))) "a/b/c/d/e"))
("a/b/c/d/e" "a/b/c/d" "a/b/c" "a/b" "a")
(->> (filter odd? (map #(* % %) (iterate inc 1))) (take-while #(< % 10000000000)) (reduce +))
166666666650000
(take 10 (map first (iterate (fn [[t n]] (let [i (inc n)] [(+ t i) i])) [1 1])))
(1 3 6 10 15 21 28 36 45 55)
(map #(when-not (= %2 %3) [%1 %2 %3]) (iterate inc 0) [:a :b :c] [:a :a :a])
(nil [1 :b :a] [2 :c :a])
(let [coll [:a :b :c :d :e]] (->> coll (iterate pop) (take (count coll)) (reverse) (cons []) (mapv vector coll)))
[[:a []] [:b [:a]] [:c [:a :b]] [:d [:a :b :c]] [:e [:a :b :c :d]]]
(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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133...
(ffirst (drop-while (comp not zero? second) (iterate (fn [[i r]] [(conj i (rem r 10)) (quot r 10)]) [() 420])))
(4 2 0)
(let [[board first-row second-row third-row] (iterate (partial drop 3) [1 2 3 4 5 6 7 8 9])] third-row)
()
(mapcat #(map vector (repeat %) (next %2)) [1 2 3 4] (iterate next [1 2 3 4]))
([1 2] [1 3] [1 4] [2 3] [2 4] [3 4])
(letfn [(k [& xs] (->> (iterate (partial * 1000) 1) (map * (reverse xs)) (reduce +)))] (k 18 869 726 156))
18869726156
(let [x [:a :b :c :d :e]] (map vector x (cons [] (reverse (take (count x) (iterate pop x))))))
([:a []] [:b [:a]] [:c [:a :b]] [:d [:a :b :c]] [:e [:a :b :c :d]])
(reduce (fn [so-far [value & origs]] (conj so-far (* value value))) [] (take-while identity (iterate next [1 2 3 4])))
[1 4 9 16]
(let [compose-n-times (fn [f n] (fn [x] (nth (iterate f x) n))) plus-10 (compose-n-times inc 10)] (plus-10 5))
15
(reduce + (map #(mod % 10) (take-while #(>= % 1) (iterate #(int (/ % 10)) 123456789))))
45
(apply str (first (last (take 32 (iterate (fn [[bits left]] [(conj bits (bit-and left 1)) (bit-shift-right left 1)]) [() -1])))))
"1111111111111111111111111111111"
(letfn [(dechunk [xs] (lazy-seq (when (seq xs) (cons (first xs) (dechunk (next xs))))))] (first (map prn (dechunk (iterate inc 1)))))
nil
"1\n"
(letfn [(random-swap [coll] (let [[l r] (split-at (rand-int (count coll)) coll)] (concat r l)))] (nth (iterate random-swap (range 20)) 5))
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)
(reduce #(if (= %2 42) (reduced %1) (inc %1)) (iterate #(bit-xor % (bit-shift-left 1 (rand-int 8))) 12))
520
(apply (fn [& xs] (if (and (seq xs) (< (first xs) 1000)) (recur (rest xs)) 17)) (iterate inc 0))
17
(take-while #(> % 1) (iterate (fn [n] (if (even? n) (/ n 2) (inc (* n 3)))) 9))
(9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2)
(take 3 (iterate (fn [m] (let [i (rand-int (count m))] (dissoc m i))) {0 :a, 1 :b, 2 :c}))
({0 :a, 1 :b, 2 :c} {0 :a, 1 :b} {0 :a})
((fn [els] (take (count els) (iterate (fn [l] (cons (last l) (butlast l))) els))) [0 1 2 3 4])
([0 1 2 3 4] (4 0 1 2 3) (3 4 0 1 2) (2 3 4 0 1) (1 2 3 4 0))
(defn fix-iter [f initial] (take-while (fn [x] (not (= x (apply f x)))) (iterate (fn [x] (apply f x)) initial)))
#'user/fix-iter
(let [items (map vector (iterate inc 0) [:a :b :c :d])] (some (fn [[i v]] (if (= v :c) i)) items))
2
((partial (comp (partial reduce +) (partial apply map *) reverse list) (iterate #(/ % 10) 0.1M)) [3 5 2 1])
0.3521M
(take-while #(< % 10000) (map first (iterate (fn [[n step]] [(+ n (inc (quot step 20))) (inc step)]) [1000 1])))
(1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1021 1023 1025 1027 1029 1031 1033 1035 1037 1039 1041 1043 1045 1047 1049 1051 1053 1055 1057 1059 1062 1065 1068 1071 1074 1077 1080 1083 1086 1089 1092 1095 1098 1101 1104 1107 1110 1113 1116 1119 1123 1127 1131 1135 1139 1143 1147 1151 1155 1159 1163 1167 1171 1175 1179 1183 1187 1191 1195 1199...