Skip to main content

Gists

splitmix32.ts
export function splitmix32(seed: number):
  () => number /* [0, 1) */ {
    let state = seed | 0;
    return () => {
      state |= 0;
      state = state + 0x9e3779b9 | 0;
      let t = state ^ state >>> 16;
      t = Math.imul(t, 0x21f0aaad);
typescript

Splitmix32: A Fast Seeded PRNG

A 32-bit seeded pseudorandom number generator derived from MurmurHash3's finalizer. Deterministic, fast, and good enough for simulations that need reproducibility — not cryptography.

union-to-intersection.ts
type UnionToIntersection<U> =
  (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void
    ? I
    : never

type Handlers =
  | { onClick: () => void }
  | { onKey: (key: string) => void }
typescript

Union to Intersection

Merge a union of object types into a single composite type.

xor.ts
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never }

type XOR<T, U> = (T & Without<U, T>) | (U & Without<T, U>)

type ById = { id: string }
type ByEmail = { email: string }

type Lookup = XOR<ById, ByEmail>
typescript

XOR Types for Mutually Exclusive Options

Enforce exactly one of two shapes at compile time.

speed-up-video.sh
SPEED=1.15
INPUT="input.mp4"
OUTPUT="output.mp4"

ffmpeg -i "$INPUT" \
    -filter_complex "[0:v]setpts=PTS/${SPEED}[v];[0:a]atempo=${SPEED}[a]" \
    -map "[v]" -map "[a]" \
    -c:v libx264 -preset fast -crf 22 \
bash

Speed Up Video with ffmpeg

Speed up or slow down video and audio tracks together using ffmpeg, with configurable speed multiplier and high-quality H.264 re-encoding.

kill-all-containers.sh
# Kill all running Docker containers, by sending
# them a SIGKILL signal.
docker ps -q \
  | xargs docker kill
bash

Kill All Running Docker Containers

Stop every running container in one shot with docker kill and xargs.