Skip to content

Instantly share code, notes, and snippets.

View wpcarro's full-sized avatar

William Carroll wpcarro

  • Hadrian
  • internet
View GitHub Profile
// HINT: Definitely use ts-result for this.
type Result<A, B> = { success: true, data: A } | { success: false, error: B };
function Ok<A>(x: A): { success: true; data: A; } {
return { success: true, data: x };
}
function Err<B>(e: B): { success: false; error: B; } {
return { success: false, error: e };
}
@wpcarro
wpcarro / generate.jsonnet
Last active July 8, 2024 17:20
Proof of concept for type-generation from jsonnet.
////////////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////////////
local string = {
typescript: 'string',
python: 'str',
psql: 'VARCHAR',
};
@wpcarro
wpcarro / turing.ts
Last active June 3, 2024 19:24
Implementing copy with two instructions: D (duplicate), T (translate)
////////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////////
// # Instruction Set # Program # Registers # Memory #
// NOTE: Things would be way easier if we knew:
// - Which sector we're currently in: Program, Registers, Memory
// - Which direction we previously moved
//
@wpcarro
wpcarro / turing.ts
Last active June 3, 2024 00:23
More ergonomic way of defining state transition ruleset
// Defines a function, compile, that can convert a textual
// representation of a state transition ruleset into a function
// that we can invoke to transition from one state to another.
//
// Also defines a simple function, decode, that converts a
// textual representation of a program into the "tape" that we
// feed our Turing Machine.
////////////////////////////////////////////////////////////////
// Types
@wpcarro
wpcarro / turing.ts
Created June 2, 2024 07:18
Baby's first Turing Machine emulator
// Turing Machine emulator implemented in TypeScript.
// For awhile I've known what a Turing Machine represented in computing,
// by regurgitating to someone that "it can compute anything". I could
// even explain how there was a tape, a read/writer, symbols, and on
// better days I would even name-drop "cellular automata", but if actually
// attempting to emulate (easy) and *program* (difficult) a Turing Machine
// was a totally new can of worms for me... a bit of a cold shower.
//
// NOTE: I cheated by incrementing the numbers (only god will judge me)
//
@wpcarro
wpcarro / convolve.ts
Created May 22, 2024 18:18
Using convolution for horizontal edge detection.
// 5x5 image
const image = [
[1, 2, 3, 4, 5],
[5, 4, 3, 2, 1],
[0, 2, 4, 6, 8],
[8, 6, 4, 2, 0],
[3, 4, 5, 2, 1],
];
// seeded for horizontal edge-detection
@wpcarro
wpcarro / logic.ts
Created May 22, 2024 17:39
Logic gates from NOT, OR
// NOT: given
// OR: given
// NOR: derived
// AND: derived
// NAND: derived
// XOR: derived
// XNOR: derived
function nor(x: boolean, y: boolean): boolean {
return !(x || y);
@wpcarro
wpcarro / sized_lru.py
Created April 6, 2024 18:24
Create an LRU cache with sized elements.
from collections import OrderedDict # this is awesome
class LRU(object):
def __init__(self, capacity: int):
self.capacity = capacity
self.xs = OrderedDict()
def __repr__(self):
return f"; ".join(f"{k}: {v[0]} ({v[1]})" for k, v in self.xs.items())
@wpcarro
wpcarro / schedule.py
Last active July 5, 2023 17:26
Proof-of-concept OR
def job_shop():
machines = [
Machine(cmm_id="1"),
Machine(cmm_id="2"),
]
jobs = [
Job(job_id="431", instances=3, duration_min=20),
Job(job_id="459", instances=1, duration_min=100), # ???
Job(job_id="449", instances=10, duration_min=40),
@wpcarro
wpcarro / form.tsx
Created March 29, 2023 17:25
Custom encoder/decoder with react-hook-form and zod.
import React from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import DimensionID from "lib/quality/DimensionID";
////////////////////////////////////////////////////////////////////////////////
// Main
////////////////////////////////////////////////////////////////////////////////