Generators Buffers


I’m struggling to write a generator that takes another generator and buffers its result.

For example, I want to compute the delta between two coordinate pairs which are yielded from a generator one by one, e.g.
[x1, y1] and [x2, y2].

Preferably I’d have a partitionGen gen that takes any other gen and a partition size, e.g.

partitionGen(2, coordsGen) but I haven’t been able to implement something that would do the job. Still can’t wrap my head around generators.

Thanks for the help in advance!

  • d

Here’s a quick notebook implementing a generator buffering values from another generator:

Edit do_something and coordGen as necessary and see if it does what you want.

I think I learned most of this from this MDN page.

Hope this helps!

1 Like

If you want to consume values from a generator, typically the easiest thing to do is to treat the generator as an iterable and use a for-of loop. Here are few examples: