Skip to content

Instantly share code, notes, and snippets.

@mperry
Last active August 29, 2015 13:59
Show Gist options
  • Save mperry/10878344 to your computer and use it in GitHub Desktop.
Save mperry/10878344 to your computer and use it in GitHub Desktop.
@Grab("org.functionaljava:functionaljava:3.1")
import fj.F
import fj.data.Stream
import groovy.transform.Memoized
@Memoized
Stream<Integer> primes() {
Stream.cons(2, { ->
Stream.range(3).filter { Integer i ->
primes().takeWhile({ Integer j -> j * j <= i } as F).forall { Integer k -> i % k != 0 }
}
})
}
println primes().takeWhile { it <= 20 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment