Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
mlhaufe / uuid7.ts
Created June 30, 2024 17:37
Create uuid7()
function xuuid7(): string {
const randomUUID = crypto.randomUUID();
const randomUUIDArray = uuidToBytes(randomUUID);
// Get the current timestamp in milliseconds
const timestamp = Math.floor(Date.now());
// Convert the timestamp to a byte array
const timestampBytes = new Uint8Array(8);
const timestampView = new DataView(timestampBytes.buffer);
@mlhaufe
mlhaufe / expression-problem.mts
Created September 8, 2023 00:42
Trivial OO Expression Problem Solved in TypeScript
// See the following for a Scala example: <https://i.cs.hku.hk/~bruno/papers/Modularity2016.pdf>
/* Initial code */
abstract class Exp { abstract evaluate(): number }
class Lit extends Exp {
constructor(readonly x: number){ super(); }
evaluate(): number { return this.x }
}
class Add extends Exp {
constructor(readonly left: Exp, readonly right: Exp) { super() }
@mlhaufe
mlhaufe / callcc.js
Created July 13, 2023 22:03
callcc
class ContError extends Error { returnValue = undefined; }
function callcc(fnCont) {
const contErr = new ContError("Unable to continue current continuation.");
const fnEscape = (returnValue) => {
contErr.returnValue = returnValue;
throw contErr;
}
try {
return fnCont(fnEscape);
@mlhaufe
mlhaufe / delimited-continuations.js
Created July 13, 2023 21:41 — forked from yelouafi/delimited-continuations.js
delimited continuations using javascript generators
// We model the call stack using a linked list of Generators
// Each Generator has a _return field pointing back to its parent
function stepGen(gen, arg) {
const {done, value} = gen.next(arg)
if(done) {
if(gen._return) {
stepGen(gen._return, value)
}
@mlhaufe
mlhaufe / TypeLevel.ts
Last active June 10, 2023 16:18
TypeScript Union and Intersection types
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type IntersectionToUnion<I> = (I extends any ? (x: I) => any : never) extends ((x: infer U) => any) ? U : never;
/**
* Zip two tuples together into a tuple of tuples
* @example
* ZipTuple<['name', 'age', 'isActive'], [string, number, boolean]>
* => [["name", string], ["age", number], ["isActive", boolean]]
*/
@mlhaufe
mlhaufe / islanders.ts
Created May 5, 2022 01:42
12 islanders balancing problem
// Random Integer from m to n excluding e
const randInt = (m: number, n: number, e?: number): number => {
const result = Math.floor(Math.random() * m) + n;
return e == undefined ? result :
result == e ? randInt(m, n, e) :
result
}
class Person { constructor(public weight: number) {} }
@mlhaufe
mlhaufe / Functional.ts
Created April 16, 2022 03:37
Expression Problem
/** Number Expressions */
// data
type Exp =
{ tag: 'Lit', value: number } |
{ tag: 'Add', left: Exp, right: Exp }
// operations
function evaluate(exp: Exp): number {
switch (exp.tag) {
@mlhaufe
mlhaufe / error.txt
Created May 24, 2021 13:57
Magnolia SPAv2 error
Context: <https://groups.google.com/a/magnolia-cms.com/g/user-list/c/MRpHYKkOMhA>
Type Exception Report
Message com.machinezoo.noexception.WrappedException: java.net.ConnectException: Connection refused (Connection refused)
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
@mlhaufe
mlhaufe / cyclicPerms.js
Last active May 12, 2021 14:07
Permutations
// cyclicPerms(5)
// "1,2,3,4,5
// 2,3,4,5,1
// 3,4,5,1,2
// 4,5,1,2,3
// 5,1,2,3,4"
const cyclicPerms = (n) =>
Array.from({length: n},(_,i) => i + 1)
.map((_, i, xs) => [...xs, ...xs].slice(i, i+n))
.join('\n')
@mlhaufe
mlhaufe / bool.ts
Created July 21, 2020 19:13
Boolean control structures
interface Boolean {
ifTrue<T>(fn: () => T): T | undefined
ifFalse<T>(fn: () => T): T | undefined
}
Object.assign(Boolean.prototype, {
ifTrue(fn: Function) {
return this ? fn.apply(this) : undefined
},
ifFalse(fn: Function) {