Skip to content

Instantly share code, notes, and snippets.

@nvanderw
nvanderw / Dockerfile
Last active October 15, 2022 04:54
bash sieve of eratosthenes
FROM alpine:latest
RUN ["apk", "add", "bash"]
COPY "sieve.sh" "$WORKDIR"
ENTRYPOINT ["./sieve.sh"]
@nvanderw
nvanderw / stv.py
Last active September 18, 2022 17:10
Meek counting method for single transferable votes
# Moved: https://github.com/nvanderw/voting/blob/main/proportional.py
@nvanderw
nvanderw / golfed.py
Created May 20, 2022 04:39
Python Blessed terminal animation -- golfed and ungolfed
(i:=__import__,n:=255,p:=lambda a:print(a,end='',flush=True),t:=i('blessed').Terminal(),H:=0,w:=t.width,h:=t.height,p(t.clear+t.home),x:=1,y:=1,X:=1,Y:=1,u:=lambda v,p,m:v if 0<p<m-1else -v,p(t.hide_cursor),all((x:=x+X,y:=y+Y,X:=u(X,x,w),Y:=u(Y,y,h),R:=i('colorsys').hsv_to_rgb(H/n,1,1),p(t.move_xy(x,y)+t.bold+t.color_rgb(*map(lambda m:int(m*n),R))+"☻"),H:=H+7&n,i('time').sleep(.01))for _ in iter(int,1)))
@nvanderw
nvanderw / sigipc.py
Last active May 7, 2022 08:58
IPC using only Unix/POSIX signals
import os
import signal
import sys
import time
import random
NUM_ITERATIONS = 10000000
NUM_WORKERS = 20
def unpack_bits(bs):
@nvanderw
nvanderw / bouncy.py
Created May 5, 2022 06:34
A multi-colored blessed screensaver
from blessed import Terminal
from colorsys import hsv_to_rgb
from time import sleep
from random import randrange, choice
from sys import stdout
# Print with no trailing newline
def printn(*args, **kwargs):
kwargs['end'] = ''
print(*args, **kwargs)
# For finding the best wordle starting word
import argparse
import itertools
import math
import random
from dataclasses import dataclass
from multiprocessing import Pool
from typing import Any
@nvanderw
nvanderw / gist:11004767
Last active August 29, 2015 14:00
Something catamorphism-like?
newtype Fix f = Fix (f (Fix f))
-- Define List as the fixed point of a type operator
data ListR a t = Nil | Cons a t
type List a = Fix (ListR a)
-- The type operator ListR represents "one layer" of the structure
-- of List. We can do something fold-like with it.
-- My question is, is there a name for f? It's like a catamorphism
-- but doesn't recurse into the data.
@nvanderw
nvanderw / PostScript.idr
Last active August 29, 2015 13:57
Proof-of-concept EDSL for writing PostScript with static stack effects.
module Main
data Literal : Type where
float : Float -> Literal
-- A PostScript program which takes a stack of one size to a stack of
-- another size.
-- (PostScript m n) takes a stack of size m and returns a stack of size n.
data PostScript : Nat -> Nat -> Type where
nop : PostScript n n
@nvanderw
nvanderw / EvenOdd.hs
Created March 19, 2014 15:09
Proof that numbers go even-odd-even-odd...
{-# LANGUAGE DataKinds, KindSignatures, GADTs #-}
data Nat = Zero | Succ Nat
data Even :: Nat -> * where
Even_base :: Even Zero
Even_induct :: Even k -> Even (Succ (Succ k))
data Odd :: Nat -> * where
Odd_base :: Odd (Succ Zero)
@nvanderw
nvanderw / Index.hs
Created March 19, 2014 06:14
Finite sets and safe list indexing
{-# LANGUAGE DataKinds, KindSignatures, GADTs #-}
data Nat = Zero | Succ Nat
-- Bounded naturals
data Fin :: Nat -> * where
FZ :: Fin (Succ n)
FS :: Fin n -> Fin (Succ n)
data Vect (n :: Nat) a where