Generic Instance Method 総称体インスタンスメソッド

reduce(into:_:)

Returns the result of combining the elements of the asynchronous sequence using the given closure, given a mutable initial value. 非同期シーケンスの要素をこの与えられたクロージャを使って結合する結果を返します、ある可変の初期値を与えられます。

Declaration 宣言

func reduce<Result>(into initialResult: Result, _ updateAccumulatingResult: (inout Result, Element) async throws -> Void) async rethrows -> Result

Parameters パラメータ

initialResult

The value to use as the initial accumulating value. The nextPartialResult closure receives initialResult the first time the closure executes. 最初から蓄積されている値として使われる値。nextPartialResultクロージャは、initialResultを、そのクロージャが遂行される最初の時に受け取ります。

nextPartialResult

A closure that combines an accumulating value and an element of the asynchronous sequence into a new accumulating value, for use in the next call of the nextPartialResult closure or returned to the caller. あるクロージャ、それは蓄積値と非同期シーケンスのいち要素を新しい蓄積値へと結合します、nextPartialResultクロージャの次の呼び出しにおいて使うためにまたは呼び出し側に返されるように。

Return Value 戻り値

The final accumulated value. If the sequence has no elements, the result is initialResult. 最終的に蓄積された値。シーケンスが1つも要素を持たないならば、結果はinitialResultです。

Discussion 解説

Use the reduce(into:_:) method to produce a single value from the elements of an entire sequence. For example, you can use this method on a sequence of numbers to find their sum or product. reduce(into:_:)メソッドを使って、あるシーケンス全体の要素それらからある単一の値を生成してください。例えば、あなたはこのメソッドを数値からなるシーケンス上で使うことで、それらの和または積を見つけます。

The nextPartialResult closure executes sequentially with an accumulating value initialized to initialResult and each element of the sequence. nextPartialResultクロージャは、initialResultへと初期化された蓄積値とそのシーケンスの各要素を使って連続して遂行します。

Prefer this method over reduce(_:_:) for efficiency when the result is a copy-on-write type, for example an Array or Dictionary. このメソッドを、効率のためにreduce(_:_:)より優先してください、その結果がコピーオンライト型である場合、例えばArrayまたはDictionaryに対しては。

See Also 参照

Transforming a Sequence シーケンスを変形する