Skip to content

Instantly share code, notes, and snippets.

View Alwinfy's full-sized avatar

Alwinfy Alwinfy

  • United States
View GitHub Profile
def ziprec(parms, vals):
if isinstance(parms, list):
for p, v in zip(parms, vals):
yield from ziprec(p, v)
else: yield parms, vals
class Env(dict):
__slots__ = ["parent"]
def __init__(self, l, parent):
super().__init__(l)
import java.util.Arrays;
import java.util.Optional;
import java.util.Map;
import java.util.Iterator;
import java.util.AbstractMap.SimpleImmutableEntry;
public class Hamt<K, V> implements Iterable<Map.Entry<K, V>> {
static <K> boolean equals(K left, K right) {
return left.equals(right);
from warnings import catch_warnings, filterwarnings # this is always nice to see at the top of a file
class Primitive:
__slots__ = ["args"]
def __init__(self, *args): self.args = args
def __await__(self): return (yield self.args) # also a fun sight to see
async def eof(): return await Primitive("eof")
async def munch(): return await Primitive("munch")
async def choice(*args): return await Primitive("choice", args)
@Alwinfy
Alwinfy / audio_demo.py
Last active May 30, 2024 04:01
audio DSP demo (WARNING: LOUD)
#!/usr/bin/env python3
# requires `pip install pyaudio`
import pyaudio
# most DSP software on the market uses floats -1 to 1
# we instead use ints 0 to 255, due to a quirk of pyaudio
single_rotation = [int(i * 255 / 99) for i in range(100)]
# or:
{
"plugin": "Stoermelder-P1",
"model": "Stroke",
"version": "2.0",
"params": [],
"data": {
"panelTheme": 1,
"keys": [
{
"button": -1,
@Alwinfy
Alwinfy / bake.py
Last active March 25, 2024 19:57
bake a poem into a colourful cake
#!/usr/bin/env python3
# bake.py - score a poem with colourized ANSI codes
# Type a poem at stdin, print the colourized poem at stdout.
# Requires at least 256-color support from the teletype.
import sys
def ansi(*codes):
return "\x1b[" + ";".join(map(str, codes)) + "m"
def color(nib):
def modifyat(ls, ix, fn):
return ls[:ix] + (fn(ls[ix]),) + ls[ix + 1:]
def global_counter():
code = global_counter.__code__
global_counter.__code__ = code.replace(co_consts = modifyat(code.co_consts, len(code.co_consts) - 1, (1).__add__))
return 0
; wavetables
(def andW (make-wt 8000
(fn [x] (if (> x 0.5) 1 -1))))
(def xorW (make-wt 8000
(fn [x] (if (< (abs x) 0.5) 1 -1))))
(def bufferW (make-wt 8000
(fn [x] (if (> x 0.01) 1 -1))))
(def notW (hardsat -10))
; waveshapers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int acc = 0;
int buf = 0;
char *data = NULL;
char *start;
struct {
(define (slurp f . args)
(let loop ()
(define head (apply f args))
(if (eof-object? head)
'()
(cons head (loop)))))
(define (classify ch)
(case ch
[(#\#) #t]