Batch an Async Stream with a Typed Generator

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write batch(source, size) as an async generator: read the items of the async iterable source and yield them in arrays of size items, in order, with a shorter last array for any left over, and nothing at all for an empty source. Type it so each array yielded holds the source's own item type, and the size must be a number.

Your code must pass

  • Yields full batches in order
  • Yields a shorter last batch
  • Yields nothing for an empty source
  • Has no type errors
  • Yields arrays of the source's items
  • Rejects a size that isn't a number