import UIKit
import Combine
var subscriptions = Set<AnyCancellable>()
func example(of name: String, closure: @escaping () -> Void) {
print("-------------------- \(name) --------------------")
closure()
}
example(of: "test") {
//1. Create a new publisher, which will emit a finite number of values — 1 through 10,
// and then complete, using the publisher property on Sequence types.
let numbers = (1 ... 10).publisher
// 2. Use the filter operator, passing in a predicate where you only allow through
// numbers that are multiples of three.
numbers
.filter{$0.isMultiple(of: 3)}
.sink { print("\($0) is multiple of 3")}
.store(in: &subscriptions)
}
example(of: "removeDuplicates") {
let words = "hey hey there! want to listen to mister mister ?"
.components(separatedBy: " ")
.publisher
words
.removeDuplicates()
.sink(receiveValue: {print($0)})
.store(in: &subscriptions)
}
// compactMap(_ transform: (Int) throws -> ElementOfResult?) rethrows -> [ElementOfResult]
// compactMap: Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
example(of: "compactMap") {
let strings = ["a", "1.24", "3", "def", "45", "0.23"].publisher
strings
.compactMap{Float($0)}
.sink(receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "ignoreOutput") {
let strings = ["a", "1.24", "3", "def", "45", "0.23"].publisher
strings
.compactMap{Float($0)}
.ignoreOutput()
// .sink(receiveValue: {print($0)})
.sink(receiveCompletion: {print("Completed with: \($0)")}, receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "first(where:)") {
let numbers = (1...9)
.publisher
numbers
.print()
.first(where: { $0 % 2 == 0})
.sink(receiveCompletion: {print("completed! \($0)")}, receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "last(where:)") {
let numbers = (1...9).publisher
numbers
.last(where: {$0 % 2 == 0})
.sink(receiveCompletion: {print($0)}, receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "last(where:)2") {
let numbers = PassthroughSubject<Int, Never>()
numbers
.last(where: {$0 % 2 == 0})
.sink(receiveCompletion: {print("Completed with: \($0)")}, receiveValue: {print($0)})
.store(in: &subscriptions)
numbers.send(1)
numbers.send(2)
numbers.send(3)
numbers.send(4)
numbers.send(5)
numbers.send(6)
numbers.send(completion: .finished)
}
example(of: "dropFirst") {
let numbers = (1...10).publisher
numbers
.dropFirst(7)
.sink(receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "dropWhile") {
let numbers = (1...10).publisher
numbers
.drop(while: {$0 % 6 != 0}) // drop until it becomes true and then emit
.sink(receiveValue: {print($0)})
.store(in: &subscriptions)
}
// PassthroughSubject : you can manually send values through.
// drop(untilOutputFrom:) : Ignores elements from the upstream publisher until it receives an element from a second publisher.
// forEach: Calls the given closure on each element in the sequence in the same order as a for-in loop
example(of: "drop(untilOutputFrom:)") {
let isReady = PassthroughSubject<Void, Never>()
let taps = PassthroughSubject<Int, Never>()
taps
.drop(untilOutputFrom: isReady)
.sink(receiveValue: {print($0)})
.store(in: &subscriptions)
(1...5).forEach { n in
taps.send(n)
if n == 3 {
isReady.send()
}
}
}
example(of: "prefix") {
let numbers = (1...10).publisher
numbers
.prefix(3)
.sink(receiveCompletion: {print($0)}, receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "prefix(while:)") {
let numbers = (1...10).publisher
numbers
.prefix(while: {$0 < 3}) // emit until it becomes false. and if that is the case, finish.
.sink(receiveCompletion: {print($0)}, receiveValue: {print($0)})
.store(in: &subscriptions)
}
example(of: "prefix(untilOutputFrom:)") {
let isReady = PassthroughSubject<Void, Never>()
let taps = PassthroughSubject<Int, Never>()
taps
.prefix(untilOutputFrom: isReady)
.sink(receiveCompletion: {print("Completed with: \($0)")}, receiveValue: {print($0)})
.store(in: &subscriptions)
(1...5).forEach { n in
taps.send(n)
if n == 2 {
isReady.send()
}
}
}