Skip to content

Instantly share code, notes, and snippets.

@mtth
mtth / instrument.js
Last active November 17, 2015 06:22
Instrument Avro types
/* jshint node: true */
'use strict';
var avsc = require('avsc');
/**
* Function returning an instrumented type.
*
* @param schema {Object} Schema to parse.
@mtth
mtth / js.py
Created September 12, 2015 17:20
Quine Python to JavaScript to Python
s = "s = %j; t = %j; print t % (t, s)"; t = "var util = require('util'); var s = %r; var t = %r; console.log(util.format(t, t, s));"; print t % (t, s)
@mtth
mtth / json_variable.py
Created September 11, 2015 15:09
JSON encoding with variable expansion
#!/usr/bin/env python
# encoding: utf-8
"""JSON serialization with value expansion."""
from json import JSONEncoder
import re
# Try 1.
def indices(m, n):
"""Index generator to visit a 2-dimensional matrix in increasing i + j.
:param m: Number of rows.
:param n: Number of columns.
"""
for i in range(m):
for j in range(min(n, i + 1)):
yield (i - j, j)
@mtth
mtth / DuplexIterable.js
Created February 24, 2015 01:12
Bidirectional REST iterable.
/* jshint browser: true, node: true */
(function (root) {
'use strict';
/**
* Bidirectional lazy loader.
*
* This class exposes a very simple API (`reset`, `next`, and `prev`) via
* which we can iterate (lazily and in both directions!) over a traditional
@mtth
mtth / LazyIterator.js
Last active August 29, 2015 14:15
Lazy asynchronous iterator
/* jshint browser: true */
(function (root) {
'use strict';
/**
* Simple lazy asynchronous iterator.
*
* Lazily iterate over a collection served from a standard REST endpoint.
* Elements are loaded by batch (of size `highWaterMark`), and
@mtth
mtth / batch.js
Created February 14, 2015 20:39
BatchReadable stream
var stream = require('stream'),
util = require('util');
/**
* Batch loaded readable stream.
*
* @param {Function} `fn(cb)`, where `cb(err, iter)`. Function used to feed
* the stream. `iter` should either be an iterator or `null` to signal EOT.
* @param {Object} `opts` Options forwarded to `stream.Readable`, along with
* the following: `batchHighWatermark`, the maximum number of batches
/**
* Benchmark functions serially.
*
* This is useful for async functions which yield too often to the event
* loop to be correctly benchmarked.
*
*/
function Benchmark() {
var fns = {};
@mtth
mtth / table.c
Created December 12, 2014 04:04
/**
* https://news.ycombinator.com/item?id=8737349
*
*/
#include <stdlib.h>
#define SIZE 1024
#!/usr/bin/env python
# encoding: utf-8
"""Python numbers magic!
https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/
Adapted for python 2.7.
"""