ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Combine-Chap7_Sequence Operator
    Combine 2021. 12. 10. 18:01
    import Foundation
    import Combine
    
    var subscriptions = Set<AnyCancellable>()
    
    
    func example(of name: String, closure: () -> Void) {
        print("-----------------\(name)----------")
        
        closure()
    }
    
    example(of: "hi") {
        print("hi")
        
    }
    
    
    
    example(of: "min") {
        let publisher = [1,-50,246,0].publisher
        
        publisher
            .print("publisher")
            .min()
            .sink(receiveValue: {print("Lowest value is \($0)")})
            .store(in: &subscriptions)
    }
    
    // using own comparator
    example(of: "min non-Comparable") {
        let publisher = ["12345",
                         "ab",
                         "hello world"]
            .compactMap{$0.data(using: .utf8)}
            .publisher
        
        publisher
            .print("publisher")
            .min { $0.count < $1.count}
            .sink(receiveValue: {data in
                let string = String(data: data, encoding: .utf8)!
                print("Smallest data is \(string), \(data.count) bytes")
            })
            .store(in: &subscriptions)
    }
    
    example(of: "max") {
        let publisher = ["A", "F", "Z", "E"].publisher
        
        publisher
            .print("publisher")
            .max()
            .sink(receiveValue: {print("Highest value is \($0)")})
            .store(in: &subscriptions)
    }
    
    example(of: "first") {
        let publisher = ["A", "B", "C"].publisher
        
        publisher
            .print("publisher")
            .first()
            .sink(receiveValue: {print("First valie is \($0)")})
            .store(in: &subscriptions)
    }
    
    
    example(of: "first(where:)") {
        let publisher = ["J", "O", "H", "N"].publisher
        
        publisher
            .print("publisher")
            .first(where: {"Hello World".contains($0)})
            .sink(receiveValue: {print("First match is \($0)")})
            .store(in: &subscriptions)
    }
    
    example(of: "last") {
        let publisher = ["A","B","C"].publisher
        
        publisher
            .print("publisher")
            .last()
            .sink(receiveValue: {print("Last value is \($0)")})
            .store(in: &subscriptions)
    }
    
    
    
    example(of: "output(at:)") {
        let publisher = ["A", "B", "C"].publisher
        
        publisher
            .print("publisher")
            .output(at: 1)
            .sink(receiveValue: {print("Value at index 1. is \($0)")})
            .store(in: &subscriptions)
    }
    
    
    example(of: "output(in:)") {
        let publisher = ["A", "B", "C", "D", "E"].publisher
        
        publisher
            .output(in: 1...3)
            .sink(receiveCompletion: {print($0)}, receiveValue: {print("Value in range: \($0)")})
            .store(in: &subscriptions)
    }
    
    
    example(of: "count") {
      // 1
      let publisher = ["A", "B", "C"].publisher
    // 2
      publisher
        .print("publisher")
        .count()
        .sink(receiveValue: { print("I have \($0) items") })
        .store(in: &subscriptions)
    }
    
    example(of: "contains") {
        let publisher = ["A", "B", "C", "D", "E"].publisher
        let letter = "F"
        
        publisher
            .print("publisher")
            .contains(letter)
    //        .sink(receiveValue: {contains in
    //            print(contains ? "Publisher emitted \(letter)!" : "Publisher never emitted \(letter)")
    //        })
            .sink(receiveCompletion: {print($0)}, receiveValue: {contains in
                print(contains ? "Publisher emitted \(letter)!" : "Publisher never emitted \(letter)")
            })
            .store(in: &subscriptions)
    }
    
    
    example(of: "contains(where:)") {
        struct Person {
            let id: Int
            let name: String
        }
        
        let people = [
           (456, "Scott Gardner"),
           (123, "Shai Mishali"),
           (777, "Marin Todorov"),
           (214, "Florent Pillet")
         ]
         .map(Person.init)
         .publisher
        
        people
            .contains(where: {$0.id == 800 || $0.name == "Marin Todorov"})
    //        .sink { contains in
    //            print(contains ? "Criteria matchees!" : "Couldn't find a match for the criteria")
    //        }
            .sink(receiveValue: { contains in
                print(contains ? "Criteria matches!" : "Couldn't find a match for the criteria")})
            .store(in: &subscriptions)
    }
    
    
    example(of: "allSatisfy") {
        let publisher = stride(from: 0, to: 5, by: 2).publisher
        
        publisher
            .print("publisher")
            .allSatisfy { $0 % 2 == 0 }
            .sink { allEven in
                print(allEven ? "All numbers are even" : "Something is odd...")
            }
            .store(in: &subscriptions)
    }
    
    example(of: "reduce1") {
        let publisher = [1,2,3,4,5].publisher
        
        publisher
            .reduce(-1) { old , new in
                print("old value: \(old), new value: \(new)")
                return old + new
            }
            .sink { print("emitted value is \($0)")
            }
            .store(in: &subscriptions)
    }
    
    example(of: "reduce2") {
    //    let publisher = [1,2,3,4,5].publisher
        let publisher = ["Hel", "lo", " ", "Wor", "ld", "!"].publisher
        
        publisher
            .reduce("") { accumulator , value in
    //            print("old value: \(accumulator), new value: \(new)")
                return accumulator + value
            }
            .sink { print("emitted value is :      \($0)")
            }
            .store(in: &subscriptions)
    }
    
    example(of: "reduce2") {
    //    let publisher = [1,2,3,4,5].publisher
        let publisher = ["Hel", "lo", " ", "Wor", "ld", "!"].publisher
        
        publisher
            .reduce("", +)
            .sink { print("emitted value is :      \($0)")
            }
            .store(in: &subscriptions)
    }

    'Combine' 카테고리의 다른 글

    Combine_Chap5_CombiningOperators  (0) 2021.11.26
    Combine_Chap2_Publishers&Subscribers  (0) 2021.11.23
    Combine_Chap4_FilteringOperators  (0) 2021.11.21
    Combine_Chap3_TransformingOperators  (0) 2021.11.21
    Swift - Combine  (0) 2021.11.16
Designed by Tistory.