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

map(_:)

Creates an asynchronous sequence that maps the given closure over the asynchronous sequence’s elements. ある非同期シーケンスを作成します、それは与えられたクロージャを非同期シーケンスのもつ要素のすべてにわたってマップします。

Declaration 宣言

func map<Transformed>(_ transform: @escaping (ChildTaskResult) async -> Transformed) -> AsyncMapSequence<ThrowingTaskGroup<ChildTaskResult, Failure>, Transformed>

Parameters パラメータ

transform

A mapping closure. transform accepts an element of this sequence as its parameter and returns a transformed value of the same or of a different type. マップを行うクロージャ。transformはこのシーケンスのひとつの要素をそれのパラメータとして受け取り、同じもしくは異なる型の変換された値を返します。

Return Value 戻り値

An asynchronous sequence that contains, in order, the elements produced by the transform closure. ある非同期シーケンス、それはtransformクロージャによって生み出された要素それらを、順番に含みます。

Discussion 解説

Use the map(_:) method to transform every element received from a base asynchronous sequence. Typically, you use this to transform from one type of element to another. map(_:)メソッドを使うことで、基底非同期シーケンスから受け取ったあらゆる要素を変換してください。概して、あなたはこれを使ってある型の要素から別のものへと変換します。

In this example, an asynchronous sequence called Counter produces Int values from 1 to 5. The closure provided to the map(_:) method takes each Int and looks up a corresponding String from a romanNumeralDict dictionary. This means the outer for await in loop iterates over String instances instead of the underlying Int values that Counter produces: この例において、Counterと呼ばれる非同期シーケンスは、Int値を1から5まで生み出します。map(_:)メソッドに提供されたクロージャは、各Intをとり、そして対応しているStringromanNumeralDict辞書から捜します。これは、外側のfor await inループがStringインスタンスそれらのすべてにわたって反復することを意味します、Counterが生み出す基礎をなすIntそれらではなく:


let romanNumeralDict: [Int: String] =
    [1: "I", 2: "II", 3: "III", 5: "V"]


let stream = Counter(howHigh: 5)
    .map { romanNumeralDict[$0] ?? "(unknown)" }
for await numeral in stream {
    print("\(numeral) ", terminator: " ")
}
// Prints: I  II  III  (unknown)  V

See Also 参照

Accessing an Asynchronous Sequence of Results 結果それらからなるある非同期シーケンスにアクセスする