Created
October 22, 2017 13:28
-
-
Save vnnvanhuong/920793af9012d93f3935f5438ddaed21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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