Skip to content

Instantly share code, notes, and snippets.

(defun copy-for-stackoverflow (start end)
(interactive "r")
(let ((oldbuf (current-buffer)))
(with-current-buffer (generate-new-buffer "so-paste")
(insert-buffer-substring oldbuf start end)
(indent-rigidly (point-min) (point-max) 4)
(kill-ring-save (point-min) (point-max))
(kill-buffer))))
(let [coll [[:a :b :c] [:d] [:e :f]]]
(into {} (for [[idx xs] (map-indexed vector coll),
x xs]
[x idx])))
useful.seq=> (glue conj [] #(< (peek %1) %2) (constantly false)
[1 2 3 0 9 1 5 6])
([1 2 3] [0 9] [1 5 6])
useful.seq=> (apply max-key count *1)
[1 5 6]
@amalloy
amalloy / calculate_change.clj
Created August 26, 2012 00:24 — forked from actsasbuffoon/calculate_change.clj
My first attempt at Clojure. Suggestions/criticism welcome.
(let [currency-values [[:hundreds 10000]
[:fifties 5000]
[:twenties 2000]
[:tens 1000]
[:fives 500]
[:ones 100]
[:quarters 25]
[:dimes 10]
[:nickels 5]
[:pennies 1]]]
(let [q (atom {:contents #queue [], :ret nil})]
{:take (fn []
(:ret (swap! q (fn [{:keys [contents]}]
{:contents (pop contents)
:ret (peek contents)}))))
:put (fn [x]
(swap! q update-in :contents conj x))})
(def charset "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(def N (count charset))
(defn valueFor [x]
(.indexOf charset (str x)))
(defn charFor [x]
(nth charset x))
(defn combiner [o]
(when (window-system)
(global-unset-key "\C-z")
(dolist (key (list (kbd "C-x C-c") (kbd "s-q")))
(global-set-key key (lambda ()
(interactive)
(message "Why would you ever leave?")))))
(letfn [(remove-once [x coll]
(lazy-seq
(let [y (first coll)]
(if (= x y)
(rest coll)
(cons y (remove-once x (rest coll)))))))]
(defn permute [coll]
(lazy-seq
(if-let [coll (seq coll)]
(for [x coll,
(defn fight-one-round [[a b :as fighters]]
(let [ticks (apply min (map :cooldown-remaining fighters))
damage-dealt (fn [fighter]
(if (= ticks (:cooldown-remaining fighter))
(:dmg fighter)
0))
tick (fn [fighter]
(update-in fighter [:cooldown-remaining]
(fn [cdr]
(if (= cdr ticks)
(defn carry-over [coll]
(reductions (fn [[x :as xs] ys]
(update-in ys [0] (fn [y]
(or (not-empty y) x))))
coll))
user> (carry-over '(["fresh" :hello :world] ["" :foo :bar] ["" :bar :baz] ["cool" :qux :qex] ["" :etc :etc]))
(["fresh" :hello :world] ["fresh" :foo :bar] ["fresh" :bar :baz] ["cool" :qux :qex] ["cool" :etc :etc])