Skip to content

Instantly share code, notes, and snippets.

View qntm's full-sized avatar

qntm qntm

View GitHub Profile
@qntm
qntm / high-water-mark-roman.js
Last active January 18, 2020 19:02
What is the smallest number which, when expressed in Roman numerals, won't fit in a 280-character Tweet?
// <https://twitter.com/Revolvolutionry/status/1165616879009816576>
// <https://oeis.org/A036746>
const { arabicToRoman, romanToArabic } = require('big-roman')
// The entry at index `i` in this array is the smallest Roman numeral
// of length `i`
const results = ['']
const stopAt = 281
let powerOfTen = 0
@qntm
qntm / roman.js
Last active June 17, 2019 19:50
An extremely spare Roman numerals converter
// No bounds/type checking
// No support for the overbar extension for numbers past 3999
const banks = [
['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
['', 'M', 'MM', 'MMM']
]
@qntm
qntm / quine.js
Last active October 16, 2023 12:50
There are others like it, but this one is quine
(s => console.log(`${s}\n(${JSON.stringify(s)})\n`))
("(s => console.log(`${s}\\n(${JSON.stringify(s)})\\n`))")
@qntm
qntm / index.js
Last active September 15, 2020 20:14
Promises/A+ implementation in 300 bytes
let P=exports.P=(t,e,r=t=>typeof t==typeof P,c,o=t=>c=t,h=(t,e)=>{try{e=t(e),l==e?c():e===Object(e)&&r(t=e.then)?t.call(e,t=>e&&h(e=>t,e=0),t=>e&&o([t],e=0)):o([e,1])}catch(t){o([t])}},l={r:(c,P)=>setTimeout(l=>r(P=c[1]?t:e)?h(P,c[0]):o(c)),then:(t,e,r=P(t,e),h=o)=>(c?r.r(c):o=t=>r.r(t,h(t)),r)})=>l
@qntm
qntm / doubleEquals.js
Last active November 18, 2017 20:33
An implementation of the JavaScript `==` operator without using `==` itself.
'use strict'
// <http://www.ecma-international.org/ecma-262/5.1/#sec-8> Type
var type = function (x) {
// The `typeof` operator gives us almost what we want
return (
typeof x === 'function' ? 'object'
: x === null ? 'null'
: typeof x
)
@qntm
qntm / variadic.js
Last active June 5, 2017 01:19
Variadic fixed point combinators? They're more likely than you think
/**
Analogous to Array.prototype.map but for objects. If you
REALLY don't want to modify Object.prototype you can
modify this into a regular function `objectMap(obj, f)` I GUESS
but it doesn't affect the basic idea of what happens below
*/
Object.prototype.map = function(f) {
const mapped = {};
Object.keys(this).forEach(key => {
mapped[key] = f(this[key], key, this);
@qntm
qntm / daylengths.js
Created November 17, 2016 19:54
Which day was the shortest?
"use strict";
var tai = require("t-a-i");
var prevlen = undefined;
var prevcount = 0;
for(var d = Date.UTC(1961, 0, 1); d < Date.UTC(2017, 0, 1); d += 86400000) {
var d2 = d + 86400000;
var len = tai.unixToAtomic(d2) - tai.unixToAtomic(d);
len = Math.round(len * 10000) / 10000;
@qntm
qntm / yearlengths.js
Last active July 5, 2019 19:10
Which year was the longest?
"use strict"
const tai = require("t-a-i")
for (let y = 1961; y <= new Date().getFullYear(); y++) {
const start = tai.unixToAtomic(Date.UTC(y, 0, 1))
const end = tai.unixToAtomic(Date.UTC(y + 1, 0, 1))
console.log(y, end - start)
}
@qntm
qntm / indent.py
Last active February 28, 2022 04:07
Fibonindentation! Works in Python!
if True:
pass
if True:
if True:
if True:
if True:
if True:
if True:
if True:
print("yup, syntactically valid Python")
@qntm
qntm / STag.regex
Created January 15, 2016 23:28
RegEx match open tags except XHTML self-contained tags
<[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF][\-.0-9:A-Z_a-z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0300-\u036F\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF]*([\u0020\u0009\u000D\u000A]+[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF][\-.0-9:A-Z_a-z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0300-\u036F\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF]*[\u0020\u0009\u000D\u000A]*=[\u0020\u0009\u000D\u000A]*("([^<&"]|&([:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\U