This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import PhotosUI | |
struct LimitedLibraryPicker: UIViewControllerRepresentable { | |
@Binding var isPresented: Bool | |
func makeUIViewController(context: Context) -> UIViewController { | |
.init() | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AppKit | |
struct EventMonitor: AsyncSequence { | |
typealias Element = NSEvent | |
enum Scope { | |
case local | |
case global | |
} | |
var scope: Scope = .local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct OffsetIndexCollection<Base: RandomAccessCollection>: RandomAccessCollection { | |
var base: Base | |
var distance: Int | |
@inlinable @inline(__always) | |
var startIndex: Base.Index { | |
index(base.startIndex, offsetBy: distance) | |
} | |
@inlinable @inline(__always) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Float32 { | |
public static func gaussianRandomPair(using generator: inout some RandomNumberGenerator) -> (Float32, Float32) { | |
let u1 = Float.random(in: 0..<1, using: &generator) | |
let u2 = Float.random(in: 0..<1, using: &generator) | |
let r = sqrt(-2 * log(u1)) | |
let theta = 2 * Float.pi * u2 | |
let z0 = r * cos(theta) | |
let z1 = r * sin(theta) |
NewerOlder