μ λ€λ¦ ꡬ쑰체 | μ£Όμ΄μ§ 쑰건 ν΄λ‘μ κ° μμλ₯Ό λ°ννλ κ²μ κ³μν΄μΌ νλμ§ λνλ΄λ λμ μμλ₯Ό λ€μ λ°ννλ Publisher
μ£Όμ΄μ§ 쑰건μ ν΅κ³Όνλ λμ ν΄λΉ μμλ₯Ό λ€μ λ°ννλ©°, μ£Όμ΄μ§ 쑰건μ ν΅κ³Όνμ§ λͺ»νλ©΄ μ’
λ£νλ€.
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
predicate
: μμ λ°νμ κ³μν΄μΌ νλμ§ κ²°μ νλ ν΄λ‘μ
prefix
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.PrefixWhile Publisher
Publishers
.PrefixWhile(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])) { $0 < 3 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixWhile Error")
case .finished:
print("Combine PrefixWhile Finish")
}
}, receiveValue: { value in
print("Combine PrefixWhile : \(value)")
})
.store(in: &cancellables)
// prefix Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.prefix { $0 < 3 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixWhile Error")
case .finished:
print("Combine PrefixWhile Finish")
}
}, receiveValue: { value in
print("Combine PrefixWhile : \(value)")
})
.store(in: &cancellables)
// Combine PrefixWhile : 1
// Combine PrefixWhile : 2
// Combine PrefixWhile Finish
μμ Publisherλ 1, 2, 3μ κ°μ μ°¨λ‘λλ‘ λ΄κ³ , 3 λ―Έλ§μΈ λμ κ°μ λ΄λλ‘ νλ 쑰건μ μ€μ νμλ€.
λ°λΌμ 1, 2μ κ°μ 쑰건μ ν΅κ³Όνμ§λ§ 3μ κ°μ 쑰건μ ν΅κ³Όνμ§ λͺ»νλ€.
κ²°κ³Όμ μΌλ‘ 1, 2μ κ°μ λ΄κ³ μ’
λ£νλ€.
RxSwift
takeWhile
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.from([1, 2, 3])
.takeWhile { $0 < 3 }
.subscribe(onNext: { value in
print("RxSwift PrefixWhile : \(value)")
}, onError: { _ in
print("RxSwift PrefixWhile Error")
}, onCompleted: {
print("RxSwift PrefixWhile Finish")
})
.disposed(by: disposeBag)
// RxSwift PrefixWhile : 1
// RxSwift PrefixWhile : 2
// RxSwift PrefixWhile Finish
ReactiveSwift
take
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer([1, 2, 3])
.take { $0 < 3 }
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift PrefixWhile : \(value)")
case .failed:
print("ReactiveSwift PrefixWhile Error")
case .completed:
print("ReactiveSwift PrefixWhile Finish")
default:
break
}
}
// ReactiveSwift PrefixWhile : 1
// ReactiveSwift PrefixWhile : 2
// ReactiveSwift PrefixWhile Finish
μ°Έκ³
ReactiveX - Operators - Take