Skip to content

Instantly share code, notes, and snippets.

View daltonclaybrook's full-sized avatar

Dalton Claybrook daltonclaybrook

View GitHub Profile
@daltonclaybrook
daltonclaybrook / Flag.css
Last active August 15, 2022 15:30
The US flag in React + CSS
.container {
position: relative;
display: flex;
flex-direction: column;
gap: 0px;
aspect-ratio: 1.9;
}
.stripe {
flex-basis: 0;
@daltonclaybrook
daltonclaybrook / lib.rs
Created June 23, 2022 16:36
Example of compile-time URL parsing in Rust
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use quote::quote;
use regex::Regex;
#[proc_macro]
pub fn make_url(_item: TokenStream) -> TokenStream {
let input = parse_macro_input!(_item as syn::LitStr);
let url_regex = Regex::new(r"^(\w+)://([\w-]+(?:\.[\w-]+)*)((?:/[\w-]+)*)?(\?[\w\-=&]+)?$").unwrap();
typealias Roll = Int
typealias Score = Int
enum FrameAttempt {
case strike
case spare(first: Roll)
case open(first: Roll, second: Roll)
case tenth(first: Roll, second: Roll, third: Roll?)
case inProgress(first: Roll, second: Roll?)
}
@daltonclaybrook
daltonclaybrook / TextAttributes.swift
Created January 15, 2022 19:13
An Equatable, value-semantic container for NSAttributedString attributes
import Foundation
/// An Equatable container for storing and accessing just the attributes of an NSAttributedString without
/// caring about the string itself.
struct TextAttributes: Equatable {
private var storage = NSMutableAttributedString(string: "")
mutating func setAttributes(_ attributes: [NSAttributedString.Key: Any], range: NSRange) {
preserveUniqueReferenceToStorageIfNecessary()
extendStringRangeIfNecessary(range: range)
import Foundation
protocol Placeholding {
static func placeholder() -> Self
}
struct PlaceholderArray<Element: Placeholding>: RandomAccessCollection {
typealias SubSequence = Slice<Self>
var startIndex: Int { indices.lowerBound }
import Combine
struct AwaitPublisher<Value>: Publisher {
typealias Output = Value
typealias Failure = Never
private let block: () async -> Value
init(block: @escaping () async -> Value) {
self.block = block
@daltonclaybrook
daltonclaybrook / linked_list.c
Created August 13, 2021 20:22
Doubly-linked list implementation for chapter 1 of Crafting Interpreters
//
// linked_list.c
// CLox
//
// Created by Dalton Claybrook on 8/13/21.
//
#include <stdlib.h>
#include <string.h>
#include "linked_list.h"
import Foundation
import libspacekit
public struct AstroPhoto: Equatable {
public let title: String
public let url: URL
}
private struct AstroPhotoContext {
let fetcher: AstroPhotoFetcher
import Foundation
private enum TimeoutResult<T> {
case success(T)
case timeout
}
struct TimeoutError: Error {}
/// Run an arbitrary async task that is cancelled after a provided timeout interval
@daltonclaybrook
daltonclaybrook / xcode.env
Created May 22, 2021 02:05
Dump of all environment variables available to a run script build phase in Xcode
ACTION="build"
AD_HOC_CODE_SIGNING_ALLOWED="YES"
ALTERNATE_GROUP="staff"
ALTERNATE_MODE="u+w,go-w,a+rX"
ALTERNATE_OWNER="dalton.claybrook"
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES="NO"
ALWAYS_SEARCH_USER_PATHS="NO"
ALWAYS_USE_SEPARATE_HEADERMAPS="NO"
APPLE_INTERNAL_DEVELOPER_DIR="/AppleInternal/Developer"
APPLE_INTERNAL_DIR="/AppleInternal"