min(by:)
Publishes the minimum value received from the upstream publisher, after it finishes.
Declaration
func min(by areInIncreasingOrder: @escaping (Self.Output, Self.Output) -> Bool) -> Publishers.Comparison<Self>Parameters
- areInIncreasingOrder:
A closure that receives two elements and returns true if they’re in increasing order.
Return Value
A publisher that publishes the minimum value received from the upstream publisher, after the upstream publisher finishes.
Discussion
Use min(by:) to determine the minimum value in the stream of elements from an upstream publisher using a comparison operation you specify.
This operator is useful when the value received from the upstream publisher isn’t Comparable.
In the example below an array publishes enumeration elements representing playing card ranks. The min(by:) operator compares the current and next elements using the rawValue property of each enumeration value in the user supplied closure and prints the minimum value found after publishing all of the elements.
enum Rank: Int {
case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
let cards: [Rank] = [.five, .queen, .ace, .eight, .king]
cancellable = cards.publisher
.min {
return $0.rawValue < $1.rawValue
}
.sink { print("\($0)") }
// Prints: "ace"After this publisher receives a request for more than 0 items, it requests unlimited items from its upstream publisher.