Skip to content

Commit

Permalink
Extend AsyncSequence with forEach function (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif authored Aug 9, 2024
1 parent 47aa728 commit d3df319
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
18 changes: 18 additions & 0 deletions Sources/Later/Extensions/AsyncSequence+Stream.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// AsyncSequence+Stream.swift
// Later
//
// Created by Leif on 8/8/24.
//

extension AsyncSequence {
/// Executes a given task for each value emitted by the stream.
/// - Parameter task: The task to be executed for each value.
public func forEach(
do task: @escaping (Element) async throws -> Void
) async throws {
for try await value in self {
try await task(value)
}
}
}
10 changes: 0 additions & 10 deletions Sources/Later/Stream/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ final public class Stream<Value: Sendable>: AsyncSequence, Sendable {
self.init(emitAction: nil, didSucceed: didSucceed, didFail: didFail, task: task)
}

/// Executes a given task for each value emitted by the stream.
/// - Parameter task: The task to be executed for each value.
public func forEach(
do task: @escaping (Value) async throws -> Void
) async throws {
for try await value in self {
try await task(value)
}
}

/// Transforms the values emitted by the stream using a given closure.
/// - Parameter transform: The closure to transform each value.
/// - Returns: A new stream with the transformed values.
Expand Down

0 comments on commit d3df319

Please sign in to comment.