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
// A simpler method than reflection-based traversal, using Unsafe | |
public static Unsafe getUnsafe() { | |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); | |
f.setAccessible(true); | |
return (Unsafe) f.get(null); | |
} | |
public static long sizeOf(Object o) { | |
Unsafe u = getUnsafe(); |
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 rational-bozo-sort | |
"Choose, compare & if necessary exchange random indices n times, using the | |
implicit ordering given by the lookup function" | |
[lookup s n] | |
(let [a (to-array s) | |
c (alength a)] | |
(dotimes [_ n] | |
(let [idx1 (rand-int c) | |
idx2 (rand-int idx1) | |
v1 (aget a idx1) |
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
(defprotocol TwoPersonFn | |
(operate [this a b])) | |
(defn shorter? [a b]) | |
(defn marry! [a b]) | |
(defn same-age? [a b]) | |
(extend-type (class shorter?) TwoPersonFn | |
(operate [this a b] (this a b))) | |
(extend-type (class marry!) TwoPersonFn |
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 ^:private poison (Object.)) | |
(defn seq-to-qs | |
"Place each element of the lazy seq in each of the queues, and finally the | |
poison value." | |
[s & qs] | |
(doseq [e s q qs] (.put ^BlockingQueue q e)) | |
(doseq [q qs] (.put ^BlockingQueue q poison))) | |
(defn lazy-q-seq |
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 unique-vec1 | |
"Straight-up Clojure sequence traversal" | |
[s] | |
(vec (set s))) | |
(defn unique-vec2 | |
"Conj into transient set and vectorize" | |
[s] | |
(loop [remaining s | |
seen (transient #{})] |
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 with-interns | |
"interns => [intern1 intern2...] | |
Evaluates body while making available many functions, bound to the | |
symbols in interns. Each fn, when called, returns a deduped reference to | |
its argument. The deduplication is with respect to any previously-called | |
arguments to that fn. | |
(with-interns [intern] | |
(into {} | |
(for [k (range 100000)] |
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 intern-map | |
[m] | |
(let [val-set (set (vals m))] | |
(into {} | |
(for [[k v] m] | |
{k (val-set v)})))) | |
(defn make-duped-hashmap [] | |
(into {} | |
(for [k (range 10000000)] |
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 switch-refs | |
"Must be called in transaction" | |
[a b] | |
(let [av @a bv @b | |
setter (fn [_ v] v)] | |
(commute a setter bv) | |
(commute b setter av))) | |
(defn switch! | |
"Blocks on both sequentially, nontransactional" |
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 flexigroup | |
"Like group-by, but allows arbitrary calculation of keys and values from | |
source coll, (for instance, to roll up distinct particular elements of a | |
vector by distinct other parts) and arbitrary combinations of old and new | |
vectors (for instance, to perform an efficient online count). | |
(flexigroup identity identity conj [] coll) == group-by | |
(flexigroup identity identity (fn [a b] (inc a)) 0) == online count | |
(flexigroup #(subvec % 0 1) #(subvec % 1) conj #{} coll) == all the | |
distinct rests of the vectors, grouped by the first element" | |
[key-f val-f combine-f init-v coll] |
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
;dummy.csv is of format "1,2,3\n4,5,6\n" | |
;fails since everything is lazy (but works if you do something that forces resolution) | |
(with-open [r (reader "file:///home/brycen/dummy.csv")] | |
(for [lines (read-csv r)] | |
(zipmap [:x :y :z] lines))) | |
;Doesn't close "properly" (but works most of the time) | |
(for [lines (read-csv (reader "file:///home/brycen/dummy.csv"))] | |
(zipmap [:x :y :z] lines)) |