Skip to content

Instantly share code, notes, and snippets.

View bnyeggen's full-sized avatar

Bryce Nyeggen bnyeggen

View GitHub Profile
@bnyeggen
bnyeggen / qseq.clj
Created September 1, 2012 14:00
Blocking queues to lazy seqs and vice versa
(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
@bnyeggen
bnyeggen / fn_protocols.clj
Created September 21, 2012 15:28
Functions can implement protocols too
(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
@bnyeggen
bnyeggen / bozo.clj
Created November 6, 2012 02:21
Rational bozo sort
(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)
@bnyeggen
bnyeggen / TombstoneBinarySearch.java
Created October 20, 2015 02:00
Binary search that considers magic value as not present
public class TombstoneBinarySearch {
public static int binarySearch(long[] a, int fromIndex, int toIndex, long key, long tombstone) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
final int origMid = mid;
long midVal = a[mid];
@bnyeggen
bnyeggen / Crypt.java
Created November 29, 2015 22:04
Stream based en/decryption in Java 7
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
@bnyeggen
bnyeggen / SizeOf.java
Last active December 10, 2015 13:08
Unsafe-based sizeof
// 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();
@bnyeggen
bnyeggen / hinttype.clj
Created February 2, 2013 21:13
Macro-generated deftype with type-hinted fields.
(defmacro hinttype
[type-name & hint-symbols]
`(deftype ~type-name
[~@(for [s hint-symbols]
(with-meta (gensym "a")
{:tag (case s
:int 'int
:long 'long
:float 'float
:double 'double
@bnyeggen
bnyeggen / RandomElementProvider.java
Created October 6, 2013 04:10
Threadsafe pseudo-shuffle - each caller gets a random element of the backing array, with the guarantee that each element will be selected at most once. The tradeoff is that as we approach all elements selected, we hit a slowdown as we're forced to scan for the remaining elements. For that reason, this is best suited to situations where we need a…
import java.util.Random;
import java.util.concurrent.atomic.AtomicIntegerArray;
public class RandomElementProvider {
//Flag value - not valid as an element of the backing array.
public static final int INVALID = Integer.MIN_VALUE;
//After this many failed random selections, devolve to a scan.
//Corresponds to being able to handle on average (1 - 1/RETRY_THRESHOLD) of elements
//being selected already before scanning
public static final int RETRY_THRESHOLD = 4;
@bnyeggen
bnyeggen / core.clj
Last active December 27, 2015 14:29
Running a Compojure app with a main function via java -jar
(ns myproject.core
(:use compojure.core)
(:require [compojure.route :as route]
[compojure.handler :as handler]
[ring.adapter.jetty :as ring-jetty])
(:gen-class))
(defroutes my-routes
(GET "/" [] "<h1>Hello World</h1>")
(route/not-found "<h1>Page not found</h1>"))
@bnyeggen
bnyeggen / geoull.cl
Created December 16, 2013 04:24
Geospatial recommendation kernel
/*
OpenCL code for a geospatial recommender. The underlying algorithm is:
- Each user has a set of XY points
- Similarity between user A and user B is defined as the inverse mean minimum distance to each of user A's points, from any of user B's points. This is not symmetric. E.g., if user A has points at (0,0) and (10,10), and user B has a point at (4,4), similarity of B to A will be 1 / ((sqrt(4^2 + 4^2) + sqrt(6^2 + 6^2)) / 2), and similarity of A to B will be 1 / (sqrt(4^2 + 4^2)). It is possible to define other similarity measures along the same lines (e.g. inverse mean minimum squared distance) that will give different results. We're all about the heuristics.
- For evaluating the predicted strength at a new XY location for a given main user,
- Calculate an inverse-square dropoff from every point (as if one was calculating gravity in an N-body simulation). Again, one could use some other exponent with better empirical foundation. This can be optimized by excluding points beyond a certa