Skip to content

Instantly share code, notes, and snippets.

@RubenVerborgh
Created July 3, 2016 22:40
Show Gist options
  • Save RubenVerborgh/9569bff4cc4416aafa83746d9514e125 to your computer and use it in GitHub Desktop.
Save RubenVerborgh/9569bff4cc4416aafa83746d9514e125 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes with AsyncIterator
var AsyncIterator = require('asynciterator'),
TransformIterator = AsyncIterator.TransformIterator;
function EratosthenesIterator(base) {
TransformIterator.call(this);
base = base || AsyncIterator.range(2);
base.once('data', prime => {
this._push(prime);
this.source = new EratosthenesIterator(base.filter(n => n % prime));
});
}
TransformIterator.subclass(EratosthenesIterator);
module.exports = EratosthenesIterator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment