Skip to content

Add helper methods to reduce a sequence into an object or another sequence #13

@dimitribouniol

Description

@dimitribouniol

Most of the time, users want to reduce a sequence, of say Bytes, into a single object.

func reduce<T>(into: T.Type = T.self, transformer: (iterator) async throws -> T) async throws -> T {
    var iterator = self.makeAsyncIterator()

    let result = try await transformer(iterator)
    
    /// Make sure we are EOF
    guard try await iterator.next() == nil
    else { throw ... }
    
    return result
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions