이니셜라이저는 두 개의 인자를 받는다.
시퀀스에서 nil 값을 없애는 등의 작업을 하고 싶을 때 사용할 수 있다.
// Publishers.CompactMap Publisher
Publishers.CompactMap(upstream: Publishers.Sequence<[Int?], Never>(sequence: [1, nil, 2])) { $0 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine CompactMap Error")
case .finished:
print("Combine CompactMap Finish")
}
}, receiveValue: { value in
print("Combine CompactMap : \(value)")
})
.store(in: &cancellables)
// compactMap Operator
Publishers.Sequence<[Int?], Never>(sequence: [1, nil, 2])
.compactMap { $0 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine CompactMap Error")
case .finished:
print("Combine CompactMap Finish")
}
}, receiveValue: { value in
print("Combine CompactMap : \(value)")
})
.store(in: &cancellables)
// Combine CompactMap : 1
// Combine CompactMap : 2
// Combine CompactMap Finish
Observable.from([1, nil, 2])
.compactMap { $0 }
.subscribe(onNext: { value in
print("RxSwift CompactMap : \(value)")
}, onError: { _ in
print("RxSwift CompactMap Error")
}, onCompleted: {
print("RxSwift CompactMap Finish")
})
.disposed(by: disposeBag)
// RxSwift CompactMap : 1
// RxSwift CompactMap : 2
// RxSwift CompactMap Finish
SignalProducer([1, nil, 2])
.filterMap { $0 }
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift CompactMap : \(value)")
case .failed:
print("ReactiveSwift CompactMap")
case .completed:
print("ReactiveSwift CompactMap")
default:
break
}
}
// ReactiveSwift CompactMap : 1
// ReactiveSwift CompactMap : 2
// ReactiveSwift CompactMap Finish