Instance Method インスタンスメソッド

min(by:)

Returns the minimum element in the asynchronous sequence, using the given predicate as the comparison between elements. 非同期シーケンスの中の最小の要素を返します、与えられた述部を要素間の比較として使います。

Declaration 宣言

@warn_unqualified_access func min(by areInIncreasingOrder: (Element, Element) async throws -> Bool) async rethrows -> Element?

Parameters パラメータ

areInIncreasingOrder

A predicate that returns true if its first argument should be ordered before its second argument; otherwise, false. ある述部、それはそれの最初の引数がそれの2番目の引数の前に並べられるべきならばtrueを返します;そうでなければ、false

Return Value 戻り値

The sequence’s minimum element, according to areInIncreasingOrder. If the sequence has no elements, returns nil. シーケンスの最小の要素、areInIncreasingOrderによる。シーケンスが要素を持たないならば、nilを返します。

Discussion 解説

Use this method when the asynchronous sequence’s values don’t conform to Comparable, or when you want to apply a custom ordering to the sequence. このメソッドを、非同期シーケンスのもつ値それらがComparableに準拠しない時に、またはあなたがあるあつらえの順序付けをそのシーケンスに適用したい時に使ってください。

The predicate must be a strict weak ordering over the elements. That is, for any elements a, b, and c, the following conditions must hold: 述部は、それら要素に対して厳密弱順序でなければなりません。すなわち、何らかの要素ab、そしてcに対して、以下の条件が保持されなければなりません:

  • areInIncreasingOrder(a, a) is always false. (Irreflexivity) areInIncreasingOrder(a, a)は常にfalseである。(非反射)

  • If areInIncreasingOrder(a, b) and areInIncreasingOrder(b, c) are both true, then areInIncreasingOrder(a, c) is also true. (Transitive comparability) areInIncreasingOrder(a, b)areInIncreasingOrder(b, c)が両方ともtrueならば、そのときareInIncreasingOrder(a, c)もまたtrueである。(推移的比較性)

  • Two elements are incomparable if neither is ordered before the other according to the predicate. If a and b are incomparable, and b and c are incomparable, then a and c are also incomparable. (Transitive incomparability) 2つの要素は、述部によるとどちらもが他の前に並べられるならば比較できないabが比較できないならば、そしてbcが比較できないならば、そのときacもまた比較できない。(推移的比較不能性)

The following example uses an enumeration of playing cards ranks, Rank, which ranges from ace (low) to king (high). An asynchronous sequence called RankCounter produces all elements of the array. The predicate provided to the min(by:) method sorts ranks based on their rawValue: 以下の例は、トランプのランクそれらからなるある列挙、Rankを使います、それはace(低)からking(高)までに及びます。RankCounterと呼ばれる非同期シーケンスは、その配列のすべての要素を取り出します。min(by:)メソッドに提供された述部は、ランクそれらをそれらのrawValueに基づいて並べ換えます:


enum Rank: Int {
    case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}


let min = await RankCounter()
    .min { $0.rawValue < $1.rawValue }
print(min ?? "none")
// Prints: ace

See Also 参照

Finding Elements 要素を見つける