Skip to content

Instantly share code, notes, and snippets.

View mbrandonw's full-sized avatar

Brandon Williams mbrandonw

View GitHub Profile
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()
}
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
}
}
+
+
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()
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)) }
)
}
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)
func optional<A>(_ parser: Parser<A>) -> Parser<A?> {
return Parser<A?> { str in
.some(parser.run(&str))
}
}
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 })
}
}
// 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
}
import Foundation
import PlaygroundSupport
import UIKit
class VC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.translatesAutoresizingMaskIntoConstraints = false
func their<Root, Value>(
_ f: @escaping (Root) -> Value,
_ g: @escaping (Value, Value) -> Bool
)
-> (Root, Root)
-> Bool {
return { g(f($0), f($1)) }
}