Skip to content

Instantly share code, notes, and snippets.

struct RangeSliceCollection<Base: Collection, Ranges: Collection>: Collection where Ranges.Element == Range<Base.Index> {
var base: Base
var ranges: Ranges
var startIndex: Ranges.Index { ranges.startIndex }
var endIndex: Ranges.Index { ranges.endIndex }
func index(after i: Ranges.Index) -> Ranges.Index {
ranges.index(after: i)
}
extension Task where Success == Never, Failure == Never {
static let perpetual = Task {
await withCheckedContinuation { (_: CheckedContinuation<Void, Never>) in }
preconditionFailure()
}
}
@JadenGeller
JadenGeller / Versioned.swift
Last active May 12, 2024 09:27
useful property wrapper for prefix sums
@propertyWrapper
struct Versioned<Value>: RandomAccessCollection {
private var history: [Value]
var startIndex: Int { 0 }
var endIndex: Int { allValues.endIndex }
subscript(position: Int) -> Value {
get { allValues[position] }
set { allValues[position] = newValue }
@JadenGeller
JadenGeller / ToolbarAccessory.swift
Created May 7, 2024 02:19
Extend macOS toolbar downward in SwiftUI
import SwiftUI
extension View {
func toolbarAccessory(@ViewBuilder content: () -> some View) -> some View {
safeAreaInset(edge: .top) {
VStack(spacing: 0) {
content()
Divider()
}
.background(Material.bar, ignoresSafeAreaEdges: [.top])
func apportion(quota: Int, _ population: [Int]) -> [Int] {
var portions = population.map({ $0 / quota })
let errors = population.map({ $0 % quota })
let adjustments = (errors.reduce(0, +) + quota / 2) / quota
errors
.enumerated()
.sorted(by: { $0.element > $1.element })
.prefix(adjustments)
.forEach {
func lowestCommonAncestor<ID: Hashable>(of ids: (ID, ID), parent: (ID) -> ID?) -> ID? {
var ancestor: (ID?, ID?) = ids
var visited: (Set, Set) = ([ids.0], [ids.1])
while ancestor.0 != nil || ancestor.1 != nil {
if let ancestor = ancestor.0 {
guard !visited.1.contains(ancestor) else { return ancestor }
}
if let ancestor = ancestor.1 {
guard !visited.0.contains(ancestor) else { return ancestor }
struct NilMaxInteger<Base: FixedWidthInteger>: RawRepresentable, ExpressibleByNilLiteral {
var rawValue: Base
init(rawValue: Base) {
self.rawValue = rawValue
}
init(_ value: Base?) {
assert(value != .max)
self.init(rawValue: value ?? .max)
}
struct ResourceManager {
#if DEBUG
let ticket: Int = .random(in: 0..<Int.max)
#endif
private var retainCount: [Int]
init(capacity: Int) {
self.retainCount = .init(repeating: 0, count: capacity)
}
struct Handle: ~Copyable {
func triangularMatrixIndex(row: Int) -> Int {
row * (row + 1) / 2
}
func triangularMatrixCoordinate(_ index: Int) -> (row: Int, column: Int) {
let row = Int(sqrt(2 * Float32(index) + 0.25) - 0.5)
let column = index - triangularMatrixIndex(row: row)
return (row: row, column: column)
}
struct MergeSortedSequence<First: Sequence, Second: Sequence>: Sequence where First.Element == Second.Element {
var first: First
var second: Second
var areInIncreasingOrder: (Element, Element) -> Bool
struct Iterator: IteratorProtocol {
var first: First.Iterator
var second: Second.Iterator
var areInIncreasingOrder: (Element, Element) -> Bool