Skip to content

Instantly share code, notes, and snippets.

View bergwerf's full-sized avatar

Herman Bergwerf bergwerf

View GitHub Profile
@bergwerf
bergwerf / vec.ts
Created March 22, 2025 23:02
Arbitrary numeric tensors in TypeScript
type Array_Element<A> = A extends (infer T)[] ? T : never
type Fixed_Array<A, X> =
[A] extends [[]] ? [] :
[A] extends [[X, ...infer T]] ? [X, ...Fixed_Array<T, X>] : never
type Vector<V> = Fixed_Array<V, Vector_Car<V>>
type Vector_Car<V> = [V] extends [number[]] ? number : Vector<Array_Element<V>>
class Vec<V extends Vector<V>> {
@bergwerf
bergwerf / sprintf.ts
Created May 6, 2024 09:49
sprintf in TypeScript
// Based on https://github.com/alexei/sprintf.js
//
// Copyright (c) 2007-present, Alexandru Mărășteanu <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
@bergwerf
bergwerf / loaders.scss
Created June 21, 2023 15:39
Animated loaders in Sass
@mixin droplet($color, $time, $size, $scale) {
position: relative;
&::after {
content: '';
display: inline-block;
width: $size;
height: $size;
border-radius: $size;
background: $color;
@bergwerf
bergwerf / server.py
Created May 31, 2023 16:39
Minimalistic server
import io
import json
import http.server
import socketserver
HOST = "localhost"
PORT = 8080
class AnalyzerHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
@bergwerf
bergwerf / cycles.glsl
Created January 29, 2022 16:24
Hue cycling over grayscale
// Try on: https://www.shadertoy.com/new
// With: https://github.com/andrewhills/ShadertoyCustomTextures
// https://www.shadertoy.com/view/XljGzV
vec3 hsl2rgb( in vec3 c )
{
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
}
@bergwerf
bergwerf / pigeons.v
Created November 10, 2020 11:07
Another attempt at an elegant pigeon-hole principle proof in Coq.
Require Import Utf8 Nat PeanoNat Lia Compare_dec.
Import Nat.
Section Pigeons.
Variable f : nat -> nat.
Variable Σ N : nat.
Hypothesis bound : ∀i, i < N -> f i < Σ.
Hypothesis overflow : Σ < N.
@bergwerf
bergwerf / primes.txt
Created August 21, 2020 21:10
Binary Turing Machine that recognizes primes
; http://morphett.info/turing/turing.html
0 * * l start1
start1 * _ l start2
start2 * 1 l start3
start3 * _ l start4
start4 * 1 r first1
first1 _ _ r first2
@bergwerf
bergwerf / bb5.go
Last active May 28, 2020 22:11
A Busy Beaver simulator and Pixmap writer
package main
import "fmt"
type tm struct {
halt int
states []state
}
type trans struct {
@bergwerf
bergwerf / demos.v
Last active May 25, 2020 10:47
Coq demos of CoInductive, Setoid, Function
(* Examples of co-induction, quotient types (setoid), custom termination *)
Require Import Coq.Init.Nat.
Require Import Coq.Unicode.Utf8.
Require Import Coq.Arith.Mult.
Require Import Coq.Arith.Compare_dec.
Require Import Coq.Classes.RelationClasses.
Require Import Coq.Classes.Morphisms.
Require Import Coq.micromega.Lia.
Import PeanoNat.Nat.
@bergwerf
bergwerf / puzzle.v
Created March 3, 2020 23:38
Is this proof fundamentally impossible in Coq? (without resorting to Sigma types)
Require Import Coq.Lists.List.
Require Import Coq.micromega.Lia.
Import ListNotations.
Arguments In {_}.
Lemma problem N (P : nat -> Prop) (Q : nat -> nat -> Prop) :
(forall n : nat, P n -> n <= N) ->
(forall n : nat, n <= N -> exists x, Q n x) ->
exists l : list nat, forall n, P n -> exists x, In x l /\ Q n x.
Proof.