(let [f #(inc %) m (memoize #(println (% 1)))] (m f) (m f))
nil
"2\n"
(defn find-first-with-index [pred coll] (reduce #(if (pred %2) (reduced [%1 %2]) (inc %1)) 0 coll))
#'user/find-first-with-index
(map (fn [[x y z]] [(inc x) y z]) '((1 0 0) (3 2 2)))
([2 0 0] [4 2 2])
(drop-while #(< % 100) (reductions #(do (println %) (inc %2)) [98 99 100 101]))
(100 101 102)
"98\n100\n101\n"
(reduce (fn [[state acc] item] [(inc state) (str acc state item)]) [1 ""] '[foo bar zonk])
[4 "1foo2bar3zonk"]
((fn ngrams [x] (mapv #(vec (take (inc %) x)) (range (count x)))) [1 2 3 4])
[[1] [1 2] [1 2 3] [1 2 3 4]]
(loop [x 0 v (transient [])] (if (> x 5) (persistent! v) (recur (inc x) (conj! v x))))
[0 1 2 3 4 5]
(reduce #(assoc %1 %2 (inc (%1 %2 0))) {} [1 2 2 3 3 3 4 4])
{1 1, 2 2, 3 3, 4 2}
(take 20 (iterate #(->> % - (+ (-> % int (* 2))) inc (/ 1)) 1))
(1 1/2 2N 1/3 3/2 2/3 3N 1/4 4/3 3/5 5/2 2/5 5/3 3/4 4N 1/5 5/4 4/7 7/3 3/8)
(do (def ^:const x 1) (defn foo [] (inc x)) (println (foo)) (def x 2) (println (foo) x))
nil
"2\n2 2\n"
(for [x [1 2 3] :let [y (inc x)] z [y x y]] [x y z])
([1 2 2] [1 2 1] [1 2 2] [2 3 3] [2 3 2] [2 3 3] [3 4 4] [3 4 3] [3 4 4])
(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
(reduce (fn [m x] (update-in m [x] (fnil inc 0))) {} [2 3 3 2 4 2 5])
{2 3, 3 2, 4 1, 5 1}
(update-in [1 1 {:a [0 0 {:c 20}], :b 9} 3 3] [2 :a 2 :c] inc)
[1 1 {:a [0 0 {:c 21}], :b 9} 3 3]
((fn [f coll] (reduce (fn [acc x] (conj acc (f x))) [] coll)) inc [1 2 3 4])
[2 3 4 5]
(reduce #(if (>= 20 %2) (inc %1) %1) 0 [1 5 9 12 15 20 34])
6
(reduce (fn [acc el] (if (odd? el) (inc acc) acc)) 0 [1 2 1 2 3 3 4])
4
(let [base 36] (map #(inc (read-string (apply str "0" (drop-last (Integer/toString (dec %) base))))) (range 1 100)))
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3)
(let [v [1 2 3 4 5] i 2] (into (subvec v 0 i) (subvec v (inc i))))
[1 2 4 5]
(reduce (fn [m e] (assoc m e (inc (get m e 0)))) {} [1 2 3 2 2 3])
{1 1, 2 3, 3 2}
(into {} (map (fn [[k v]] [k (if (even? k) (inc v) v)]) {1 10, 2 11, 3 10}))
{1 10, 2 12, 3 10}
(loop [m {} i 0] (if (> i 5) m (recur (assoc m i (str i)) (inc i))))
{0 "0", 1 "1", 2 "2", 3 "3", 4 "4", 5 "5"}
((fn [x] (map vector (concat [0] (range 11 (inc x) 10)) (concat (range 10 x 10) [x]))) 55)
([0 10] [11 20] [21 30] [31 40] [41 50] [51 55])
(loop [l '(1 2 3 2) i 0 max -1 max-i -1] (if-let [[head & tail] l] (if (> head max) (recur tail (inc i) head i) (recur tail (inc i) max max-i)) max-i))
2
(defn my-count [x] (fn self [y c] (if (next y) (self (next y) (inc c)) c)) x 0)
#'user/my-count
(defn update-nth [s n f] (concat (take n s) [(apply f [(nth s n)])] (drop (inc n) s)))
#'user/update-nth
(defn oinc [num] (reduce + num (map #(%) (repeat (inc (count (str (:arglists (meta oinc))))) #(*)))))
#'user/oinc
(reductions (fn [move word] (({"next" inc, "prev" dec} word) move)) 0 (clojure.string/split "next prev next next" #" "))
(0 1 0 1 2)
(reductions (fn [a b] (if b (inc a) a)) 0 (map odd? (take 10 (repeatedly #(rand-int 2)))))
(0 0 0 1 1 1 1 1 2 3 4)
(let [v (vec (range 10)) n 3] (concat (subvec v 0 n) (subvec v (inc n) (count v))))
(0 1 2 4 5 6 7 8 9)
(reduce #(assoc %1 %2 (inc (%1 %2 0))) {} [:a :a :b :b :b :c :d :e :e])
{:a 2, :b 3, :c 1, :d 1, :e 2}
(letfn [(xif [p f] #(if (p %) (f %) %))] (map (xif even? inc) (range 1 11)))
(1 3 3 5 5 7 7 9 9 11)
(let [items '(a b c)] (reduce (fn [[i acc] x] [(inc i) (conj acc x)]) [0 []] items))
[3 [a b c]]
(let [+i (fn [n] (inc (or n 0)))] (reduce #(update-in % [%2] +i) {} (seq "foo bar baz")))
{\space 2, \a 2, \b 2, \f 1, \o 2, \r 1, \z 1}
(-> {:foo [1 2]} (update-in [:foo] conj 3) (update-in [:foo 1] + 10) (update-in [:foo] (partial mapv inc)))
{:foo [2 13 4]}
(persistent! (reduce-kv (fn [m k v] (assoc! m k (inc v))) (transient {}) {:a 0, :b 1, :c 2}))
{:a 1, :b 2, :c 3}
(let [iterative (fn [x] (loop [y x] (if (= y 10000) :done (recur (inc y)))))] (iterative 0))
:done
((fn [a] (do (println "swaped out: " @a) (println "swaped in: " (swap! a inc)))) (atom 0))
nil
"swaped out: 0\nswaped in: 1\n"
(reduce (fn [acc path] (update-in acc path (fnil inc 0))) {} '((a b c) (a b c)))
{a {b {c 2}}}
(count (loop [c 0 x [0]] (if (< c 4601) (recur (inc c) (cons 0 x)) x)))
4602
(for [i (range (inc (count [1 2 3 4])))] ((juxt take drop) i [1 2 3 4]))
([() (1 2 3 4)] [(1) (2 3 4)] [(1 2) (3 4)] [(1 2 3) (4)] [(1 2 3 4) ()])
(let [make-seq (fn make-seq [start-value] (lazy-seq (cons start-value (make-seq (inc start-value))))) a (make-seq 1)] (take 4 a))
(1 2 3 4)
(reduce (fn [acc x] (update-in acc [x] (fnil inc 0))) {} (tree-seq vector? seq [[1] [1 [1] [1]]]))
{1 4, [1 [1] [1]] 1, [1] 3, [[1] [1 [1] [1]]] 1}
(let [fns [inc #(mod % 100) #(* % %)]] (reduce #(%2 %1) 104 fns))
25
(reduce (fn [acc x] (update-in acc [x] (fnil inc 0))) {} (tree-seq vector? seq [[:x] [:x [:x] [:x]]]))
{:x 4, [:x [:x] [:x]] 1, [:x] 3, [[:x] [:x [:x] [:x]]] 1}
(last (filter #(< % 10) (reductions #(%2 %1) 0 [inc (partial * 5) (constantly 50)])))
5
(let [apply-if #(if (%1 %3) (%2 %3) %3)] (map #(apply-if odd? inc %) (range 10)))
(0 2 2 4 4 6 6 8 8 10)
(let [x [1 2 3 4 5 7]] (clojure.set/difference (set (range 1 (inc (last x)))) (set x)))
#{6}
(update-in {:a {:b 1}} [:a :b] #(if % (inc %) (throw (Exception. "this is bad code"))))
{:a {:b 2}}
(let [x 1] (-> x inc (as-> y (-> y (+ 10) (as-> z [x y z])))))
[1 2 12]