Skip to content

Instantly share code, notes, and snippets.

View pqnelson's full-sized avatar

Alex pqnelson

  • Unfold, Inc.
  • Los Angeles, CA
View GitHub Profile
@pqnelson
pqnelson / toy-obj.clj
Last active August 29, 2015 13:57
This is the relevant code snippets describing a toy object system in Clojure, taken from Amit Rathore's "Clojure in Action"
;; from Amit Rathore's "Clojure in Action", source code taken from his site, freely and legally avaible
;; inheritence
(declare this find-method)
(defn new-object [klass]
(let [state (ref {})]
(fn thiz [command & args]
(condp = command
:class klass
@pqnelson
pqnelson / euler-population.clj
Created April 6, 2014 19:46
A Monte Carlo analysis of a problem by Euler.
(ns euler-population.core)
;; "After the Flood, all people are descended from six people. If we
;; suppose that after 200 years the population had grown to 1,000,000,
;; then by what part must the population grow each year?"
;; — Leonhard Euler's "Introductio in Analysin Infinitorum", Ch. 6, example 3
(def initial-conditions {:men 3
:women 3})
@pqnelson
pqnelson / math_helper.clj
Last active August 29, 2015 13:58
Math helps
(ns math-helper
:require [clojure.set :as set-utils])
(defn float? [x] (instance? java.lang.Float x))
(defn double? [x] (instance? java.lang.Double x))
(defmacro defconst [const-name const-val]
`(def
~(with-meta const-name
(assoc (meta const-name) :const true))
// $ g++ -std=c++11 object.cpp
// $ ./a.out
// Object@0x00d01010
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip> // for setw, setfill
#include "types.hpp"
#include "object.hpp"
@pqnelson
pqnelson / number-thy.cpp
Created August 14, 2014 16:54
Pell numbers, Fibonacci numbers, memoized, and GCD...oh my...
#include <functional>
#include <iostream>
#include <string>
#include <map>
/*
* An example of some number theory calculations, done with dynamic
* programming.
*
* Partially inspired by http://programminggenin.blogspot.com/2013/01/memoization-in-c.html

Reader Macros in Common Lisp

Reader macros are perhaps not as famous as ordinary macros. While macros are a great way to create your own DSL, reader macros provide even greater flexibility by allowing you to create entirely new syntax on top of Lisp.

Paul Graham explains them very well in [On Lisp][] (Chapter 17, Read-Macros):

The three big moments in a Lisp expression's life are read-time, compile-time, and runtime. Functions are in control at runtime. Macros give us a chance to perform transformations on programs at compile-time. ...read-macros... do their work at read-time.

Macros and read-macros see your program at different stages. Macros get hold of the program when it has already been parsed into Lisp objects by the reader, and read-macros operate on a program while it is still text. However, by invoking read on this text, a read-macro can, if it chooses, get parsed Lisp objects as well. Thus read-macros are at least as powerful as ordinary macros.

% a simple lewis diagram of a phospholipid
numeric u;
u = 1pc;
beginfig(0)
% hydrophilic head
label(btex ${\rm CH}_{2}$ etex, (0,0));
draw (u, 0)--(1.75u,0);
label(btex ${\rm CH}$ etex, (2.5u, 0));
draw (3.25u, 0)--(4u, 0);
@pqnelson
pqnelson / fcmp.d
Last active August 29, 2015 14:19
Floating Point Comparison Helpers
import std.math;
// based on http://fcmp.sourceforge.net/
immutable real sqrtEpsilon = sqrt(real.epsilon); /* on x86, about 3.29272253991359623327e-10 */
int floatCompare(real x1, real x2, real epsilon = sqrtEpsilon) {
int exponent;
real delta;
real difference;

Tuning JVM GC for a big, mathy, STMful Clojure app

by Bryce Nyeggen

As part of my “real job”, I’m developing a Clojure-based app that does a significant amount of number crunching. The inner loop continuously ref-sets random portions of a large array of refs (when I say “large”, I mean that I can plausibly fire off a 50gb heap). I had a tough time getting it performant, and it’s an interesting enough story that I thought I’d relate it here.

After the standard futzing with algorithms and data structures, the thing that ended up holding me back was excessive time in GC. I started with the “throughput” collector (hey, I’m doing number crunching, I don’t have real-time requirements, throughput is awesome!). Somewhat surprisingly, I saw worse and worse performance as my app ran, ending in a kind of sawtoothed purgatory of GC. What little information I found about Clojure-specific GC tuning uniformly showed using the CMS / low-latency / concurrent collector as a good choice. Cu

@pqnelson
pqnelson / half_inning.clj
Last active August 29, 2015 14:24
Expected runs for number of successful plays in a half-inning
(ns half-inning)
(defn- int->play [i]
(let [i (mod i 5)]
(condp = i
0 :BB
1 :H1
2 :H2
3 :H3
4 :HR)))