Skip to content

Instantly share code, notes, and snippets.

tax_payable = (band_info, gross) ->
tax_in_band = (band) ->
band.rate * (Math.min(band.max, gross) - Math.min(band.min, gross))
total = 0
bands = for band in band_info
prev_total = total
total += band.width + 0
{ width: band.width, rate: band.rate, min: prev_total, max: total }
@mk270
mk270 / latin2utf.c
Created April 12, 2012 14:27
Convert ISO-8859-1 to UTF-8 with constant RAM (cf iconv)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX 4096
void die(const char *s) {
perror(s);
exit(1);
}
@mk270
mk270 / horizontal_accountability.ml
Created July 19, 2012 16:00
Modelling horizontal accountability / checks and balances in OCaml
type branch =
| Legislative
| Executive
| Judicial
| Other
type origin =
| Elected
| Appointed
@mk270
mk270 / mysql_with_ftwrl.rb
Created July 20, 2012 14:02
CLI wrapper for "FLUSH TABLES WITH READ LOCK"; usage mysql_with_ftwrl.rb do_snapshot --db=name
#!/usr/bin/ruby
require 'dbi'
db_name = 'db_test'
db_user = 'username'
db_pass = 'pass1234'
db_host = '127.0.0.1'
exit_status = nil
@mk270
mk270 / sql2amqp.py
Created July 25, 2012 17:04
Shortcut to dump SQL query dictionaries to AMQP
#!/usr/bin/env python
import oursql
import pika
import json
import sys
db_config = {"db":"dbname", "user":"myuser", "passwd":"mypasswd"}
def dispatch(queue_name, query):
@mk270
mk270 / console
Created August 4, 2012 16:17
dyntype compilation problem
mk270@marsilio ~> opam install dyntype
Install: dyntype.0.8.2
[dyntype.0.8.2] Build commands:
make
make install
[21570] WARNING: Command "make" failed (see /home/mk270/.opam/log/command960.{info,err,out})
ocamlbuild dyntype.cmxa dyntype.cma
/usr/local/bin/ocamlopt.opt -I /usr/local/lib/ocaml/ocamlbuild unix.cmxa /usr/local/lib/ocaml/ocamlbuild/ocamlbuildlib.cmxa myocamlbuild.ml /usr/local/lib/ocaml/ocamlbuild/ocamlbuild.cmx -o myocamlbuild
ocamlfind ocamldep -package type_conv -modules lib/type.mli > lib/type.mli.depends
ocamlfind ocamlc -c -package type_conv -I lib -I pa_lib -I lib_test -o lib/type.cmi lib/type.mli
@mk270
mk270 / batched_generator.py
Last active March 16, 2021 14:27
batched generator for Python
import itertools
def batch_generator(size, g):
buffer = []
try:
while True:
for i in range(0, size):
buffer.append(g.next())
yield buffer
buffer = []
@mk270
mk270 / ocaml_package_helloworld
Created December 20, 2012 23:06
Basic guide to packaging OCaml code with current tools. (current ~= December 2012)
When making and publishing an OCaml programme or library, there are several
tools you might use to compile and package and distribute your work. Building
OCaml projects is a bit different from C or similar languages, so the usual
"./configure && make && make test && make install" convention doesn't work.
As of December 2012, the recommended tools for building an OCaml package are
Oasis and OPAM. This document assumes you are using a UNIX-like system.
OPAM is responsible for installing new OCaml packages and libraries, such
as the dependencies of your own project. Oasis helps automate the build
system for your project.
@mk270
mk270 / compare_explain_update
Created January 5, 2013 14:56
compare EXPLAIN UPDATE
without indices
Hash Join (cost=13973.56..43799.18 rows=31766 width=583)
Hash Cond: ((p."globalSchemeId")::text = (s."GlobalSchemeId")::text)
-> Hash Join (cost=13963.11..43034.28 rows=31766 width=579)
Hash Cond: ((p."countryPayment")::text = (c.code)::text)
-> Hash Join (cost=13955.08..42271.81 rows=31766 width=575)
Hash Cond: ((p."globalRecipientId")::text = (r."globalRecipientId")::text)
-> Seq Scan on payment p (cost=0.00..18317.68 rows=860568 width=571)
-> Hash (cost=13784.48..13784.48 rows=13648 width=13)
@mk270
mk270 / gist:4988309
Created February 19, 2013 18:06
OpenEverything
sed 's/\(.*\)/open\1/' < /usr/share/dict/words