Last active
December 17, 2023 02:17
-
-
Save externvoid/63f007f1a96f57678961983df943a331 to your computer and use it in GitHub Desktop.
Swift Combine Operator Example, ズンドコキヨシ with Combine
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 Foundation | |
import Combine | |
let ずんどこ水源 = Timer.publish(every: 0.1, on: .main, in: .default) | |
.autoconnect() | |
.map { _ in Int.random(in:0..<2) == 0 ? "ずん" : "どこ" } | |
.share() | |
let センサー付き水門 = ずんどこ水源 | |
.scan([], { (reservoir: [String], element: String) -> [String] in | |
// 溜池 | |
var newReservoir = Array(reservoir + [element]) | |
if newReservoir.count > 5 { | |
newReservoir.removeFirst() | |
} | |
return newReservoir.prefix(5).map { $0 } | |
}) | |
.allSatisfy { $0 != ["ずん", "ずん", "ずん", "ずん", "どこ"] } | |
.map { _ in } | |
.share() | |
let スピーカー = センサー付き水門.map { _ in "き・よ・し!" } | |
.handleEvents(receiveOutput: { _ in | |
print("(せーの...!)") | |
}) | |
.delay(for: .seconds(1.0), scheduler: RunLoop.main) | |
let cancellable = ずんどこ水源 | |
.prefix(untilOutputFrom: センサー付き水門) // センサーが検知するまで、ずんどこを流す | |
.merge(with: スピーカー) // 検知したら、き・よ・し!を流す | |
.sink(receiveCompletion: { _ in | |
print("===完===") | |
}, receiveValue: { | |
print("input: \($0)") | |
}) | |
import Cocoa | |
RunLoop.current.run(until: Date.now + 3 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment