Skip to content

generatorFlatMap / generatorFlat #20

@E-gy

Description

@E-gy

In a spiritual follow-up to #18, it would be nice to have flattening operations on async generators.

asyncGeneratorFlat would transform AsyncGenerator<AsyncGenerator<T, ...>> into AsyncGenerator<T>, and asyncGeneratorFlatMap (arguably more powerful) would iterate over AsyncGenerator<A, ...> with mapper function (A, ...) => AsyncGenerator<B, ...> and produce AsyncGenerator<B, ...>.

This can be especially powerful with a similar ordered: false flag, allowing for earliest-first yield at nested levels - a behavior that is not achievable via flat (nor reduction via trivial chaining of produced generators).

Those functions can be useful when aggregating 2nd layer nested data mapped 1:N to 1st layer which is also mapped 1:N to the outer data, in an asynchronous iterable manner (combine that with gRPC streams or websockets, and you can see how it can be used on the web). For example, in a data structure that lists boxes stored in every room in one data source, and the rooms in a house in a different data source, asyncGeneratorFlatMap can be used to obtain an unordered stream of all boxes in a house (presumably the 'stored in' and 'part of' relations are stored as distinct elements for every pair, with additional metadata, and retrieving them as a stream individually is more efficient than retrieving the whole block).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions