This file contains hidden or 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
class PersistentList | |
attr_reader :length, :head, :tail | |
include Enumerable | |
EMPTY = Object.new | |
class << EMPTY | |
include Enumerable | |
def length; 0; end | |
def each(&block); end | |
def to_s; "()"; end | |
def cons(item); PersistentList.new(item, self); end |
This file contains hidden or 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 with-local-redefs-fn | |
[a-var its-new-value func] | |
(cast clojure.lang.IFn @a-var) | |
(alter-meta! a-var | |
(fn [m] | |
(if (::scope-count m) | |
(update-in m [::scope-count] inc) | |
(assoc m | |
::scope-count 1 | |
::thread-local-var (doto (clojure.lang.Var/create @a-var) |
This file contains hidden or 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 primes | |
"Returns a lazy sequence of primes." | |
[] | |
(cons 2 | |
(lazy-seq | |
(let [local-primes (->> (primes) | |
(map (juxt identity #(apply + (repeat % %)))))] | |
(->> (iterate #(+ % 2) 3) | |
(remove (fn [n] | |
(->> local-primes |
This file contains hidden or 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 defring | |
[additive-identity | |
multiplicative-identity | |
add | |
negate | |
multiply] | |
`(let [add# ~add | |
negate# ~negate | |
multiply# ~multiply] | |
(defn ~'+ |
This file contains hidden or 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
(ns deftypes.are.slow | |
(:use criterium.core)) | |
(deftype Pair [a b]) | |
(bench | |
(->> (range 100000) | |
(map #(vector % (inc %))) | |
(map second) | |
(apply +))) |
This file contains hidden or 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
(import '[java.util.concurrent Executors Executor]) | |
(set! *warn-on-reflection* true) | |
(def ^Executor tp (Executors/newFixedThreadPool 4)) | |
;; Reflection warning, NO_SOURCE_PATH:1:1 - call to submit can't be resolved. | |
(.submit ^Executor tp ^Callable (constantly 42)) |
This file contains hidden or 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
(extend-protocol IReadResult | |
java.sql.Array | |
(read-result [sqlarray] | |
(let [rs (.getResultSet sqlarray)] | |
(loop [rows []] | |
(if (.next rs) | |
(recur (conj rows (.getObject rs 2))) | |
rows))))) |
This file contains hidden or 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 usages | |
[filename] | |
(let [forms (-> filename | |
slurp | |
(->> (format "(%s)")) | |
read-string) | |
analyses (map clojure.tools.analyzer/analyze-form forms) | |
defs (filter (comp #{:def} :op) analyses)] | |
(into {} | |
(for [def defs |
This file contains hidden or 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 byte->long [b] (if (neg? b) (+ 256 b) b)) | |
(defn bytes->long | |
[bytes] | |
{:pre [(= 8 (count bytes))]} | |
(apply + (map bit-shift-left (reverse (map byte->long bytes)) (range 0 64 8)))) | |
(defn bytes->uuid | |
[bytes] | |
(java.util.UUID. (bytes->long (take 8 bytes)) |
This file contains hidden or 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
(let [re-1 #"([^#\.:]*)((?:[#\.][^#\.:]+)*)" | |
re-2 #"([#\.])([^#\.:]+)"] | |
(defn css | |
"Laser selector for css stuff. Just handles the AND format | |
of foo#bar.baz.bang, no ORing." | |
[s] | |
(let [[_ a b] (re-matches re-1 (name s))] | |
(assert (or (seq a) (seq b)) "Bad css selector.") | |
(apply l/and | |
(if (empty? a) |