Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generatorFlatMap / generatorFlat #20

Open
E-gy opened this issue Aug 23, 2024 · 0 comments
Open

generatorFlatMap / generatorFlat #20

E-gy opened this issue Aug 23, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@E-gy
Copy link

E-gy commented Aug 23, 2024

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).

@E-gy E-gy added the enhancement New feature or request label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant