Skip to content

Instantly share code, notes, and snippets.

View nicholaslythall's full-sized avatar

Nicholas Lythall nicholaslythall

View GitHub Profile
@nicholaslythall
nicholaslythall / Equatable+TestCases.swift
Last active November 29, 2025 22:48
Generic Equatable testing
protocol Randomizable {
static func randomized() -> Self
}
/// - returns: An array of producer functions that will return a value `T`, using the given `factory`, where each producer invokes
/// `factory` with a different combination of parameters. When used to construct a test matrix, the number of returned producers is enough to
/// verify that the equality implementation of `T` correctly considers each input parameter.
func makeTestCasesForEquality<T, each U: Randomizable & Equatable>(factory: @escaping (repeat each U) -> T) -> [() -> T] {
/// - returns: two different random values of the given `type`
@nicholaslythall
nicholaslythall / sleepytimes.sh
Created August 31, 2025 00:36
Calculate how long and when you've been at your computer today.
#!/bin/bash
set -e
# ANSI color codes
GREEN="\033[0;32m"
RED="\033[0;31m"
BLUE="\033[0;34m"
GREY="\033[0;90m"
YELLOW="\033[0;33m"
MAGENTA="\033[0;35m"
@nicholaslythall
nicholaslythall / sleepytimes.sh
Last active August 31, 2025 00:35
Calculate how long and when you've been at your computer today.
#!/bin/bash
set -e
# ANSI color codes
GREEN="\033[0;32m"
RED="\033[0;31m"
BLUE="\033[0;34m"
GREY="\033[0;90m"
YELLOW="\033[0;33m"
MAGENTA="\033[0;35m"
struct DynamicShapeStyle: ShapeStyle {
let light: AnyShapeStyle
let dark: AnyShapeStyle
init<Light: ShapeStyle, Dark: ShapeStyle>(light: Light, dark: Dark) {
self.light = AnyShapeStyle(light)
self.dark = AnyShapeStyle(dark)
}
func resolve(in environment: EnvironmentValues) -> some ShapeStyle {
@nicholaslythall
nicholaslythall / Sorted.swift
Created September 7, 2020 23:08
Swift property wrapper that ensures a wrapped array is always sorted, with convenience initializers for Comparable elements and sorting by a KeyPath
@propertyWrapper
struct Sorted<Value> {
var wrappedValue: [Value] {
didSet {
wrappedValue.sort(by: comparator)
}
}
typealias Comparator = (Value, Value) -> Bool
@nicholaslythall
nicholaslythall / Middleware.swift
Created April 27, 2018 02:41
Basic middleware implementation in swift
import Foundation
class MiddlewareStack<T, U> {
typealias Next = (T) -> U
typealias Layer = (T, Next) -> U
private var layers = [Layer]()
private let center: Next
private var stack: Next