Skip to content

Instantly share code, notes, and snippets.

@voltrevo
voltrevo / PromiseMap.js
Created September 25, 2015 08:08
PromiseMap
'use strict';
// core modules
var assert = require('assert');
module.exports = function(map) {
var keys = Object.keys(map);
return Promise.all(keys.map(function(mapKey) {
return map[mapKey];
@voltrevo
voltrevo / topologies.txt
Created October 20, 2015 02:45
hexagonal topologies
I - J - K - L - G - H - I
/ \ / \ / \ / \ / \ / \ / \
F - A - B - C - D - E - F - A
\ / \ / \ / \ / \ / \ / \ / \
L - G - H - I - J - K - L - G
\ / \ / \ / \ / \ / \ / \ /
D - E - F - A - B - C - D
F - G - H - E - F
@voltrevo
voltrevo / createMap.js
Last active October 21, 2015 06:40
A generalized map in ES5 based on linear lookup on strict comparison
'use strict';
var assert = require('assert');
module.exports = function(createDefaultValue) {
var map = {};
var keyValuePairs = [];
var lookup = function(key) {
var loadScript = function(src) {
return new Promise(function(resolve, reject) {
var scriptTag = document.createElement('script');
scriptTag.src = src;
document.body.appendChild(scriptTag);
scriptTag.addEventListener('load', function() {
console.log('loaded', src);
resolve();
@voltrevo
voltrevo / fib.js
Last active November 17, 2015 23:18
bignum fibonacci
'use strict';
var bignum = require('bignum');
var fib = function(n) {
var a = bignum(0);
var b = bignum(1);
for (var i = 1; i < n; i++) {
a = a.add(b);
var magicRequire = function(moduleName) {
return new Promise(function(resolve, reject) {
var scriptTag = document.createElement('script');
scriptTag.src = 'https://www.brcdn.org/' + moduleName + '/latest';
document.body.appendChild(scriptTag);
scriptTag.addEventListener('load', function() {
if (!window.require) {
reject(new Error('require function not found'));
'use strict';
module.exports = function(loop) {
return (function cycle(result) {
return result || loop().then(cycle);
})();
};
const enumerateChoices = (sets) => {
if (sets.length === 0) {
return [[]];
}
const subChoices = enumerateChoices(sets.slice(1));
return [].concat(...sets[0].map(
choice => subChoices.map(choices => [choice].concat(choices))
));
reduce = [init, arr, combine] => match(arr) {
[head, tail...] => (
reduce([combine([init, head]), tail, combine])
);
[] => (
init
);
};
@voltrevo
voltrevo / threePrisoners.js
Created March 12, 2016 09:36
Computer assisted proof for a 2+/3 prisoner survival strategy for https://www.youtube.com/watch?v=7hJ4Azr--s8
'use strict';
const assert = (x) => {
if (!x) {
throw new Error('Assertion failure');
}
};
const range = (n) => (new Array(n)).fill(0).map((x, i) => i);