Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
@davidbalbert
davidbalbert / notifier.go
Last active April 22, 2023 15:55
Broadcast notifier sketches
// Lightly modified from the slides for "Rethinking Classical Concurrency Patterns"
package sync
import "context"
type state struct {
seq int64
changed chan struct{} // closed upon notify
}
package main
import (
"bufio"
"bytes"
"io"
"strings"
"golang.org/x/term"
)
func commonPrefixLen(a, b string) int {
i := 0
for ; i < len(a) && i < len(b); i++ {
if a[i] != b[i] {
break
}
}
return i
}
struct ContentView: View {
@State var atTop = true
@Namespace var ns
var body: some View {
VStack {
VStack {
Text("Top")
.border(.green)
.opacity(atTop ? 1 : 0)
@davidbalbert
davidbalbert / TransformTest.swift
Created October 2, 2022 23:19
Default CGAffineTransforms in macOS
class NormalView: NSView {
override func draw(_ dirtyRect: NSRect) {
print("NormalView", NSGraphicsContext.current!.cgContext.ctm)
}
}
struct NormalViewRepresentable: NSViewRepresentable {
func makeNSView(context: Context) -> NormalView {
NormalView()
}
@davidbalbert
davidbalbert / AnyEquatable.swift
Created September 21, 2022 02:52
Three implementations of a type-erased Equatable
// Three implementations of a type-erased Equatable
struct AnyEquatable: Equatable {
class BoxBase {
func isEqual(_ other: BoxBase) -> Bool {
fatalError("not implemented")
}
}
class Box<E: Equatable>: BoxBase {
@davidbalbert
davidbalbert / main.m
Last active August 5, 2022 01:00
NSPasteboard UTF-16 nonsense
#import <Cocoa/Cocoa.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
@implementation NSData (NSArrayConversion)
- (NSArray<NSNumber*>*)byteArray {
NSMutableArray<NSNumber*> *a = [NSMutableArray arrayWithCapacity:[self length]];
const unsigned char *bytes = [self bytes];
for (int i = 0; i < [self length]; i++) {
a[i] = [[NSNumber alloc] initWithUnsignedChar:bytes[i]];
//
// ContentView.swift
// FastNote
//
// Created by David Albert on 6/9/21.
//
import SwiftUI
struct Note: Identifiable {
@davidbalbert
davidbalbert / issue.sh
Created December 20, 2020 05:15
UniFi Certificate - acme.sh + Route 53
# Run this once in your shell. All access keys and all settings will be saved by acme.sh
DOMAIN=unifi.example.com
AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar acme.sh \
--issue -d $DOMAIN \
--dns dns_aws \
--fullchain-file /tmp/$DOMAIN.crt \
--key-file /tmp/$DOMAIN.key \
--reloadcmd /path/to/unifi-import-certificate.sh
@davidbalbert
davidbalbert / miniKanren.swift
Last active April 4, 2024 19:47
miniKanren
// miniKanren.swift, written by David Albert, and released into the public domain.
//
// An implementation of the miniKanren relational programming language. Somewhat
// reductively, relational programs can be run both forwards and backwards. In
// addition to asking "what does this function return when passed these arguments,"
// you can also ask "what arguments to this function return the following value?"
// This lets you ask questions like like "what pairs of numbers sum to 12," which
// if you allow for negative numbers has an infinite number of solutions.
//
// Some mind bending miniKanren programs that others have written include: a