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 / Free.ml
Created February 14, 2014 04:48
Free monads in OCaml, for great referential transparency
module type Functor = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
end
module type Monad = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val return : 'a -> 'a t
@nvanderw
nvanderw / Monad.ml
Last active October 8, 2019 20:24
IO Monad in OCaml
module type FUNCTOR = sig
type 'v f
val map : ('a -> 'b) -> 'a f -> 'b f
end
module type MONAD = sig
type 'v f
val map : ('a -> 'b) -> 'a f -> 'b f
val pure : 'a -> 'a f
val join : ('a f) f -> 'a f
@nvanderw
nvanderw / example.py
Created October 18, 2012 01:19
Hacks that make Python more functional
from functional import fun
from itertools import imap, ifilter, takewhile, count
@fun(2)
def add(x,y): return x + y
# Here I define a function which takes a string and shows what it evaluates to.
@fun(1)
def evalPrint(s):
print "%s -> %s" % (s, eval(s))
@nvanderw
nvanderw / functors.py
Created August 21, 2012 07:19
Typeclass stuff in Python
import itertools
# Monad instance on a Python iterator, very similar to Haskell's list monad.
# In general, Haskell typeclass instances can be regarded as dictionaries which
# map the implemented function to its implementation. For a good explanation of
# this, see Philip Wadler's "Faith, Evolution, and Programming Languages"
# lecture. What this means for us is that we can write a function whose type
# signature in Haskell would be:
# Monad m => f m
#