Created
July 3, 2016 22:40
-
-
Save RubenVerborgh/9569bff4cc4416aafa83746d9514e125 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes with AsyncIterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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