Skip to content

Instantly share code, notes, and snippets.

// Space out a series of function calls to some minimum duration.
//
// We accomplish this by first providing a way to convert a series of function calls
// into an async iterable using `asyncGeneratorOf`.
// The resulting object also has an `add` method which is what the user calls
// to add items to the iterable.
// It then becomes a simple matter to write `spaceOut`, which simply inserts
// a wait inside the `for await` loop.
@rtm
rtm / chunked-memcache-pseudocode.txt
Created May 25, 2016 07:59
chunked version of memcached
// Wrap memcached to chunkify long values ("blobs").
// Write individual chunks using a key constructed from a unique GUID and chunk number.
// Store the GUID and the number of chunks as the value of the underlying key.
//
// We assume that memcached.read and memcached.write are asynchronous and return promises.
// We also define asynchonous assemble and disassemble routes.
//
// The behavior of this code is that when a key is read,
// even if another client starts writing a new value for the
// key, the previous value will be returned.
// Find the length of the longest monotonically increasing or monitonically
// decreasing sequence of values in an array.
function longest_run(a) {
const less = (a, b) => a < b;
const more = (a, b) => a > b;
const cum = (a, f) => a.map((v, i) => f(v, a[i-1])); // compare adjacent elements
const sum = (a, t = 0) => a.map(v => t = v ? t + v : 0); // running sum, reset on zero
return !a.length ? 0 :
@rtm
rtm / find.js
Last active March 24, 2016 11:03
function *find(total, count, [val, ...remaining]) {
if (!count && Math.abs(total) < 0.001) yield [];
if (!count || total < 0 || !val) return;
for (let i = 0; i <= count; i++, total -= val)
for (let counts of find(total, count - i, remaining))
yield [i, ...counts];
}
for (let solution of find(100, 100, [3.44, 2.87, 1.99, 0.59]))
@rtm
rtm / currify.js
Last active April 28, 2020 03:24
Currify a function
// Transform a function into one which takes partial arguments,
// returning a function which can be invoked with the remaining ones.
function currify(f) {
var len = f.length;
return function _f() {
var old_args = arguments;
return function() {
var args = [];
@rtm
rtm / deep-equals.js
Last active April 4, 2017 15:33
JS deep equals
function deepEquals(x, y) {
if (x === y) return true;
if (x !== x) return y !== y;
if (typeof x !== typeof y) return false;
if (typeof x !== 'object') return false;
var typeX = getType(x);
var typeY = getType(y);
if (typeX !== typeY) return false;
@rtm
rtm / Myers.js
Last active December 13, 2015 17:08
Myers-style inheritance for JavaScript ES5. Simple, elegant, functional semi-classic implementation of inheritance.
// Class is a meta-class, whose instances are other classes.
// Therefore, to create a class, instantiate Class with A=Class.new().
// To instantiate a class inheriting from another class, use B=Class.new(A).
// After creating a class, optionally assign a constructor with A.ctor=function(){}.
// Within constructor, this.super(...) access constructor of parent class.
// To specify a template for instances of the class, assign to A.proto.
// To instantiate a class, a=A.new(), or specify arguments to be passed to constructor.
//
// Myers-style inheritance as implemented here attempts to be as lightweight as possible.
// It limits itself to the bare necessities.