Skip to content

Instantly share code, notes, and snippets.

@edrex
Created January 27, 2012 02:10
Show Gist options
  • Save edrex/1686549 to your computer and use it in GitHub Desktop.
Save edrex/1686549 to your computer and use it in GitHub Desktop.
class Primes extends Iterable[Int] {
import scala.collection.mutable.ListBuffer
var primes = ListBuffer[Int]()
var ints = Iterator.from(2)
def iterator = new Iterator[Int] {
def hasNext = true
def next:Int = {
ints.dropWhile(n => primes.exists(n % _ == 0))
primes += ints.next
primes.last
}
}
}
val p = new Primes
println(p.take(5))
// output: ListBuffer(2,3,4,5,6) ehhh?
//assert(p.take(5) == ListBuffer(2,3,5,7,11))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment