Skip to content

Instantly share code, notes, and snippets.

@omidgolparvar
Created July 10, 2025 09:41
Show Gist options
  • Select an option

  • Save omidgolparvar/dee6f70288ad86c1f2f30cd3ab493420 to your computer and use it in GitHub Desktop.

Select an option

Save omidgolparvar/dee6f70288ad86c1f2f30cd3ab493420 to your computer and use it in GitHub Desktop.
protocol Flow: Equatable {
func isEqual(to value: Self) -> Bool
}
enum Flows {}
extension Flows {
struct FlowOne: Flow, CustomStringConvertible {
let name: String
var description: String { "FlowOne(name: \(name))"}
func isEqual(to value: Flows.FlowOne) -> Bool {
name == value.name
}
}
struct FlowTwo: Flow, CustomStringConvertible {
let age: Int
var description: String { "FlowTwo(age: \(age))" }
func isEqual(to value: Flows.FlowTwo) -> Bool {
age == value.age
}
}
struct FlowThree: Flow, CustomStringConvertible {
var description: String { "FlowThree()" }
func isEqual(to value: Flows.FlowThree) -> Bool {
true
}
}
enum FlowFour: Flow, CustomStringConvertible {
case fourOne, fourTwo, fourThree
func isEqual(to value: Flows.FlowFour) -> Bool {
switch (self, value) {
case (.fourOne, .fourOne),
(.fourTwo, .fourTwo),
(.fourThree, .fourThree):
true
default:
false
}
}
var description: String {
switch self {
case .fourOne: "FlowFour(.fourOne)"
case .fourTwo: "FlowFour(.fourTwo)"
case .fourThree: "FlowFour(.fourThree)"
}
}
}
}
extension Flow where Self == Flows.FlowOne {
static func one(_ name: String) -> Self { Self(name: name) }
}
extension Flow where Self == Flows.FlowTwo {
static func two(_ age: Int) -> Self { Self(age: age) }
}
extension Flow where Self == Flows.FlowThree {
static var three: Self { Self() }
}
extension Flow where Self == Flows.FlowFour {
static func four(_ param: Self) -> Self {
param
}
}
func ==<First: Flow, Second: Flow>(lhs: First, rhs: Second) -> Bool {
print(#function, "Equality-Checking:", "different-type:")
guard let rhs2 = rhs as? First else { return false }
return lhs.isEqual(to: rhs2)
}
func ~=<First: Flow, Second: Flow>(lhs: First, rhs: Second) -> Bool {
print(#function, "Pattern-Matching:")
guard let rhs2 = rhs as? First else { return false }
return lhs == rhs2
}
func checkFlow(_ flow: any Flow) {
switch flow {
case .one("Omid"):
print(#function, "FlowOne:", "(name: Omid)")
case .two(35):
print(#function, "FlowTwo:", "(age: 35)")
case .three:
print(#function, "FlowThree:", "()")
case .four(.fourTwo):
print(#function, "FlowFour:", "(.fourTwo)")
default:
print(#function, "Unknown.")
}
}
let flow0: any Flow = .one("Alireza")
let flow1: any Flow = .one("Ali")
let flow2: any Flow = .one("Reza")
let flow3: any Flow = .two(10)
let flow4: any Flow = .two(20)
let flow5: any Flow = .two(35)
let flow6: any Flow = .three
let flow7: any Flow = .four(.fourOne)
let flow8: any Flow = .four(.fourTwo)
let array: [any Flow] = [flow0, flow1, flow2, flow3, flow4, flow5, flow6]
print("--------")
for (index, flow) in array.enumerated() {
print("Check-Flow:", "index: \(index)", "flow: \(flow)")
checkFlow(flow)
print("--------")
}
var cancellables = Set<AnyCancellable>()
let subject = PassthroughSubject<any Flow, Never>()
subject
.removeDuplicates(by: { $0 == $1 })
.sink(receiveCompletion: { completion in
print("Sink:", "completion: \(completion)")
}, receiveValue: { flow in
print("Sink:", "flow: \(flow)")
})
.store(in: &cancellables)
subject.send(flow0)
subject.send(flow1)
subject.send(flow1)
subject.send(flow7)
subject.send(flow8)
subject.send(flow8)
subject.send(flow0)
subject.send(flow0)
subject.send(flow4)
subject.send(.two(20))
subject.send(completion: .finished)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment