This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |