Skip to content

Instantly share code, notes, and snippets.

View puredanger's full-sized avatar
😀
Clojure is life

Alex Miller puredanger

😀
Clojure is life
View GitHub Profile
@puredanger
puredanger / breezy.clj
Last active August 29, 2015 14:05 — forked from quephird/breezy.clj
Added type hints I suspect would make math faster here. Just eyeballing, so not sure if it's all right. Normally I would check the bytecode to look for boxing as well.
(ns fun-with-quil.breezy
(:use quil.core))
(set! *unchecked-math* true)
(def ^:constant screen-w 1920)
(def ^:constant screen-h 1080)
(defn r ^double [^long x ^long y]
(loop [k 0 i 0.0 j 0.0)]
@puredanger
puredanger / MultiFn-after.java
Last active August 29, 2015 14:11
MultiFn.findAndCacheBestMethod()
private IFn findAndCacheBestMethod(Object dispatchVal) {
rw.readLock().lock();
Object bestValue; // remember the best match IFn
IPersistentMap mt = methodTable;
IPersistentMap pt = preferTable;
Object ch = cachedHierarchy;
try
{
Map.Entry bestEntry = null;
for(Object o : getMethodTable())
@puredanger
puredanger / gist:40dd3ec22fdb5836d1eb
Created December 18, 2014 15:13
Disassembled code
This file has been truncated, but you can view the full file.
Compiled from "eclipse.clj"
public final class ccw.eclipse$goto_editor_line extends clojure.lang.AFunction {
public static final clojure.lang.Var const__0;
public static final clojure.lang.Var const__1;
public static final clojure.lang.Keyword const__2;
public static final clojure.lang.Keyword const__3;
@puredanger
puredanger / gist:83f66f157f56bc9db845
Created July 19, 2015 14:28
reduce / mapv tuples
compare perf of reduce and mapv on small vectors - testing worst case of size 6
## 1.7.0
vecperf.bench=> (def v [0 1 2 3 4 5])
#'vecperf.bench/v
vecperf.bench=> (quick-bench (reduce + 0 v))
WARNING: Final GC required 1.508801076780115 % of runtime
WARNING: Final GC required 13.47335649374595 % of runtime
Evaluation count : 11114412 in 6 samples of 1852402 calls.
; What function can I put as FOO here to yield true
; Tried: hash-set, conj, concat
(defn mergeMatches [propertyMapList]
"Take a list of maps and merges them combining values into a set"
(reduce #(merge-with FOO %1 %2) {} propertyMapList))
(def in
(list
{:a 1}
@puredanger
puredanger / ByteInputStream.clj
Created September 10, 2015 22:03
Example of using gen-class to subclass a class with multi-arity method
(ns example.ByteInputStream
(:gen-class
:extends java.io.InputStream
:state byteseq
:init init
:constructors {[clojure.lang.Seqable] []}
:exposes-methods {read readSuper}
:main false))
(defn -init [byte-seq]
@puredanger
puredanger / TwoOfAType.java
Created December 10, 2010 19:28
Example of using a parameter on a constructor.
/**
Example of using a parameter on a constructor. Parameters on methods are used to
describe either type relationships between arguments to the method or between the
argument and the return type (more common but not possible in a constructor). Here
I use the type X to specify that the two args are of the same type.
When calling it, specifying the type is optional as it will automatically be
inferred. In cases where the incorrect type is inferred (wrong place in shared
hierarchy), you can specify it. If there is no common type, you'll get an error
at compile time.
@puredanger
puredanger / map.clj
Created February 25, 2011 02:30
Map creators
(defn mapmap
"Apply kf and vf to a sequence, s, and produce a map of (kf %) to (vf %)."
([vf s]
(mapmap identity vf s))
([kf vf s]
(zipmap (map kf s)
(map vf s))))
(defn mapmapmap
"Apply kf and vf to the keys and vals of a map and zip the results."
@puredanger
puredanger / gist:3540715
Created August 30, 2012 20:54
tests for sub=
(deftest test-sub=
(are [expected left right] (= expected (sub= left right))
;; prim
true 1 1
false 1 2
;; maps
true {} {:a 1}
false {:a 1} {}
true {:a 1} {:a 1}
false {:a 1} {:a 2}
@puredanger
puredanger / gist:3552458
Created August 31, 2012 13:07
sub= defn
(defn sub=
"Checks that all keys and vals in m1 are in m2 (but m2 can have extra stuff)"
[o1 o2]
(if (and (map? o1) (map? o2))
(every? identity (map (fn [[k v]] (sub= v (k o2))) o1))
(= o1 o2)))