Skip to content

Instantly share code, notes, and snippets.

View Alex-Ozun's full-sized avatar

Alex Ozun Alex-Ozun

View GitHub Profile
@Alex-Ozun
Alex-Ozun / typestate-tesla-car.swift
Last active March 22, 2024 10:03
Typestate in Swift
enum Parked {}
enum Driving {}
enum Gaming {}
private class EngineSystem {
static var shared = EngineSystem()
private init() {}
func start() {/**/}
func accelerate() { /* Uses gas pedal input to accelerate the real car */ }
@Alex-Ozun
Alex-Ozun / gist:d61e89b4ebfc97a8790c2262bf068769
Last active January 12, 2024 19:54
AppDevCon 2023 - Type-Driven Design - Reading List
// My new website. A lot of Type-Driven Design/Development content is coming in the next few weeks. Stay tuned.
https://swiftology.io
// All articles used in the talk
https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
https://www.parsonsmatt.org/2017/10/11/type_safety_back_and_forth.html
https://pragprog.com/titles/swdddf/domain-modeling-made-functional/
https://blog.codinghorror.com/falling-into-the-pit-of-success/
https://github.com/pointfreeco/swift-tagged
https://existentialtype.wordpress.com/2011/03/15/boolean-blindness/
@Alex-Ozun
Alex-Ozun / RealmWrapper.swift
Last active July 11, 2018 10:33
Thread safe wrapper around Realm database. iOS. Swift 4.1
public final class RealmWrapper {
private let _workerQueue = DispatchQueue(label: "RealmWrapper",
qos: .utility)
/// Creates Realm object on designated thread. This gives you oportunity to query some object in a thread-safe environment or resolve ThreadSafeReference before performing writes.
///
/// - Parameter block: accepts a newely created Realm object. which relates to a designated database thread.
public func open(_ block: @escaping (Realm?) -> Void) {
_workerQueue.async {
do {
block(try Realm())
@Alex-Ozun
Alex-Ozun / syncGetter.swift
Last active July 5, 2018 12:25
Convert async operation into sync getter
func syncGetter<T>(block: (@escaping (T?) -> Void) -> Void) -> T? {
let dispatchGroup = DispatchGroup()
var returnValue: T?
dispatchGroup.enter()
block() { value in
returnValue = value
dispatchGroup.leave()
}
dispatchGroup.wait()
return returnValue
@Alex-Ozun
Alex-Ozun / synchronized.swift
Created July 5, 2018 11:47
synchronized accessor in Swift
func synchronized<T>(_ object: Any, block: () -> T) -> T {
objc_sync_enter(object)
let result = block()
objc_sync_exit(object)
return result
}
@Alex-Ozun
Alex-Ozun / AsyncOperation.swift
Last active July 5, 2018 10:29
Simple starter snippet for custom async operation
public class AsyncOperation: Operation {
public enum OperationError: Error {
case cancelled
}
public var isFailed: Bool = false
private var _completion: (Error?) -> Void
private var _execution: ((Error?) -> Void) -> Void
private var _isFinished = false
private var _isExecuting = false
@Alex-Ozun
Alex-Ozun / failingMap.swift
Last active June 29, 2016 07:51
failingMap extension for SequanceType in Swift
extension SequenceType {
/**
Return an Optional<Array> containing the non-nil results of mapping transform over self or nil if any transform fails.
Complexity: O(N).
*/
public func failingMap<T>(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T]? {
let initialCapacity = self.underestimateCount()
var result = ContiguousArray<T>()
result.reserveCapacity(initialCapacity)