Publishers.Comparison

μ œλ„€λ¦­ ꡬ쑰체 | λ‹€λ₯Έ Publisherμ—μ„œ 각각의 μƒˆλ‘œμš΄ μ•„μ΄ν…œμ— λŒ€ν•˜μ—¬ 이전에 λ°œν–‰λœ μ•„μ΄ν…œκ³Ό λΉ„κ΅ν–ˆμ„ λ•Œ μ˜€λ¦„μ°¨μˆœμΌ λ•Œλ§Œ μ•„μ΄ν…œμ„ λ‹€μ‹œ λ°œν–‰ν•˜λŠ” Publisher

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream : μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • areInIncreasingOrder : 두 개의 μš”μ†Œλ₯Ό 전달받고 이듀이 μ˜€λ¦„μ°¨μˆœμ΄λ©΄ trueλ₯Ό λ°˜ν™˜ν•˜λŠ” ν΄λ‘œμ €

    • 첫 번째 μΈμžλŠ” ν˜„μž¬ λ°œν–‰λ˜λŠ” μš”μ†Œ, 두 번째 μΈμžλŠ” 이전에 λ°œν–‰λœ μš”μ†Œλ₯Ό λ‚˜νƒ€λ‚Έλ‹€.

    • ν΄λ‘œμ €μ˜ 맀 μ‹€ν–‰λ§ˆλ‹€ 이 두 개의 값을 λΉ„κ΅ν•˜μ—¬ 쑰건에 λ§žλŠ” μš”μ†Œλ₯Ό λ°˜ν™˜ν•œλ‹€.

    • 쑰건에 따라 μ˜€λ¦„μ°¨μˆœ 및 λ‚΄λ¦Όμ°¨μˆœ 섀정을 ν•  수 μžˆλ‹€.

μƒμœ„ Publisher이 λ‚΄λŠ” 값듀을 μ •λ ¬ν•˜λŠ” 것이 μ•„λ‹ˆλΌ, 쑰건에 λ§žλŠ” μ΅œμ’… ν•˜λ‚˜μ˜ κ°’λ§Œμ„ λ‚Έλ‹€.

max, min μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

이 λ•Œ Output νƒ€μž…μ΄ Comparable ν”„λ‘œν† μ½œμ„ μ±„νƒν•˜λŠ” 경우 λ³„λ„μ˜ ν΄λ‘œμ €λ₯Ό κ΅¬ν˜„ν•  ν•„μš”κ°€ μ—†λ‹€.

// Publishers.Comparison Publisher
Publishers
  .Comparison(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 3, 2, 5])) { $0 > $1 }
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Comparison Error")
    case .finished:
      print("Combine Comparison Finish")
    }
  }, receiveValue: { value in
    print("Combine Comparison : \(value)")
  })
  .store(in: &cancellables)

// Combine Comparison : 5
// Combine Comparison Finish

// max Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 3, 2, 5])
  .max()
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Comparison Error")
    case .finished:
      print("Combine Comparison Finish")
    }
  }, receiveValue: { value in
    print("Combine Comparison : \(value)")
  })
  .store(in: &cancellables)

// Combine Comparison : 5
// Combine Comparison Finish

// min Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 3, 2, 5])
  .min()
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Comparison Error")
    case .finished:
      print("Combine Comparison Finish")
    }
  }, receiveValue: { value in
    print("Combine Comparison : \(value)")
  })
  .store(in: &cancellables)

// Combine Comparison : 1
// Combine Comparison Finish

μƒμœ„ PublishersλŠ” 1, 3, 2, 5의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚΄λ©°, λ‹€μŒκ³Ό 같이 λ™μž‘ν•œλ‹€.

$0 > $1은 쑰건을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ„€μ •ν•œλ‹€.

  1. 1의 값을 λ‚΄λ©°, 첫 번째둜 λ°œν–‰λœ μš”μ†Œμ΄λ―€λ‘œ λ„˜μ–΄κ°„λ‹€.

  2. 3의 값을 λ‚΄λ©°, 3 > 1이 trueμ΄λ―€λ‘œ 3을 λ°œν–‰ν•œλ‹€.

  3. 2의 값을 λ‚΄λ©°, 2 > 3이 falseμ΄λ―€λ‘œ 3을 λ°œν–‰ν•œλ‹€.

  4. 5의 값을 λ‚΄λ©°, 5 > 3이 trueμ΄λ―€λ‘œ 5λ₯Ό λ°œν–‰ν•œλ‹€.

μ΅œμ’…μ μœΌλ‘œ 5의 값을 λ‚Έλ‹€.

max(_:) 및 min(_:) μ˜€νΌλ ˆμ΄ν„°λŠ” 말 κ·ΈλŒ€λ‘œ μƒμœ„ Publisherκ°€ μ’…λ£Œν•  λ•ŒκΉŒμ§€ 내어진 κ°’λ“€ 쀑 μ΅œλŒ€ / μ΅œμ†Œ 값을 λ°˜ν™˜ν•œλ‹€.

RxSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

ReactiveSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

μ°Έκ³ 

ReactiveX - Operators - Max

ReactiveX - Operators - Min

Last updated