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 prim [graph] | |
(let [sortedgraph (sort-by #(nth % 2) graph) | |
startv (->> graph (map #(take 2 %)) flatten set)] | |
((fn [v e] | |
(if (= v startv) (reduce + (map #(nth % 2) e)) | |
(let [minedge (first (filter (fn [e] (= 1 (count (filter identity (map #(v (nth e %)) '(0 1)))))) sortedgraph))] | |
(recur (apply conj v (take 2 minedge)) (conj e minedge))))) | |
#{(ffirst graph)} #{}))) | |
(def g [['a 'b 7] ['a 'd 5] ['b 'c 8] ['b 'e 7] ['b 'd 9] ['c 'e 5] ['d 'e 15] ['d 'f 6] ['e 'g 9] ['e 'f 8] ['f 'g 11]]) |
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 playground.eightpuz) | |
(defn correct [b] (= b [1 2 3 8 0 4 7 6 5])) | |
(def dirs {'l -1 'r 1 'u -3 'd 3}) | |
(defn move [b dir] | |
(let [idx (.indexOf b 0) | |
col (mod idx 3) | |
tgt (+ idx (dirs dir))] | |
(if |
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 algo2.unionfind) | |
(defprotocol DisjointSet | |
"A data structure that maintains informations on a number of disjoint sets." | |
(add-singleton [this x] "Add the element x to a new singleton set") | |
(connect [this x y] "Union the sets that x and y are in") | |
(get-canonical [this x] "Return the canonical element of the set x is in")) | |
(defrecord UFNode [value rank parent]) |
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
(defprotocol IFoo | |
(foo [this x]) | |
(bar [this x y])) | |
(deftype Foo [avec] | |
IFoo | |
(foo [this x] (.Foo (conj avec x))) | |
(bar [this x y] | |
(let [newfoo (foo this x)] | |
(.Foo (conj (.avec newfoo) y))))) |
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
static class ExceptionTranslatingForwardingInvocationHandler implements InvocationHandler { | |
private final ImmutableMap<Class<? extends Throwable>, Class<? extends Throwable>> exceptionMap; | |
private final Object delegate; | |
ExceptionTranslatingForwardingInvocationHandler( | |
Map<Class<? extends Throwable>, Class<? extends Throwable>> exceptionMap, | |
Object delegate) { | |
this.exceptionMap = ImmutableMap.copyOf(exceptionMap); |
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
Entity model: | |
- There exist pastures and cows | |
- Pastures have many cows | |
- Every cow has exactly one pasture | |
- Pastures have 2 unique ids a piece - one that rancher A identifies them by, one that rancher B identifies them by | |
Queries required: | |
- given a cow id, look up its type-A pasture id. | |
- given a type-A pasture id, look up its type-B pasture id. |
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 combinations [options n] | |
(let [syms (into [] (repeatedly n gensym)) | |
for-bindings (into [] (interleave syms (repeat options)))] | |
(eval `(for ~for-bindings ~syms)))) |
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
static class Foo { | |
public boolean member = true; | |
} | |
@Test | |
public void testReflectionAllocation() throws Exception { | |
Foo foo = new Foo(); | |
IdentityHashMap<Boolean, Void> set = new IdentityHashMap<Boolean, Void>(); | |
for (int i = 0; i < 100; i++) { |
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
### Keybase proof | |
I hereby claim: | |
* I am JordanLewis on github. | |
* I am jordanlewis (https://keybase.io/jordanlewis) on keybase. | |
* I have a public key whose fingerprint is B2E4 8028 65BC 67D1 1391 DB3A 5ED3 5C78 CB78 4169 | |
To claim this, I am signing this object: |
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
hodor |