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 Combine | |
func pipeReplaySubject<Output>( | |
of type: Output.Type | |
) -> ( | |
input: (Output) -> Void, | |
output: some Publisher<Output, Never> | |
) { | |
let subject = CurrentValueSubject<Optional<Output>, Never>(nil) | |
return ( |
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 TimeOutError: Error { } | |
extension Result where Failure == Error { | |
init(awaiting operation: () async throws -> Success) async { | |
do { | |
self = .success(try await operation()) | |
} catch { | |
self = .failure(error) | |
} | |
} |
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 Combine | |
//SE-361 Extensions on bound generic types (also 244 & 346) | |
extension Publisher<Int, Never> { | |
func make(byAppending value: Output) -> some Publisher<Output, Failure> { | |
append(value) | |
} | |
} | |
let p = [0].publisher.make(byAppending: 1) |
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
final class Pipe<Element> { | |
private(set) var send: (Element) -> Void = { _ in } | |
func makeStream() -> AsyncStream<Element> { | |
.init(Element.self, bufferingPolicy: .bufferingNewest(1)) { continuation in | |
send = { element in | |
continuation.yield(element) | |
} | |
} | |
} | |
} |
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 Future where Failure == Error { | |
convenience init(awaiting operation: @escaping () async throws -> Output) { | |
self.init { promise in | |
Task { | |
do { | |
let value = try await operation() | |
promise(.success(value)) | |
} catch { | |
promise(.failure(error)) | |
} |
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 UIKit | |
import OSLog | |
struct LogName { | |
let network = "Networking" | |
} | |
@dynamicMemberLookup | |
enum Log { |
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 Fibonnacci: Sequence, IteratorProtocol { | |
private var penultimate = 1 | |
private var last = 0 | |
mutating func next() -> Int? { | |
let next = penultimate + last | |
penultimate = last | |
last = next | |
return last < Int.max >> 1 ? next : nil | |
} |
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 A: Codable { | |
var a = "test" | |
var b = 1 | |
} | |
private extension Encodable { | |
var prettyPrinted: String { | |
let encoder = JSONEncoder() | |
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | |
return (try? encoder.encode(self)) |
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 UIKit | |
import PlaygroundSupport | |
protocol ProgressRenderer { | |
var draw: (Double, CGRect) -> Void { get } | |
} | |
struct ConcreteProgressRenderer: ProgressRenderer { | |
let draw: (Double, CGRect) -> Void | |
} |
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 Collection { | |
func repeatForever() -> UnfoldSequence<Element, Iterator> { | |
sequence(state: makeIterator()) { iterator in | |
iterator.next() ?? { | |
iterator = makeIterator() | |
return iterator.next() | |
}() | |
} | |
} | |
} |
NewerOlder