Skip to content

Instantly share code, notes, and snippets.

View nicholaslythall's full-sized avatar

Nicholas Lythall nicholaslythall

View GitHub Profile
@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
@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
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 {