labs42io / itiriri-async

A library for asynchronous iteration.

Home Page:https://labs42io.github.io/itiriri-async

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

group or chunk

jangchoe opened this issue · comments

Do you have plans to have a group or chunk function? Something like:

async function* generator() {
  yield* [1, 2, 3, 4, 5, 6, 7];
}

(async function () {
  const q = await itiririAsync(generator()).chunk(3).awaitAll();
  q.toArray();   // returns [[1,2,3],[4, 5, 6],[7]]
})();

Not for async iterables. Grouping a collection requires to know in advance the elements. You can instead await the async iterable then use the itiriri's sync groupBy method to achieve this:

(async function () {
  const q = (await itiririAsync(generator()).awaitAll()).groubBy((_, i) => Math.floor(i / 3));
  q.toArray();   // returns [[1,2,3],[4, 5, 6],[7]]
})();