-
Notifications
You must be signed in to change notification settings - Fork 26
/
AsyncSequence+Collect.swift
39 lines (37 loc) · 1.05 KB
/
AsyncSequence+Collect.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// AsyncSequence+Collect.swift
//
//
// Created by Thibault Wittemberg on 31/12/2021.
//
public extension AsyncSequence {
/// Iterates over each element of the AsyncSequence and give it to the block.
///
/// ```
/// let sequence = AsyncLazySequence([1, 2, 3])
/// sequence.collect { print($0) } // will print 1 2 3
/// ```
///
/// - Parameter block: The closure to execute on each element of the async sequence.
func collect(_ block: (Element) async throws -> Void) async rethrows {
for try await element in self {
try await block(element)
}
}
/// Iterates over each element of the AsyncSequence and returns elements in an Array.
///
/// ```
/// let sequence = AsyncLazySequence([1, 2, 3])
/// let origin = sequence.collect()
/// // origin contains 1 2 3
/// ```
///
/// - Returns: the array of elements produced by the AsyncSequence
func collect() async rethrows -> [Element] {
var elements = [Element]()
for try await element in self {
elements.append(element)
}
return elements
}
}