This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn gc-managed-pool | |
"Wraps a manually managed pool to create an automatically managed pool | |
that returns resources to the pool as the doled out references are | |
garbage collected by the jvm." | |
[pool borrow return] | |
(letfn [(interfaces [x] | |
(->> (ancestors (class x)) | |
(filter class?) | |
(filter #(.isInterface %)))) | |
(create-facade [x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro evaluate* [bindings & code] | |
(letfn [(eval-with-bindings [bindings code] | |
(let [ks (keys bindings)] | |
((eval `(fn [{:syms [~@ks]}] ~code)) bindings)))] | |
`(~eval-with-bindings ~bindings '(do ~@code)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ^:dynamic *hook* false) | |
(defn hook [var f] | |
(alter-var-root var | |
(fn [original] | |
(fn [& args] | |
(if *hook* | |
(apply f args) | |
(apply original args)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defonce ^:dynamic *hooked* false) | |
(defn hook [var f] | |
(alter-var-root var | |
(fn [original] | |
(fn [& args] | |
(if *hooked* | |
(apply f args) | |
(apply original args)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro cached [& body] | |
(let [dependencies (transient #{}) | |
resolved (walk/postwalk | |
(fn [form] | |
(if (symbol? form) | |
(if-some [resolved (resolve &env form)] | |
(when (var? resolved) | |
(conj! dependencies resolved) | |
(.toSymbol ^Var resolved)) | |
form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.core.async :as async]) | |
(require '[mount.core :as mount]) | |
(defn work-queue [buffer max-retries parallelism] | |
(let [chan (async/chan buffer)] | |
(dotimes [_ parallelism] | |
(async/go-loop [] | |
(when-some [i (async/<! chan)] | |
(let [{:keys [f retries]} (if (map? i) i {:retries 0 :f i})] | |
(try (f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.test :refer :all]) | |
(require '[clojure.core.async :as async]) | |
(deftest from-channel-still-consumed-after-to-channel-is-closed | |
(let [from-counter (atom 0) | |
xf-counter (atom 0) | |
from (async/chan) | |
to (async/chan 1000) | |
xf (map #(do (Thread/sleep 100) | |
(swap! xf-counter inc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ^:dynamic *locks* (atom {})) | |
(defn new-lock [] | |
{:count 1 :ref (Object.)}) | |
(defn inc-lock [lock] | |
(when lock (update lock :count inc))) | |
(defn dec-lock [lock] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro nor [& more] | |
(conj `~(partition 2 (interleave (repeat 'not) more)) 'and)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn attach-channel-cleanup [chan f] | |
(add-watch (.closed chan) | |
"channel-resource-cleanup" | |
(fn [_ _ old-state new-state] | |
(when (and (not old-state) new-state) | |
(f)))) | |
chan) |