Skip to content

Instantly share code, notes, and snippets.

@smoofra
smoofra / trees.ts
Created December 7, 2023 17:52
extendable trees
type Grammer = {
"expression": unknown
}
type Expr<α extends Grammer> = α["expression"]
type Plus<α extends Grammer> = {
kind: "plus"
left: Expr<α>
right: Expr<α>
@smoofra
smoofra / AsyncSemaphore.swift
Last active August 9, 2022 14:50
AsyncSemaphore
class AsyncSemaphore {
struct waiter {
var continuation: CheckedContinuation<Void,Never>
var next: UnsafeMutablePointer<waiter>?
}
var value : Int
var waitList : UnsafeMutablePointer<waiter>?
var lock = os_unfair_lock()
from html.parser import HTMLParser
from typing import *
import json
class SAXText(NamedTuple):
text : str
Attrs = List[Tuple[str,Optional[str]]]
class SAXStartTag(NamedTuple):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#include <iostream>
#include <stdio.h>
// better Expected monad in C++, using std::invoke
struct Error {};
template<typename T> class Expected;
template<typename T> struct IdempotentExpectedStruct {
#include <iostream>
#include <stdio.h>
// Expected monad in C++
struct Error {};
template<typename Enable, typename T, typename F, typename... Args>
struct Bind {
};
@smoofra
smoofra / semtest2.py
Last active August 17, 2019 00:34
semtest2.py
#!/usr/bin/python3
# In this example have 50 jobs to do concurrently, but only 10 are allowed to be
# done at a time. We use a semaphore to limit spawning the next task untill one
# of the 10 currently runninng ones finishes. At the end we gather up the results.
import asyncio
import os
import sys
#!/usr/bin/python3
import asyncio
async def doitem(n, sem):
try:
print('foo', n)
await asyncio.sleep(1)
print('bar', n)
finally:
@smoofra
smoofra / pcf_lcd.ino
Created April 29, 2019 07:20
Hello World for PCF8574 LCD
#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x27);
void setup() {
lcd.begin(20, 4); // initialize the lcd
lcd.home();
lcd.clear();
lcd.print("1234567890Hello&....");
lcd.setCursor(0,1);
@smoofra
smoofra / orbit.py
Created April 12, 2019 22:38
Fusion360: orbit the camera around the target point by 90 degrees
import adsk.core, adsk.fusion, adsk.cam, traceback
from adsk.core import *
from math import pi
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface