Skip to content

Instantly share code, notes, and snippets.

View jordanlewis's full-sized avatar
👀
large data banking

Jordan Lewis jordanlewis

👀
large data banking
View GitHub Profile
@jordanlewis
jordanlewis / gist:4230827
Created December 7, 2012 04:48
Prim's algorithm
(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]])
@jordanlewis
jordanlewis / gist:4233931
Created December 7, 2012 15:25
8-puzzle
(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
@jordanlewis
jordanlewis / gist:4392484
Created December 27, 2012 22:04
Initial stab at persistent union find in clojure
(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])
(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)))))
@jordanlewis
jordanlewis / gist:4741829
Created February 8, 2013 20:52
InvocationHandler that translates between exception types... probably not useful
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);
@jordanlewis
jordanlewis / modeling-cows.txt
Last active December 12, 2015 12:48
Cassandra data model: 1 or 2 column families?
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.
(defn combinations [options n]
(let [syms (into [] (repeatedly n gensym))
for-bindings (into [] (interleave syms (repeat options)))]
(eval `(for ~for-bindings ~syms))))
@jordanlewis
jordanlewis / ReflectionAllocationTest.java
Last active December 22, 2015 03:59
Extra boolean allocations via reflection
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++) {
### 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: