Skip to content

Instantly share code, notes, and snippets.

@jcalz
jcalz / goldbachregex.js
Last active July 27, 2023 21:38
Regexes where it is not known whether they match anything
// I claim that the following regular expression should match a string if and only if
// it is composed of only the character "x", and
// its length is an even number greater than two which cannot be expressed as the sum of two prime numbers
// that is, if the length of the string is a counterexample to Goldbach's conjecture.
const r = /^(?!^(x*(?<!^(?:x?|\2+(xx+))))((?!(?:x?|(xx+?)\4+)$)x*)$)xx(xx)+$/;
console.log("start");
for (let i = 0; i < 1000; i++) {
const s = "x".repeat(i);
if (r.test(s)) console.log(i)
@jcalz
jcalz / makeTupleManipulation.js
Created October 24, 2019 17:18
Tuple manipulation boilerplate
var N = 30;
var a = Array(N).fill(undefined).map((_, i) => i);
var out = [];
out.push("type Add = [" + a.map(i =>
"[" + a.filter(j => (i + j) < N).map(j =>
"'" + (i + j) + "'"
).join(",") + "]"
).join(",") + "];");
out.push("type Sub = [" + a.map(i =>
@jcalz
jcalz / nullCoalescing.ts
Last active January 3, 2019 18:30
Workaround for lack of null-coalescing operator
interface NullSigil {
[k: string]: NullSigil;
}
// phantom property to recover T from NullSafe<T>
type OriginalTypeKey = "***originalType***"
type IsNullable<T, Y, N> = null extends T ? Y :
undefined extends T ? Y : N;
@jcalz
jcalz / star.js
Last active June 25, 2018 16:20
star clip-path
var format = x => ((x+.5)*100).toFixed(4)+'%';
var f = ([[a,b],[c,d]]) => format(a)+' '+format(b)+', '+format(c)+' '+format(d);
var r = 1.5 - 0.5*Math.sqrt(5);
var th = Math.PI/2;
var pn = 2*Math.PI/5;
var R = .5;
var result=[0,1,2,3,4].map(k =>
[[R*Math.cos(th+pn*(k-.5)),R*Math.sin(th+pn*(k-.5))],[r*R*Math.cos(th+pn*(k+0)),r*R*Math.sin(th+pn*(k+0))]]).map(f).join(', ');
console.log(result);
@jcalz
jcalz / arrayDestruct.ts
Last active February 9, 2018 16:49
Array destructuring helper (producing tuples up to length 9)
function arrayDestruct
(obj: any): never[];
function arrayDestruct<T, K0 extends keyof T>
(obj: T, k0: K0): [T[K0]];
function arrayDestruct<T, K0 extends keyof T, K1 extends keyof T>
(obj: T, k0: K0, k1: K1): [T[K0], T[K1]];
function arrayDestruct<T, K0 extends keyof T, K1 extends keyof T, K2 extends keyof T>
(obj: T, k0: K0, k1: K1, k2: K2): [T[K0], T[K1], T[K2]];
function arrayDestruct<T, K0 extends keyof T, K1 extends keyof T, K2 extends keyof T>
(obj: T, k0: K0, k1: K1, k2: K2): [T[K0], T[K1], T[K2]];
@jcalz
jcalz / jsonValidator.ts
Created August 16, 2017 01:51
TypeScript JSON validator
function defined<T>(x: T | undefined): x is T {
return typeof x !== 'undefined';
}
function hasKey<K extends string>(key: K, obj: any): obj is {[P in K]: any} {
return key in obj;
}
function mark<T>(): T {
return null! as T;
@jcalz
jcalz / tuple.md
Last active March 1, 2024 03:33
TypeScript tuple inference

You can use the tuple() function in tuple.ts to infer tuple types in TypeScript and cut down on the need to repeat yourself. Without tuple(), declaring a constant of a tuple type looks like this:

const daysOfTheWeek: ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] = 
  ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];

You can't do this:

const daysOfTheWeek = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]; 
@jcalz
jcalz / builderPattern.ts
Created July 25, 2017 14:44
Builder Pattern example code
type IBuilder<T> = {
[k in keyof T]: (arg: T[k]) => IBuilder<T>
} & { build(): T }
function proxyBuilder<T>(): IBuilder<T> {
var built: any = {};
var builder = new Proxy({}, {
get: function(target, prop, receiver) {
if (prop === 'build') return () => built;
return (x: any): any => {
@jcalz
jcalz / Collections.ts
Last active July 12, 2017 18:38
Map/Set implementations for js?
interface NumberConstructor {
isNaN(x: any): boolean
}
namespace Collections {
type Dictionary<T> = {
[k: string]: T
}
@jcalz
jcalz / dollarwords.txt
Created September 15, 2015 15:42
Dollar Words
acceptors
accumulate
acknowledge
acolytes
acquitted
activates
addressing
adiabatically
adulthood
aerofoils