Demo entry 6867627

Optional extension

   

Submitted by anonymous on Aug 19, 2019 at 09:14
Language: Swift. Code size: 2.9 kB.

// MARK: - Emptiness
extension Optional {

    var isNone: Bool {
        switch self {
        case .none: return true
        case .some: return false
        }
    }

    var isSome: Bool {
        return !isNone
    }
}

// MARK: - Or
extension Optional {
    func or(_ default: Wrapped) -> Wrapped {
        return self ?? `default`
    }

    func or(else: @autoclosure () -> Wrapped) -> Wrapped {
        return self ?? `else`()
    }

    func or(else: () -> Wrapped) -> Wrapped {
        return self ?? `else`()
    }

    func or(throw exception: Error) throws -> Wrapped {
        guard let unwrapped = self else { throw exception }
        return unwrapped
    }
}

extension Optional where Wrapped == Error {
    func or(_ else: (Error) -> Void) {
        guard let error = self else { return }
        `else`(error)
    }
}

// MARK: - Map
extension Optional {
    func map<T>(_ fn: (Wrapped) throws -> T, default: T) rethrows -> T {
        return try map(fn) ?? `default`
    }

    func map<T>(_ fn: (Wrapped) throws -> T, else: () throws -> T) rethrows -> T {
        return try map(fn) ?? `else`()
    }
}

// MARK: - Combing Optionals
extension Optional {
    func and<B>(_ optional: B?) -> B? {
        guard self != nil else { return nil }
        return optional
    }

    func and<T>(then: (Wrapped) throws -> T?) rethrows -> T? {
        guard let unwrapped = self else { return nil }
        return try then(unwrapped)
    }

    func zip2<A>(with other: A?) -> (Wrapped, A)? {
        guard let first = self, let second = other else { return nil }
        return (first, second)
    }

    func zip3<A, B>(with other: A?, another: B?) -> (Wrapped, A, B)? {
        guard let first = self,
            let second = other,
            let third = another else { return nil }
        return (first, second, third)
    }
}

// MARK: - On
extension Optional {
    func onSome(some: (Wrapped) throws -> Void) rethrows {
        if let unwrapped = self { try some(unwrapped) }
    }

    func onNone(none: () throws -> Void) rethrows {
        if self == nil { try none() }
    }
}

// MARK: - Various
extension Optional {
    func filter(_ predicate: (Wrapped) -> Bool) -> Wrapped? {
        guard let unwrapped = self, predicate(unwrapped) else { return nil }
        return self
    }

    func expect(_ message: String) -> Wrapped {
        guard let value = self else { fatalError(message) }
        return value
    }
}

// MARK: - Assigning reusable views
extension Optional where Wrapped == NSView {
    mutating func get<T: NSView>(orSet expression: @autoclosure () -> T) -> T {
        guard let view = self as? T else {
            let newView = expression()
            self = newView
            return newView
        }
        return view
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).