Skip to content

Instantly share code, notes, and snippets.

@mjwillson
mjwillson / iterable.py
Last active August 29, 2015 14:27
Decorate a generator function (or other iterator-returning function) as a multi-shot iterable. A fix for many Python gotchas relating to use of one-shot iterators
class iterable(object):
"""Decorates a generator function (or any other iterator-returning
function) as something which implements the iterable protocol and
can be safely passed to other code which may iterate over it
multiple times.
Usage:
@iterable
def foo():
@mjwillson
mjwillson / equivalence_relation.rb
Created November 13, 2008 17:05
Simple Ruby class for an extensionally-specified Equivalence Relation with fast lookup
require 'set'
class EquivalenceRelation
def initialize
@equivalence_class_by_member = {}
end
def declare_equivalent(*items)
combined_equivalence_class = items.map {|item| equivalence_class_of(item)}.inject(Set.new) {|accum,set| accum.union(set)}
combined_equivalence_class.each {|member| @equivalence_class_by_member[member] = combined_equivalence_class}
end
@mjwillson
mjwillson / yaml_fixtures.rb
Created November 20, 2008 23:55
Some general-purpose fixture-loading code which supports references and respects dependencies. Quite short as it hooks into yaml.rb to do most of the work
# This is a mixin for Test::Unit::TestCase, for providing tests with a simple facility to load fixtures from
# YAML files. It aims to do as much as possible in a generic way with the YAML library rather than couple
# tightly to a database library.
#
# Fixtures can use arbitrary YAML to represent ruby objects, using !ruby/object:Some::ClassName where needed.
# you will typically need to implement yaml_initialize on the relevant class, in order for this to
# work in the way you desire (we do so here for Sequel::Model; others including ActiveRecord shouldn't be hard)
#
# Data within one fixture can refer to other fixtures by file and name, using the syntax: !fixture file/name.
# this is achieved by adding a special YAML domain type.
@mjwillson
mjwillson / tax_bands.hs
Created October 29, 2009 22:35
Pure functional tax bands
infinity = 1.0/0
savingsIncomeTax200910 = [(0, 6475), (0.1, 8915), (0.2, 43875), (0.4, infinity)]
incomeTax200910 = [(0, 6475), (0.2, 43875), (0.4, infinity)]
taxWithBands bands income
= if income <= threshold then
rate * income
else
rate * threshold + taxWithBands loweredRemainingBands remainingIncome
where
@mjwillson
mjwillson / tax_bands.rb
Created October 29, 2009 22:41
Same thing in ruby
Infinity = 1.0/0
SAVINGS_INCOME_TAX_200910 = [[0, 6475], [0.1, 8915], [0.2, 43875], [0.4, Infinity]]
INCOME_TAX_200910 = [[0, 6475], [0.2, 43875], [0.4, Infinity]]
def tax(income, bands=INCOME_TAX_200910)
rate, threshold = bands.first
if income <= threshold
rate * income
else
lowered_remaining_bands = bands[1..-1].map {|r, t| [r, t - threshold]}
@mjwillson
mjwillson / gist:675cc0259e4291d97104
Created September 7, 2015 17:55 — forked from benanne/gist:1759022
Theano AdvancedSubtensor memory leak
import theano.tensor as T
import theano
import numpy as np
import gc
def freemem():
gc.collect()
gc.collect()
gc.collect()
return theano.sandbox.cuda.cuda_ndarray.cuda_ndarray.mem_info()[0] / 1024**2
@mjwillson
mjwillson / multimethod-matrix-multiplication.clj
Last active December 10, 2015 17:58
An example of how multi-method-based dispatch might work for a binary operation like matrix multiplication. Illustrates how a variety of coercion-based defaults can be specified to make life easy on the implementer, while still easily allowing for dispatch to optimal implementation-specific routines whenever it's desired.
;; First some dummy matrix types and operations for us to play with later:
(deftype GenericDense [])
(deftype FooMatrix [])
(deftype BarMatrix [])
(defn generic-multiply [x y] "generic-multiply")
(defn foo-multiply [x y] "foo-multiply")
(defn bar-multiply [x y] "bar-multiply")
(defn foo-generic-multiply [x y] "foo-generic-multiply")
(defn generic-foo-multiply [x y] "generic-foo-multiply")
(defn bar-generic-multiply [x y] "bar-generic-multiply")
@mjwillson
mjwillson / mapreduce.clj
Last active December 16, 2015 01:19
Clojure-like psuedo-code for something closer to the actual structure of a Hadoop MapReduce computation
;; I'm sure I'm wrong here -- please correct me!
;; (Although note I'm not trying to capture the exact algorithms and dataflow complexities of hadoop here, just the logical structure of MapReduce computations and a rough sketch of how they're distributed.)
;; not just
(->> data
(pmap mapper)
(reduce reducer))
;; but something more like this.
@mjwillson
mjwillson / Encoding.java
Created April 30, 2013 16:33
convert between float arrays and byte arrays
package randomindexing;
/* Helpers for encoding various primitive arrays as byte arrays.
(Why this isn't in the stdlib I have no idea! feel free to replace
with some existing library implementation...)
All encodings use little-endian byte order.
I benchmarked using
ByteBuffer.wrap(b).order(LITTLE_ENDIAN).asFloatBuffer().get(f),
but it's around 3 times slower even on big input.
@mjwillson
mjwillson / sexpr-html-templating.clj
Last active December 17, 2015 12:49
Proof of concept: S-expressions for HTML templating in clojure
(defprotocol ToHtml
(to-html [x]))
(extend-protocol ToHtml
String
(to-html [s]
(clojure.string/escape s {\< "&lt;" \> "&gt;" \" "&quot;" \& "&amp;"}))
clojure.lang.IPersistentMap
(to-html [attrs]