This file contains hidden or 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
/** | |
Find the border between a repeating value and anything else. | |
- Parameters: | |
- key: The repeating value | |
- range: The range of indexes to search for the border within. | |
- f: A function to generate values. | |
- Returns: The last index of key. | |
*/ | |
func borderBinarySearchFLeft<T: Comparable>(lowerKey: T? = nil, range: Range<Int>, f: (Int) -> (T?)) -> Int? { | |
var lBound = range.lowerBound |
This file contains hidden or 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 CoreML | |
extension MLModel { | |
/** | |
Use the model: `let model = try MLModel(contentsOf: permanentURL)` | |
*/ | |
func download(url: URL) async throws -> URL { | |
let request = URLRequest(url: url) | |
This file contains hidden or 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 localGravity(latitude: Double, elevation: Double) -> Double { | |
let a: Double = 0.0053024 | |
let b: Double = 0.0000058 | |
let l = latitude * .pi / 180 | |
let sinl: Double = sin(l) | |
let sinl2: Double = sinl * sinl | |
let sin2l: Double = sin(l * 2) | |
let sin2l2: Double = sin2l * sin2l | |
let t: Double = 1 / 1000000 |
This file contains hidden or 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 ReplayKit | |
class ScreenRecorderModel { | |
private let recorder = RPScreenRecorder.shared() | |
var isAvailable: Bool { recorder.isAvailable } | |
func start(handler: ((Error?) -> Void)? = nil) { | |
recorder.startRecording(handler: handler) | |
} |
This file contains hidden or 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 Combine | |
extension Publisher { | |
func retryWithDelay<T, E>(retries: Int, interval: DispatchQueue.SchedulerTimeType.Stride) -> Publishers.Catch<Self, AnyPublisher<T, E>> where T == Self.Output, E == Self.Failure { | |
self.catch { error -> AnyPublisher<T, E> in | |
Publishers.Delay( | |
upstream: self, | |
interval: interval, | |
tolerance: 1, | |
scheduler: DispatchQueue.global() |
This file contains hidden or 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 | |
struct ViewSize: ViewModifier { | |
struct ViewSizePreferenceKey: PreferenceKey { | |
typealias Value = CGSize | |
static var defaultValue: Value = .zero | |
static func reduce(value _: inout Value, nextValue: () -> Value) { | |
_ = nextValue() | |
} |
This file contains hidden or 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
class Test { | |
var greeting = "Hello, playground" | |
} | |
let test = Test() | |
func setter<O: AnyObject, V>(_ obj: O, _ keyPath: ReferenceWritableKeyPath<O, V>) -> (V) -> () { | |
{ [weak obj] value in obj?[keyPath: keyPath] = value } | |
} |
This file contains hidden or 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
private struct ColorToggle: View { | |
@Binding var isOn: Bool | |
let color: Color | |
var body: some View { | |
Toggle("", isOn: $isOn) | |
.toggleStyle(ColorToggleStyle(color: color)) | |
.buttonStyle(.borderless) | |
} |
This file contains hidden or 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
let t: TimeInterval = 0.1337 | |
let measurement = Measurement<UnitDuration>(value: t, unit: .seconds) | |
.converted(to: .milliseconds) | |
measurement.formatted( | |
.measurement( | |
width: .narrow, | |
numberFormatStyle: .number.precision(.significantDigits(3)) | |
) |
This file contains hidden or 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 Color { | |
var sRGBA: Int { | |
guard let components = UIColor(self).cgColor.components?.map({ Int(0xFF * $0) }) else { return 0 } | |
return (components[3] << 24) | (components[0] << 16) | (components[1] << 8) | components[2] | |
} | |
var hex: String { | |
String(sRGBA, radix: 16, uppercase: true) | |
} | |
var named: String { | |
UIColor(self).accessibilityName |
NewerOlder