Skip to content

Instantly share code, notes, and snippets.

@erica

erica/combine4.swift

Created Jul 11, 2020
Embed
What would you like to do?
import Foundation
import Combine
// Mapping
// Arrays are sequences.
// Adding a transform (here, map) after it affects the output values sent to a subscriber/sink
print("as tens")
[6, 9, 4, 2].publisher.map({ $0 * 10 }).sink { print($0) }
// This removes the nil values
print("non-nils")
[6, 9, nil, 4, nil, 2].publisher.compactMap({ $0 }).sink { print( $0) }
// This removes evens
print("odds")
(1 ... 10).publisher.filter({ !$0.isMultiple(of: 2) }).sink { print($0) }
// Scan does aggregation
// a, ab, abc, ...
print("aggregation 1")
"abcdefg".publisher.scan("") { seed, current in seed + String(current) }.sink { print($0) }
// 1, 2, 6, 24, 120
print("aggregation 2")
[1, 2, 3, 4, 5].publisher.scan(1) { seed, current in seed * current }.sink { print($0) }
print("Nils replaced. Output is still optional")
[6, 9, nil, 4, nil, 2].publisher.replaceNil(with: 99).sink{ print($0) }
print("Empty stream replacement value")
Array<Int>().publisher.replaceEmpty(with: 2).sink { print($0) }
[1].publisher.collect()
/*
extension Publishers.Sequence {
public func allSatisfy(_ predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Result<Bool, Failure>.Publisher
public func tryAllSatisfy(_ predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Result<Bool, Error>.Publisher
public func collect() -> Result<[Publishers.Sequence<Elements, Failure>.Output], Failure>.Publisher
public func compactMap<T>(_ transform: (Publishers.Sequence<Elements, Failure>.Output) -> T?) -> Publishers.Sequence<[T], Failure>
public func contains(where predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Result<Bool, Failure>.Publisher
public func tryContains(where predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Result<Bool, Error>.Publisher
public func drop(while predicate: (Elements.Element) -> Bool) -> Publishers.Sequence<DropWhileSequence<Elements>, Failure>
public func dropFirst(_ count: Int = 1) -> Publishers.Sequence<DropFirstSequence<Elements>, Failure>
public func filter(_ isIncluded: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure>
public func ignoreOutput() -> Empty<Publishers.Sequence<Elements, Failure>.Output, Failure>
public func map<T>(_ transform: (Elements.Element) -> T) -> Publishers.Sequence<[T], Failure>
public func prefix(_ maxLength: Int) -> Publishers.Sequence<PrefixSequence<Elements>, Failure>
public func prefix(while predicate: (Elements.Element) -> Bool) -> Publishers.Sequence<[Elements.Element], Failure>
public func reduce<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) -> T) -> Result<T, Failure>.Publisher
public func tryReduce<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) throws -> T) -> Result<T, Error>.Publisher
public func replaceNil<T>(with output: T) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure> where Elements.Element == T?
public func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) -> T) -> Publishers.Sequence<[T], Failure>
public func setFailureType<E>(to error: E.Type) -> Publishers.Sequence<Elements, E> where E : Error
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.