Skip to content

Instantly share code, notes, and snippets.

@mperry mperry/primes.groovy

Last active Aug 29, 2015
Embed
What would you like to do?
@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
You can’t perform that action at this time.