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 Effect { | |
| func cancellable<Id: Hashable>(id: Id) -> Effect<Output> { | |
| return Deferred { () -> PassthroughSubject<Output, Failure> in | |
| cancellables[id]?.cancel() | |
| let subject = PassthroughSubject<Output, Failure>() | |
| cancellables[id] = self.subscribe(subject) | |
| return subject | |
| } | |
| .eraseToEffect() | |
| } |
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
| diff --git a/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift b/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift | |
| index db4ab12..ab96c5f 100644 | |
| --- a/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift | |
| +++ b/0084-testable-state-management-ergonomics/PrimeTime/ComposableArchitecture/ComposableArchitecture.swift | |
| @@ -128,3 +128,24 @@ public func logging<Value, Action>( | |
| }] + effects | |
| } | |
| } | |
| + | |
| + |
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 Publisher { | |
| func cancellable<Id: Hashable>(id: Id) -> AnyPublisher<Output, Failure> { | |
| return Deferred { () -> PassthroughSubject<Output, Failure> in | |
| cancellables[id]?.cancel() | |
| let subject = PassthroughSubject<Output, Failure>() | |
| cancellables[id] = self.subscribe(subject) | |
| return subject | |
| } | |
| .eraseToAnyPublisher() |
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 Store { | |
| public func send<LocalValue>( | |
| _ action: @escaping (LocalValue) -> Action, | |
| binding keyPath: KeyPath<Value, LocalValue> | |
| ) -> Binding<LocalValue> { | |
| Binding( | |
| get: { self.value[keyPath: keyPath] }, | |
| set: { self.send(action($0)) } | |
| ) | |
| } |
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 Foundation | |
| struct Parser<A> { | |
| let run: (inout Substring) -> A? | |
| } | |
| extension Parser { | |
| func map<B>(_ f: @escaping (A) -> B) -> Parser<B> { | |
| return Parser<B> { str -> B? in | |
| self.run(&str).map(f) |
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 optional<A>(_ parser: Parser<A>) -> Parser<A?> { | |
| return Parser<A?> { str in | |
| .some(parser.run(&str)) | |
| } | |
| } |
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 View { | |
| func presentation(_ alert: Alert?) -> some View { | |
| guard let alert = alert else { return AnyView(self) } | |
| let binding = Binding<Bool>.constant(true) | |
| return AnyView(self.presentation(binding) { alert }) | |
| } | |
| } |
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
| // A keypath-like structure for enums. | |
| struct EnumKeyPath<Whole, Part> { | |
| // Given an enum, we can try to get the associated value in a case. | |
| let get: (Whole) -> Part? | |
| // Given an associated value, we can plug it into the enum. | |
| let set: (Part) -> Whole | |
| } |
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 Foundation | |
| import PlaygroundSupport | |
| import UIKit | |
| class VC: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.view.backgroundColor = .white | |
| self.view.translatesAutoresizingMaskIntoConstraints = false |
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 their<Root, Value>( | |
| _ f: @escaping (Root) -> Value, | |
| _ g: @escaping (Value, Value) -> Bool | |
| ) | |
| -> (Root, Root) | |
| -> Bool { | |
| return { g(f($0), f($1)) } | |
| } |