Skip to content

Instantly share code, notes, and snippets.

@vnnvanhuong
Created October 22, 2017 13:28
Show Gist options
  • Save vnnvanhuong/920793af9012d93f3935f5438ddaed21 to your computer and use it in GitHub Desktop.
Save vnnvanhuong/920793af9012d93f3935f5438ddaed21 to your computer and use it in GitHub Desktop.
package vn.nvanhuong.hellojava.java8inaction;
import java.util.stream.IntStream;
public class MyMathUtils {
public static void main(String[] args) {
primes(numbers());
}
static IntStream numbers() {
return IntStream.iterate(2, n -> n + 1);
}
static int head(IntStream numbers) {
return numbers.findFirst().getAsInt();
}
static IntStream tail(IntStream numbers) {
return numbers.skip(1);
}
static IntStream primes(IntStream numbers) {
int head = head(numbers);
return IntStream.concat(IntStream.of(head),
primes(tail(numbers).filter(n -> n % head != 0)) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment