Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created March 13, 2017 17:17
Show Gist options
  • Save deyindra/4538dde6f0b1ff0f92632735bb186dc4 to your computer and use it in GitHub Desktop.
Save deyindra/4538dde6f0b1ff0f92632735bb186dc4 to your computer and use it in GitHub Desktop.
Lazy Prime Stream
object PrimeStream {
def apply(end : Int):Stream[Int] = {
def greater:(Int,Int) =>Boolean = (x,y) => x>=y
def transform: (Int) => Int = (x) => x+1
val intStream:Stream[Int] = Stream(2,end)(greater,transform)
def primeStream(stream:Stream[Int]) : Stream[Int] = {
new ConStream[Int](stream.head, primeStream(Stream(stream.tail)(_%stream.head!=0)))
}
primeStream(intStream)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment